Date Cannot be Printed. Why?

Hello I have the following Code:


I want to display in the textView lines 53 and 54 below. If I don't include the Date (lines 49 - 51), the information displays. But if I include the display of the date nothing displays. I would like some help in understanding why and what to do to get the date information displayed.



@IBActionfunc showPerformance(_ sender: NSButton) {
        studentsText.string = ""
        groupNameItem = ""
       
   //     var event: String = ""
        var convertedString: String = ""
        var covertedTime: String = ""
       
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let newDateFormatter = DateFormatter()
        newDateFormatter.dateFormat = "MM d"
       
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "HH-mm-ss"
        let newTimeFormatter = DateFormatter()
        newTimeFormatter.dateFormat = "h:mm a"
        var groupNameItem =  groupNameField.stringValue
        guard (NSApplication.shared.delegate as? AppDelegate) != nil  else {
                             return
        }
       
              _ = (NSApplication.shared.delegate as!
                  AppDelegate).persistentContainer.viewContext
              _ = NSFetchRequest(entityName: "GroupRecord")
              _ = NSSortDescriptor(key: "groupName", ascending: true)
                let fetchRequest: NSFetchRequest = GroupRecord.fetchRequest()
                fetchRequest.predicate = NSPredicate(format: "groupName == %@ ", groupNameItem)
                  do {
                      let items = try managedObjectContext.fetch(fetchRequest)
                      groupNameItem = ""
                           for item in items {
                            if let record0Item = item.value(forKey: "grade") as? String
                                  {
                                    print ("Record 0 Item is \(record0Item)")
                                gradeItem.append(record0Item)
                                    print ("Grade is \(gradeItem)")
                                if let record1Item = item.value(forKey: "groupName") as? String
                                {
                                   print ("Record 1 Item is \(record1Item)")
                                    groupNameItem.append(record1Item)
                                    print ("Group Name is \(groupNameItem)")
                                if let record2Item = item.value(forKey: "score") as? String
                                {
                                    print ("Record 2 Item is \(record2Item)")
                                    groupScoreItem.append(record2Item)
                                    print ("Group Score is \(groupScoreItem)")
       
                                   if let record3Item = item.value(forKey: "date") as? String
                                {
                                        dateItem.append(record3Item)
                                        print ("Record 3 Item is \(record3Item)")
                        let newLine =  ((dateItem) + ("  ") + ("Grade") + (" ") + (gradeItem) + ("  ") + (groupNameItem) + ("  ") + ("Group Score") + (" ") + (groupScoreItem))
                                    studentsText?.string = studentsText!.string + newLine + "\n"
                                    groupNameItem = ""
                                    groupScoreItem = ""
                                    gradeItem = ""
                                }
                            }
                        }
                    }
                }  
                       print("\(items)")
                       
                         print ("There are \(items.count) items")
                         print("Records are \(items)")
                            return()
                      } catch {
                           fatalError("Failed to fetch records: \(error)")
            }
    }

Could you clarify what you mean


if let record3Item = item.value(forKey: "date") as? String { 
   dateItem.append(record3Item) 
   print ("Record 3 Item is \(record3Item)") 
   let newLine =  ((dateItem) + ("  ") + ("Grade") + (" ") + (gradeItem) + ("  ") + (groupNameItem) + ("  ") + ("Gcore") + (" ") + (groupScoreItem)) 
   studentsText?.string = studentsText!.string + newLine + "\n" 
   groupNameItem = "" 
   groupScoreItem = "" 
   gradeItem = "" 
  }


I understand:

If you remove lines 1 and 2 (and probably 9), you get something printed, otherwise nothing.

Exact ?


If so, that just mean that if let fails because item.value(forKey: "date") as? String is nil.


2 possible reasons:

- the key "date" does not contain a value (check the spelling of date, including lowercase)

- item.value is not a String.


To test, add some print (as always):




