Q:

Getting the specific value in loading data from Core Data?

I am fetching the entities from Core Data, but I can´t access the object values to assign a variable etc.
This is my code:
    @IBAction func loadItem(sender: UIButton) {

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

    let request = NSFetchRequest(entityName: "Words")
    request.returnsObjectsAsFaults = false

    do {
        let results:NSArray = try context.executeFetchRequest(request)

        for res in results{

            print(res)
            print(res.word)

        }

    } catch {
        print("Unresolved error")
        abort()
    }

}

print(res) works fine and give me the object in the console:
<NSManagedObject: 0x7ff0095355f0> (entity: Words; id: 0xd000000000040000 <x-coredata://0E645702-9493-4D1A-8D55-4482B7948054/Words/p1> ; data: {
image = <89504e47 0d0a1a0a 0000000d 49484452 00000a6c 000006ec 08020000 009fcbb9 fc000001 18694343 50494343 2050726f 6669>;
word = test;
})

But how can I access for instance the "word" value? It only returns this error:
"ViewController.swift:36:23: Value of type 'Element' (aka 'AnyObject') has no member 'word'"
I´m trying to cast "res" to be a NSManagedObject, but I can´t seem to make it work.
Any help?

A:

You have two options. Either use Key-Value Coding:
let results = try context.executeFetchRequest(request)
for res in results {
    print(res)
    print(res.valueForKey("word"))
}

Or (better) use "Xcode->Editor->Create NSManagedObject subclass ...".
In Xcode 7 this will add two files "Words.swift" and "Words+CoreDataProperties.swift" to your project.
The first file
"Words.swift" defined the Word class (and you can extend the class
definition e.g. to add custom methods).
The second file "Words+CoreDataProperties.swift"
contains property definitions for all your Core Data properties,
for example:
extension Words {

    @NSManaged var word: String?
    // ...
}

And now you can cast the objects from the fetch request to the Word type and  access the properties directly:
let results = try context.executeFetchRequest(request) as! [Words]
for res in results {
    print(res)
    print(res.word)
}

