0

I'm having trouble making a Swift NSManagedObject subclass conform to an Objective-C protocol.

I've created a model protocol in order to abstract somewhat my Core Data models from other components in my app which have no business knowing these things are Core Data Managed Objects...

So I have a protocol as follows:

@protocol UserProtocol <NSObject>

@property (nonatomic) NSSet *friends;

@end

And my managed object subclass in swift is:

class User: NSManagedObject, UserProtocol {

    @NSManaged var friends: Set<User>

}

Unfortunately I am being told User does not conform to UserProtocol. I can't find a way to see the swift interpretation of my Objective-C protocol, is there a way to do that? If I can see that I could work out why my swift class isn't matching the protocol.

enter image description here

4
  • Perhaps you need getters and setters? Also, is var Set not equivalent to NSMutableSet? Commented Jun 30, 2015 at 11:16
  • @NSManaged requires var. You've got a point though maybe Set resolves to NSMutableSet. So isn't there a way to get a Swift view on an Objective-C piece of code? Commented Jun 30, 2015 at 11:22
  • 1
    Which Xcode version are you using? In Swift 1.2 (Xcode 6.3.2) NSSet is mapped to Set<NSObject>, so you would have to define the property as @NSManaged var friends: Set<NSObject> to satisfy the protocol. Commented Jun 30, 2015 at 11:31
  • Thank you, that did fix it. I guess this will all be a lot nicer to work with in Swift 2.0 with Objective-C generics. Please answer the question and I can accept it. Commented Jun 30, 2015 at 12:03

1 Answer 1

1

In Swift 1.2 (Xcode 6.3.2) NSSet is mapped to Set<NSObject>, so you would have to define the property as

@NSManaged var friends: Set<NSObject>

to satisfy the protocol. In Swift 2 you can define the Objective-C protocol method using "lightweight generics".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.