0

I've been struggling with this all afternoon. I can do this in Obj-C in my sleep but I can't seem to grasp what I am suppose to do in Swift. I can get an array of entities back (StoredTopic) but I keep getting errors trying to access them.

Here's the fetch code:

func fetchCoreData(_ entityName:String) -> [NSManagedObject] {

    guard let appDelegate =
        UIApplication.shared.delegate as? AppDelegate else {
            return []
    }

    let managedContext =
        appDelegate.persistentContainer.viewContext

    let fetchRequest =
        NSFetchRequest<NSManagedObject>(entityName: entityName)

    do {

        let arrData = try managedContext.fetch(fetchRequest)


    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }

    return []
}

Here's the log of arrData

[<StoredTopic: 0x6040000833e0> (entity: StoredTopic; id: 0xd000000000040000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p1> ; data: <fault>), <StoredTopic: 0x604000085960> (entity: StoredTopic; id: 0xd000000000080000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p2> ; data: <fault>), <StoredTopic: 0x604000083020> (entity: StoredTopic; id: 0xd0000000000c0000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p3> ; data: <fault>), <StoredTopic: 0x604000088de0> (entity: StoredTopic; id: 0xd000000000100000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p4> ; data: <fault>), <StoredTopic: 0x60000008c580> (entity: StoredTopic; id: 0xd000000000140000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p5> ; data: {
createDt = "2017-11-02 19:28:00 +0000";
segObj = 0;
segTopic = 0;
title = Test;
topicID = "4A55582B-3084-4ECA-8F61-C1193A85B45B";
"topic_desc" = 1234567890;
updateDt = "2017-11-02 19:28:00 +0000";

So if I try to do something like:

arrData[0].topicID I get "Value of type 'NSManagedObject' has no member 'topicID'"

If I try to do like some other SO post suggested by adding this line:

var arrStoredTopic = [StoredTopic]()
arrStoredTopic = try managedContext.fetch(fetchRequest)

I get "Cannot assign value of type '[NSManagedObject]' to type '[StoredTopic]'"

I then tried to follow this tutorial, though it was a little outdated with some of the calls and changed my code to follow it:

func fetchCoreData(_ entityName:String) -> [NSManagedObject] {

    guard let appDelegate =
        UIApplication.shared.delegate as? AppDelegate else {
            return []
    }

    let managedContext = appDelegate.persistentContainer.viewContext
    var fetchRequest = NSFetchRequest<NSFetchRequestResult>()
    let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: managedContext)
    fetchRequest.entity = entityDescription

    do {

        //let arrData = try managedContext.fetch(fetchRequest)
        let results = try managedContext.fetch(fetchRequest)

        if results.count > 0 {

            let storedData = results[0] as! NSManagedObject

        }

    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }

    return []
}

Still not able to access the StoredTopic properties.

So how do I fetch managedObjects from Core Data so I can work with them? I'm obviously confused.

Thanks

1 Answer 1

1

Change

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entityName)

to

let fetchRequest = NSFetchRequest<StoredTopic>(entityName: entityName)

or

let fetchRequest = StoredTopic.fetchRequest()

In your original code you are saying the fetch request will fetch NSManagedObjects not StoredTopics and that is why you are having issues with accessing properties and assigning the results of the fetch request to a [StoredTopic].

Swift is a type safe language so it is very strict about things that Objective-C doesn't worry about (until it crashes at runtime).

Sign up to request clarification or add additional context in comments.

4 Comments

Oh God, I know, it's driving me nuts. Owe you a beer. :)
You'll get the hang of it in time, I started with Objective-C before there was such a thing as iPhones and managed to get it. It mostly comes down to making sure you tell the compiler exactly what things are.
So did I, I was building desktop apps for a publishing company, then the phone came out and made that transition. I don't have formal comp-sci training, Obj-c was really my first language so sometimes it's an uphill struggle...but thanks. I appreciate the help.
Glad to be of help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.