1
votes

I have two header of a class and it's extension(generated by Xcode for NSManaged object). But I'm getting a linker error and I figured out it was due to a circular reference.

Conversation+CoreDataClass.h

NS_ASSUME_NONNULL_BEGIN

@interface Conversation : NSManagedObject

@end

NS_ASSUME_NONNULL_END

#import "Conversation+CoreDataProperties.h"

Conversation+CoreDataProperties.h

#import "Conversation+CoreDataClass.h"

NS_ASSUME_NONNULL_BEGIN

@interface Conversation (CoreDataProperties)

+ (NSFetchRequest<Conversation *> *)fetchRequest;

@end

NS_ASSUME_NONNULL_END

You can clearly see the circular reference here. I found this question where the problem was to add a @class declaration and remove the header. So I commented out the import statement in the Conversation+CoreDataProperties.h and added @class Conversation;. Now two errors pop up saying it's an undefined class. Have attached the screenshot of the error below. I don't quite understand why this is happening and what I need to do to fix it. Any help is much appreciated. Thanks!

enter image description here

Linker error

duplicate symbol _OBJC_CLASS_$_Conversation in: /Users/xxx/Library/Developer/Xcode/DerivedData/xxx-gbhivuwptwzhkldfbmjghkokozgn/Build/Intermediates/xxx.build/Debug-iphoneos/xxx.build/Objects-normal/arm64/Conversation+CoreDataClass.o duplicate symbol _OBJC_METACLASS_$_Conversation in: /Users/xxx/Library/Developer/Xcode/DerivedData/xxx-gbhivuwptwzhkldfbmjghkokozgn/Build/Intermediates/xxx.build/Debug-iphoneos/xxx.build/Objects-normal/arm64/Conversation+CoreDataClass.o duplicate symbol _OBJC_CLASS_$_ConversationDate in: /Users/xxx/Library/Developer/Xcode/DerivedData/xxx-gbhivuwptwzhkldfbmjghkokozgn/Build/Intermediates/xxx.build/Debug-iphoneos/xxx.build/Objects-normal/arm64/ConversationDate+CoreDataClass.o duplicate symbol _OBJC_METACLASS_$_ConversationDate in: /Users/xxx/Library/Developer/Xcode/DerivedData/xxx-gbhivuwptwzhkldfbmjghkokozgn/Build/Intermediates/xxx.build/Debug-iphoneos/xxx.build/Objects-normal/arm64/ConversationDate+CoreDataClass.o ld: 4 duplicate symbols for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

3
That's a bit strange. Try cleaning your project (shift-cmd-K) and rebuild. - dirkgroten
@dirkgroten That didn't help. Still have the errors. - ebby94

3 Answers

0
votes

So I commented out the import statement in the Conversation+CoreDataProperties.h and added @class Conversation;.

You may only do this if you're aren't using any of the Convention class's interface (for instance declaring a property of type Convention. You can't do this if you're extending Convention via a category or class extension.

You have a legit circular reference here you must resolve. You can either:

  1. Move the class and the category into the same source file (if there's no special reason this needs to be in a category you could just move the declaration of fetchRequest into the class itself).
  2. Stop importing +CoreDataProperties.h in the class header, and import it instead wherever callers need to be calling fetchRequest.
0
votes

the duplicate symbols warning do result because

  1. You have the class multiple times in your project and the compiler does not know, which to choose (then it is mostly a duplicate linking error)
  2. There is something wrong with your ConversationDate+CoreDataClass

Just a question that raised from your source code: why do you import an extension of the class you want to extend?

#import "Conversation+CoreDataClass.h"
@interface Conversation (CoreDataProperties)

+ (NSFetchRequest<Conversation *> *)fetchRequest;

@end

All the extension class usually needs to know are basically the classes they extend:

#import "Conversation.h"
@interface Conversation (CoreDataProperties)

+ (NSFetchRequest<Conversation *> *)fetchRequest;

@end

Only the implementation file (.m) would have to import the header file. E.g:

#import "Conversation+CoreDataClass.h"
@implementation Conversation (CoreDataProperties)

+ (NSFetchRequest<Conversation *> *)fetchRequest { 
    // body
    return nil;
}

@end

And last question: Are you sure there exists no other class extension with the same name in your project? CoreData might also have created them itself.

Please have a look in your xCode Project like this: Use lookup functionality on the bottom left of xCode

0
votes

Found the solution to my problem in this answer. I just had to remove the .m files from the Compile Sources.