将NSManaged Object转换为数组iOS Swift

时间:2015-06-16 09:47:04

标签: ios swift core-data

我正在使用JSQCoreDataKit(https://github.com/jessesquires/JSQCoreDataKit)从Coredata中获取数据

因此,当我获取时,它返回AnyObject

类型的result.objects

如何将其转换为对象数组,以便我可以访问id和name等属性。

internal static func GetCountryEntityList(entityname: String, predicate: String)->AnyObject{
    let model = CoreDataModel(name: module, bundle: NSBundle(identifier: bundle)!)

    let stack = CoreDataStack(model: model)

    let privateStack = CoreDataStack(model: model, storeType: NSInMemoryStoreType, options: nil, concurrencyType: .PrivateQueueConcurrencyType)

    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)

    let countryEntity = entity(name: entityname, context: stack.managedObjectContext)
    let request = FetchRequest<Country>(entity: countryEntity)
    if predicate != ""{
        let predicate = NSPredicate(format: predicate)
        request.predicate = predicate
    }
    request.sortDescriptors = [sortDescriptor]
    let result = fetch(request: request, inContext: stack.managedObjectContext)

    if !result.success {
        println("Error = \(result.error)")
    }

    return result.objects
}

这是使用JSQCoreData

从CoreData获取数据的代码
    var countryListNew = [Country]()
    countryListNew = CoreDataManager.GetCountryEntityList("Country", predicate: "") as! [Country]

以上是我的控制器中的代码

enter image description here

这就是结果。对象是

enter image description here

这就是countryListNew

请参阅我无法访问姓名或身份证件

enter image description here 国家级

0 个答案:

没有答案