使用未解析的标识符

时间:2016-03-16 08:48:19

标签: swift xcode7

伙计们,请你帮忙解决这个错误。我正在与<class> <employee type="Senior Software Developer"> <empid>A433568</empid> <empname>John Mathews</empname> <address>6th Avenue Street</address> </employee> <employee type="Junior Software Developer"> <empid>A433678</empid> <empname>Sunny Mathews</empname> <address>5th Avenue Street</address> </employee> <employee type="Trainee"> <empid>A434567</empid> <empname>Brad Hodge</empname> <address>4th Avenue Street</address> </employee> <employee type="Senior Software Developer"> <empid>A433599</empid> <empname>Glenn Powell</empname> <address>6th Avenue Street</address> </employee> <employee type="Senior Software Developer"> <empid>A433588</empid> <empname>Recordo Mathews</empname> <address>6th Avenue Street</address> </employee> </class> 合作。我在这段代码中有错误。具体在代码的最后部分(返回项)。错误描述告诉使用未解析的标识符&#39;项目&#39;

CoreData

错误的具体代码是

退货

2 个答案:

答案 0 :(得分:1)

您在do {}块中初始化了项目,因此函数中的项目不知道什么是项目变量,因此您应该在函数的开头初始化项目变量,以便: -

class func fetchEntities(className:NSString, withPredicate predicate:NSPredicate?, managedObjectContext:NSManagedObjectContext)->NSArray{
let items: NSArray!
let fetchRequest:NSFetchRequest = NSFetchRequest()
let entetyDescription:NSEntityDescription = NSEntityDescription.entityForName(className as! String, inManagedObjectContext: managedObjectContext)!

fetchRequest.entity = entetyDescription

if predicate != nil{
    fetchRequest.predicate = predicate!
}

fetchRequest.returnsObjectsAsFaults = false


do{
   items = try managedObjectContext.executeFetchRequest(fetchRequest)

} catch {

    print("error")

}
return items
}

答案 1 :(得分:0)

代码

do{
   let items = try managedObjectContext.executeFetchRequest(fetchRequest)

}

创建一个新范围。在该范围之外items无法访问。