0

I am trying to access a NSDate variable stored within coredata as AnyObject and print the day. When I print it, it returns nil. When I print the Date without .day after it it prints the correct value. Edit: workoutDate is an attribute of the entity 'Workout' and is of type 'Date'

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context: NSManagedObjectContext = appDel.managedObjectContext

        do {
            let request = NSFetchRequest(entityName: "Workout")
            let results = try context.executeFetchRequest(request)

            if results.count > 0 {
                for item in results as! [Workout]{


                    let time = item.valueForKey("workoutTime")
                    let currentWorkoutDate = item.valueForKey("workoutDate")
                    let workoutDateDay = currentWorkoutDate!
                    print(workoutDateDay)
                    print(workoutDateDay.day!)
Ryan Hampton
  • 319
  • 1
  • 4
  • 21

2 Answers2

2

NSDate has no day property. However, the Key-Value coding method

let currentWorkoutDate = item.valueForKey("workoutDate")

returns an AnyObject? and you can call any (existing) Objective-C method on that object (compare The strange behaviour of Swift's AnyObject). The compiler does not complain because day is a property of NSDateComponents. But at runtime, it is detected that the object does not respond to this property, and therefore workoutDateDay.day is nil.

The solution is to convert the value to an NSDate object:

if let workoutDate = item.valueForKey("workoutDate") as? NSDate {
    // ...
}

or better, create a NSManagedObject subclass for the entity and use the property accessors.

Then use the NSCalender methods to get NSDateComponents from the date, for example

if let workoutDate = item.valueForKey("workoutDate") as? NSDate {
    let day = NSCalendar.currentCalendar().component(.Day, fromDate: workoutDate)
    print(day)
    // ...
}
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thank you for your answer. This clears up the problem! – Ryan Hampton Aug 14 '16 at 14:12
  • Im getting the error 'Type Int has no member Day' when I try to print day – Ryan Hampton Aug 14 '16 at 14:35
  • @RyanHampton: did you copy the code exactly? It works for me. The `component(.Day, fromDate: workoutDate)` method returns the day as an Int, you don't have to access `.day` on the return value. Alternatively, use `components(.Day, fromDate: workoutDate)` – Martin R Aug 14 '16 at 14:42
  • I get an error of 'use of unresolved identifier workoutDate' on the line let day = NSCalendar.currentCalendar().component(.Day, fromDate: workoutDate) – Ryan Hampton Aug 14 '16 at 14:45
1

CoreData returns NSDate which has no method day. So, your problem is in that method if you have added it.

Sverrisson
  • 17,970
  • 5
  • 66
  • 62
  • If this is the case then why does it pop up as an option when I add a full stop (period) after typing in workoutDate? – Ryan Hampton Aug 14 '16 at 14:10
  • @RyanHampton because it's anytype, but you know it will always be NSDate so just: let currentWorkoutDate = item.valueForKey("workoutDate") as! NSDate – Sverrisson Aug 14 '16 at 14:13
  • 2
    @RyanHampton If you hold down that ALT key and click on the variable, then Xcode will tell you what type it is. Then you can always find out if the type you expected is being interpreted. – Sverrisson Aug 14 '16 at 14:20
  • Ah thats useful. Thank you! – Ryan Hampton Aug 14 '16 at 14:27