How to fetch date

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"

let fetchRequest: NSFetchRequest = GroupRecord.fetchRequest()
                fetchRequest.predicate = NSPredicate(format: "groupName == %@ ", groupNameItem)
                  do {
                      let items = try managedObjectContext.fetch(fetchRequest)

                    for item in items {
                      if let dateItem = (item.value(forKey: "date"))
                                    {
                                        print(dateItem)
                              }
                    }
          }


I have a date attribute with dates. Record was saved, but I cannot get that date displayed in a textView. All I am getting is the current system date.

Any help will be appreciated.

Replies

Where do you put in the textView ? You don't show in code.


What result of print do you get ?

I don't know why but it says reply is currently being moderated I there any way you can see the reply?

Did you post a url (in the request message for instance) ? If so, edit the header as h ttps with a space.

let firstPart = (item.value(forKey: "date")!)

print ("First part is \(firstPart)")

let secondPart = ("Grade") + (" ") + (gradeItem)

let thirdPart = (groupNameItem) + (" ") + (groupScoreItem)

let fourthPart = (formatingDate) + (" ") + ("Group Score So Far is") + (" ") + (totalScore) + "\n"


let newLine = secondPart + " " + thirdPart + " " + fourthPart + " " + fifthPart // Display in TextView

studentsText?.string = studentsText!.string + newLine + "\n" + "\n" // TextView output

So, please tell what you get from print:


  let firstPart = (item.value(forKey: "date")!)

  print ("First part is \(firstPart)")          // WHAT DO YOU GET HERE ?

  let secondPart = ("Grade") + (" ") + (gradeItem)
  let thirdPart = (groupNameItem)  + (" ") + (groupScoreItem)
  let fourthPart = (formatingDate) + (" ") + ("Group Score So Far is") + ("  ") + (totalScore) + "\n"
 
// THERE IS NO firstPart HERE. INTENTIONAL ?
let newLine =  secondPart + "  " + thirdPart + " " + fourthPart + "  " + fifthPart // Display in TextView
studentsText?.string = studentsText!.string  +  newLine + "\n" + "\n" // TextView output

First part is 2020-02-13 // first record

First part is 2020-02-14 // Second Record


Intentionally not included to get code to run because causes crash when included