“Self.ManageableType”类型的值没有成员“uid”

时间:2016-02-07 17:53:08

标签: ios xcode swift nsmanagedobject

这是我的代码,它构建在XCode 7.2.1中。 当我尝试在XCode 7.3 beta 2中构建项目时,我收到错误“类型的值'Self.ManageableType'没有成员'uid'”

protocol Manageable {
  typealias ManageableType : NSManagedObject
  var uid: String { get set }
}

extension Manageable {

  static func className() -> String {
    return String(self)
  }

  static func fetchObjects(predicate: NSPredicate?,
    completion:(fetchedObjects: [String: ManageableType]) -> ()) {
      let entityDescription = NSEntityDescription.entityForName(className(),
        inManagedObjectContext: CoreDataStack.sharedInstance.context)
      let fetchRequest = NSFetchRequest()
      fetchRequest.entity = entityDescription
      if let p = predicate {
        fetchRequest.predicate = p
      }
      var fetchedObjectsDict: [String: ManageableType] = [:]
      do {
        let result = try CoreDataStack.sharedInstance.context.executeFetchRequest(fetchRequest) as! [ManageableType]
        if result.count > 0 {
          for object in result {
            fetchedObjectsDict[object.uid] = object
          }
        }
      } catch {
        print("ERROR FETCH MANAGEABLE OBJECTS: \(error)")
      }
      completion(fetchedObjects: fetchedObjectsDict)
  }
}

当我尝试将循环代码块更改为:

for object in result {
    let uid = object.valueForKey("uid") as! String
    fetchedObjectsDict[uid] = object
}

我收到错误“不明确使用'valueForKey'”

为什么在新的XCode版本中会出现这些错误,请帮助吗?

1 个答案:

答案 0 :(得分:1)

您的协议扩展需要类型约束

extension Manageable where Self : NSManagedObject, ManageableType == Self { ... }