Search code examples
iosxcodeswiftnsmanagedobject

Value of type 'Self.ManageableType' has no member 'uid'


This is my code, which builds in XCode 7.2.1. When I try to build the project in XCode 7.3 beta 2, I got the error "Value of type 'Self.ManageableType' has no member '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)
  }
}

When I try to change loop code block into:

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

I got the error "Ambiguous use of 'valueForKey'"

Why these errors happen here in new XCode version, help please?


Solution

  • Your protocol extension needs a type constraint

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