print( item.value(forKey: "date"))
if let record3Item = item.value(forKey: "date") as? String {
   dateItem.append(record3Item)
   print ("Record 3 Item is \(record3Item)")
   let newLine =  ((dateItem) + ("  ") + ("Grade") + (" ") + (gradeItem) + ("  ") + (groupNameItem) + ("  ") + ("Gcore") + (" ") + (groupScoreItem))
   studentsText?.string = studentsText!.string + newLine + "\n"
   groupNameItem = ""
   groupScoreItem = ""
   gradeItem = ""
  }

Hello Claude31:


Here is an output from the output log:


date = "2020-01-21 21:44:03 +0000";

grade = 12;

groupName = SCORCENT;

negativeScore = 7700;

score = 15400;


The print statements for all the attributes in the entity GroupRecord print except the "date" attribute. I was expecting that the date in the output log would be printed. I noted that date is in quotes : date = "2020-01-21 21:44:03 +0000" and the others are not. Is that important?


Does getting output from a date require special coding in Swift?


fetchRequest.predicate = NSPredicate(format: "groupName == %@ ", groupNameItem)
                  do {
                      let items = try managedObjectContext.fetch(fetchRequest)
                      groupNameItem = ""
                           for item in items {
                            if let record0Item = item.value(forKey: "grade") as? String
                                  {
                                    print ("Record 0 Item is \(record0Item)")
                                    print( item.value(forKey: "grade")!)
                                gradeItem.append(record0Item)
                                    print ("Grade is \(gradeItem)")
                                if let record1Item = item.value(forKey: "groupName") as? String
                                {
                                print ("Record 1 Item is \(record1Item)")
                                    print( item.value(forKey: "groupName")!)
                                    groupNameItem.append(record1Item)
                                    print ("Group Name is \(groupNameItem)")
                                if let record2Item = item.value(forKey: "score") as? String
                                {
                                    print ("Record 2 Item is \(record2Item)")
                                    print( item.value(forKey: "score")!)
                                    groupScoreItem.append(record2Item)
                                    print ("Group Score is \(groupScoreItem)")
       
                                   if let record3Item = item.value(forKey: "date") as? String
                                {
                                    print ("What is going on here!!!")
                                    print( item.value(forKey: "date")!)
                                    if let record3Item = item.value(forKey: "date") as? String {
                                    dateItem.append(record3Item)
                                    print ("Record 3 Item is \(record3Item)")
                                      }
                                        dateItem.append(record3Item)
                                        print ("Record 3 Item is \(record3Item)")
   
                        let newLine =  ((dateItem) + ("  ") + ("Grade") + (" ") + (gradeItem) + ("  ") + (groupNameItem) + ("  ") + ("Group Score") + (" ") + (groupScoreItem))
                                    studentsText?.string = studentsText!.string + newLine + "\n"
                                    groupNameItem = ""
                                    groupScoreItem = ""
                                    gradeItem = ""
                                }
                            }
                        }
                    }
                }

To which lines in code does this correspond to ?


date = "2020-01-21 21:44:03 +0000";

grade = 12;

groupName = SCORCENT;

negativeScore = 7700;

score = 15400;


Please show more log so that we can understand what's going on.

pprint from line 8, 9, 11, 14, … etc …

This is a print of the CoreData datastore for entity: GroupRecord

date = "2020-01-21 21:44:03 +0000";

grade = 12;

groupName = SCORCENT;

negativeScore = 7700;

score = 15400;


The log for tests from lines 7 - 35 is as follows:

CoreData: details: SQLite: EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDATE, t0.ZGRADE, t0.ZGROUPNAME, t0.ZNEGATIVESCORE, t0.ZSCORE FROM ZGROUPRECORD t0 WHERE t0.ZGROUPNAME = ?

2 0 0 SCAN TABLE ZGROUPRECORD AS t0

Record 0 Item is 12 // line 9 print statement

12 // line 10 print statement

Grade is 12 // line 12 print statement

Record 1 Item is SCORCENT // line 16 print statement

SCORCENT // line 9 print statement

Group Name is SCORCENT // line 18 print statement

Record 2 Item is 11550 // line 21 print statement

11550 // line 22 print statement

Group Score is 11550. // line 24 print statement

There are no results for print statements on lines 28, 29, 32, 35



let fetchRequest: NSFetchRequest = GroupRecord.fetchRequest()
                fetchRequest.predicate = NSPredicate(format: "groupName == %@ ", groupNameItem)
                  do {
                      let items = try managedObjectContext.fetch(fetchRequest)
                      groupNameItem = ""
                           for item in items {
                            if let record0Item = item.value(forKey: "grade") as? String
                                  {
                                    print ("Record 0 Item is \(record0Item)")
                                    print( item.value(forKey: "grade")!)
                                gradeItem.append(record0Item)
                                    print ("Grade is \(gradeItem)")
                                if let record1Item = item.value(forKey: "groupName") as? String
                                {
                                print ("Record 1 Item is \(record1Item)")
                                    print( item.value(forKey: "groupName")!)
                                    groupNameItem.append(record1Item)
                                    print ("Group Name is \(groupNameItem)")
                                if let record2Item = item.value(forKey: "score") as? String
                                {
                                    print ("Record 2 Item is \(record2Item)")
                                    print( item.value(forKey: "score")!)
                                    groupScoreItem.append(record2Item)
                                    print ("Group Score is \(groupScoreItem)")
       
                                   if let record3Item = item.value(forKey: "date") as? String
                                {
                                    print ("What is going on here!!!")
                                    print( item.value(forKey: "date")!)
                                    if let record3Item = item.value(forKey: "date") as? String {
                                    dateItem.append(record3Item)
                                    print ("Record 3 Item is \(record3Item)")
                                      }
                                        dateItem.append(record3Item)
                                        print ("Record 3 Item is \(record3Item)")
   
                        let newLine =  ((dateItem) + ("  ") + ("Grade") + (" ") + (gradeItem) + ("  ") + (groupNameItem) + ("  ") + ("Group Score") + (" ") + (groupScoreItem))
                                    studentsText?.string = studentsText!.string + newLine + "\n"
                                    groupNameItem = ""
                                    groupScoreItem = ""
                                    gradeItem = ""
                                }
                            }
                        }
                    }
                }

Seems you have updated your post on Jan 29, 2020 4:13 PM,


but that does not answer my questions:

- To which lines in code does this print correspond to ?

- show more log so that we can understand what's going on.

- print from line 8, 9, 11, 14, … etc …

The log for tests from lines 7 - 35 is as follows:

CoreData: details: SQLite: EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDATE, t0.ZGRADE, t0.ZGROUPNAME, t0.ZNEGATIVESCORE, t0.ZSCORE FROM ZGROUPRECORD t0 WHERE t0.ZGROUPNAME = ?

2 0 0 SCAN TABLE ZGROUPRECORD AS t0

Record 0 Item is 12 // line 9 print statement

12 // line 10 print statement

Grade is 12 // line 12 print statement

Record 1 Item is SCORCENT // line 16 print statement

SCORCENT // line 9 print statement

Group Name is SCORCENT // line 18 print statement

Record 2 Item is 11550 // line 21 print statement

11550 // line 22 print statement

Group Score is 11550. // line 24 print statement

There are no results for print statements on lines 28, 29, 32, 35

Probably you have inserted a full URL.


Start it with h ttps:// instead of no space.

Hello Claude31:


I got the correct Results with the following:


func getFormattedDate(date: Date, format: String) -> String {
                let dateformat = DateFormatter()
                dateformat.dateFormat = format
                return dateformat.string(from: date)
        }


Then:



     let record3Item = item.value(forKey: "date") as? String
      let formatingDate = getFormattedDate(date: Date(), format: "dd-MMM-yyyy")
      print(formatingDate)

I just don't understand your explanation, sorry.


What was your problem in yesterday's post Jan 30, 2020 10:24 AM ?

You said:

There are no results for print statements on lines 28, 29, 32, 35


I do not see how the change would solve it.


Could you answer those questions:

- To which lines in code does this print correspond to ?

- show more log so that we can understand what's going on.

- print from line 8, 9, 11, 14, … etc …

Date Cannot be Printed. Why?
 
 
Q