我正在寻找在函数中从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!
}
我在这里称呼它。
var properties = Properties()
var getInfo = properties.readData()
println(getInfo)
for eachInfo:AnyObject in getInfo{
println(eachInfo)
}
那就是这样的东西......
<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;
})
这很棒。但是当我使用下面的代码访问其中的数据时....
for eachInfo:AnyObject in getInfo{
println(eachInfo.contactName)
}
我收到以下错误。
&#34;&#39; AnyObject&#39;没有名为&#39; contactName&#39;&#34;
的成员
我确信我遗漏了一些简单的东西,但我无法在网上找到它。
非常感谢任何协助甚至更好的方式。
回答。
首先,将名称空间添加到核心数据对象。 http://i.stack.imgur.com/qNNLo.png
其次,强烈键入函数的输出,以便将数据输入到自定义对象。在这种情况下,它被称为&#34;属性&#34;。
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>
}
最后,调用函数并循环数据。
var properties = Properties()
var getInfo = properties.readData()
for eachInfo in getInfo{
println(eachInfo.contactName)
}
答案 0 :(得分: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)
}
我这里没有Mac,但我很确定这样的事情。这样就可以获得强类型数据。
答案 1 :(得分:0)
找到它。
当将结果作为NSArray传回时,需要将其转换为NSManageObject类。
因此,在回报中正确调用和显示单条信息就像这样。
var properties = Properties()
var getInfo = properties.readData()
println(getInfo)
for eachInfo:AnyObject in getInfo{
// Cast AnyObject type into the equavalent NSManagedObject.
let info = eachInfo as Properties
println(info.contactName)
}