Core data ';任何对象';没有名为'的成员;联系人姓名';阻止我的循环
我正在寻找从函数中的coredata读取数据并在事后循环结果的最佳方法 我尝试了很多方法,但似乎在尝试获取每个对象中的特定数据片段时遇到了麻烦 这是我的设置Core data ';任何对象';没有名为'的成员;联系人姓名';阻止我的循环,core-data,swift,Core Data,Swift,我正在寻找从函数中的coredata读取数据并在事后循环结果的最佳方法 我尝试了很多方法,但似乎在尝试获取每个对象中的特定数据片段时遇到了麻烦 这是我的设置 func readData()->NSArray{ let entityName:String = "Properties" let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let co
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;
我得到以下错误
“'AnyObject'没有名为'contactName'的成员”
我确信我遗漏了一些简单的东西,但我在网上找不到
任何帮助或更好的方法都将不胜感激
回答。 首先,向核心数据对象添加名称空间。 其次,强类型化函数的输出,以便将数据类型化为自定义对象。在这种情况下,它被称为“属性” 试一试
func readData()->数组{
让entityName:String=“属性”
让appDel:AppDelegate=UIApplication.sharedApplication().delegate作为AppDelegate
let context:NSManagedObjectContext=appDel.managedObjectContext
//指定将使用的内容
let request=NSFetchRequest(entityName:entityName)
//这将返回所需对象的实例。如果没有该实例,则
//工作起来很痛苦。这一点节省了一些步骤。
request.returnsObjectsAsFaults=false;
//获取数据并将其放入变量中
var results=context.executeFetchRequest(请求,错误:nil)
以数组形式返回结果
}
var getInfo:Array=properties.readData()
println(getInfo)
对于getInfo中的每个信息{
println(信息联系人姓名)
}
我这里没有Mac电脑,但我很确定这样的东西可以用。这样您就拥有了强类型数据。找到了它
当将结果作为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)
}
即使我这样做了,在接收端,它仍然希望将其视为“任何对象”。导致了与此相关的问题。。getInfo{println(eachInfo.contactName)}中eachInfo:Array的var properties=properties()var getInfo=properties.readData(),这会导致EXC\u BAD\u指令错误。var properties=properties()var getInfo=properties.readData()用于getInfo{println(eachInfo.contactName)}中的每个hinfo。您的方法确实有效。我只需要确保在设置Coredata对象时添加了名称空间。我会把答案贴出来。
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)
}
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)
}
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)
}