Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'AnyObject' does not have a member named 'contactName' Blocking my loop

I am looking for the best way to read data from coredata in a function and loop over the results after the fact.

I have tried it a number of ways but seem to get hung up on trying to get to the specific pieces of data within each object.

Here is my setup.

    func readData()->NSArray{
    let entityName:String = "Properties"
    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext

    // Specify what this will be working with
    let request = NSFetchRequest(entityName: entityName)

    // This will return instance of the object needed. Without which this would be
    // a pain to work with. This bit saves a few steps.
    request.returnsObjectsAsFaults = false;

    // Get the data and put it into a variable
    var results = context.executeFetchRequest(request, error: nil)

    return results!
}

And I call it here..

var properties = Properties()
    var getInfo = properties.readData()
    println(getInfo)

    for eachInfo:AnyObject in getInfo{
        println(eachInfo)
    }

And that dumps out something like this...

<NSManagedObject: 0x7f87fa711d60> (entity: Properties; id: 0xd000000000040000 <x-coredata://F627AD12-3CEC-4117-8294-616ADEE068DC/Properties/p1> ; data: {
acreage = 0;
conditionId = 0;
contactBusinessName = nil;
contactEmail = nil;
contactName = Bob;
contactPhoneNumber = 456456456;
isFav = nil;
latitude = 0;
longitude = 0;
price = 0;
propertyCity = nil;
propertyId = nil;
propertyState = nil;
propertyStreetAddress = nil;
propertyType = 0;
propertyZip = nil;
squareFeet = 0;
year = 0;

})

And that is great. But when I go to access the data within with the below code....

for eachInfo:AnyObject in getInfo{
        println(eachInfo.contactName)
    }

I get the following error.

"'AnyObject' does not have a member named 'contactName'"

I am sure that there is something simple I am missing but I can't find it online.

Any assist or even a better way would be greatly appreciated.


Answered.

First, Add namespace to core-data objects. http://i.stack.imgur.com/qNNLo.png

Second, strongly type the output of the function so that the data is typed to the custom object. In this case it is called "Properties".

    func readData()->Array<Properties>{


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

    // Specify what this will be working with
    let request = NSFetchRequest(entityName: entityName)

    // This will return instance of the object needed. Without which this would be
    // a pain to work with. This bit saves a few steps.
    request.returnsObjectsAsFaults = false;

    // Get the data and put it into a variable
    var results = context.executeFetchRequest(request, error: nil)

    return results! as Array<Properties>


}

Lastly, call on the function and loop over the data.

       var properties = Properties()
    var getInfo = properties.readData()

    for eachInfo in getInfo{

        println(eachInfo.contactName)

    }
like image 528
Christopher Wade Cantley Avatar asked Sep 19 '14 17:09

Christopher Wade Cantley


1 Answers

Try

func readData()->Array<Properties>{
    let entityName:String = "Properties"
    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext

    // Specify what this will be working with
    let request = NSFetchRequest(entityName: entityName)

    // This will return instance of the object needed. Without which this would be
    // a pain to work with. This bit saves a few steps.
    request.returnsObjectsAsFaults = false;

   // Get the data and put it into a variable
   var results = context.executeFetchRequest(request, error: nil)

   return results! as Array<Properties>
}

var getInfo:Array<Properties> = properties.readData()
println(getInfo)

for eachInfo in getInfo{
    println(info.contactName)

}

I do not have a Mac here but I'm pretty sure somthing like this works. This way you have strongly typed data.

like image 200
Arno van Lieshout Avatar answered Nov 11 '22 15:11

Arno van Lieshout