'AnyObject'没有名为'contactName'的成员阻止我的循环

Chr*_*ley 9 core-data swift

我正在寻找从函数中的coredata读取数据的最佳方法,并在事后循环结果.

我已经尝试了很多方法,但似乎想要了解每个对象中的特定数据片段.

这是我的设置.

    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!
}
Run Code Online (Sandbox Code Playgroud)

我在这里称它为..

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

    for eachInfo:AnyObject in getInfo{
        println(eachInfo)
    }
Run Code Online (Sandbox Code Playgroud)

那就是这样的东西......

<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;
Run Code Online (Sandbox Code Playgroud)

})

这很棒.但是当我使用下面的代码访问内部数据时....

for eachInfo:AnyObject in getInfo{
        println(eachInfo.contactName)
    }
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

"'AnyObject'没有名为'contactName'的成员"

我确信有一些简单的我想念但我无法在网上找到它.

任何协助甚至更好的方式将不胜感激.


回答.

首先,将名称空间添加到核心数据对象. http://i.stack.imgur.com/qNNLo.png

其次,强烈键入函数的输出,以便将数据输入到自定义对象.在这种情况下,它被称为"属性".

    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>


}
Run Code Online (Sandbox Code Playgroud)

最后,调用函数并循环数据.

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

    for eachInfo in getInfo{

        println(eachInfo.contactName)

    }
Run Code Online (Sandbox Code Playgroud)

Arn*_*rno 1

尝试

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)

}
Run Code Online (Sandbox Code Playgroud)

我这里没有 Mac,但我很确定这样的东西可以工作。这样你就拥有了强类型数据。