1

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)

ebby94
  • 3,131
  • 2
  • 23
  • 31

3 Answers3

0

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.
PHD
  • 586
  • 1
  • 4
  • 11
  • Doing the changes makes it compile, but I'm still getting the linker error for duplicate symbols. I've added the error in the question – ebby94 Mar 30 '17 at 05:04
0

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

Lepidopteron
  • 6,056
  • 5
  • 41
  • 53
  • I found the solution to the problem, posted an answer. Also, there weren't any duplicate files present. For the question `why do you import an extension of the class you want to extend?`, this was automatically generated by Xcode :) Although removing the import statement nor moving the extension code into the class directly didn't seem to help me with the issue. – ebby94 Mar 30 '17 at 05:47
  • Cool! Don't forget to mark your own answer as solution, so the thread can be closed :) – Lepidopteron Mar 30 '17 at 06:08
  • I can mark it as answered only after a day. Will do it tomorrow :) – ebby94 Mar 30 '17 at 06:15
0

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

Community
  • 1
  • 1
ebby94
  • 3,131
  • 2
  • 23
  • 31