0
votes

The code generator for NSManagedObject subclasses in Xcode 7.2- isn't on pair with the code generation for Objective-C. (No accessor methods for to-many relationships.)

So I want to do this:

  • Generate NSManagedObject subclasses in Objective-C.
  • Create a Bridging header file.
  • Write Swift extensions for the generated Objective-C classes.
  • Use the extended objective-C classes in Swift.

I do the steps above, but I get a Value of type Person, has no member syHello error, when I try to call the code below.

Objective-C:

@interface Person (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSSet<Pet *> *pets;

@end

@interface Person (CoreDataGeneratedAccessors)

- (void)addPetsObject:(Pet *)value;
- (void)removePetsObject:(Pet *)value;
- (void)addPets:(NSSet<Pet *> *)values;
- (void)removePets:(NSSet<Pet *> *)values;

@end

Swift:

public extension Person {

    public func sayHello() {

        print("Hello! My name is \(self.name).")

    }

}

Swift:

let myPerson = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: myAppDelegate.managedObjectContext) as! Person
myPerson.sayHello()

What do I need to do for the compiler to pick upp my extension on my Person class, so that I can call sayHello on it?

1
That should work. Do you import both "Person.h" and "Pet.h" in the bridging header file? - Martin R
Yes! This is the content of my bridging header file. #import "Person+CoreDataProperties.h" #import "Person.h" #import "Pet+CoreDataProperties.h" #import "Pet.h" - weenzeel
Is the "Target Membership" checkbox selected for the Swift file with the extension? - Martin R
If I check under Target Membership in the file inspector, the checkbox for my target is not checked. But I can't check it either. What must I do to be able to check it? - weenzeel
That is strange. How did you add the file to the project? What is the file name? - Martin R

1 Answers

0
votes

As Martin R pointed out; my Swift file was not connected to my target. Replaced my Swift file with a new one, and it worked.