Apple Mach-O Linker Error using Core Data classes in OCUnit

倖福魔咒の 提交于 2019-12-19 19:56:40

问题


OK, here's my code in my test class:

 - (NSManagedObjectContext*)managedObjectContextWithConcurrencyType:(NSManagedObjectContextConcurrencyType)concurrencyType {
    NSManagedObjectModel *mom = [NSManagedObjectModel mergedModelFromBundles:nil];
    STAssertNotNil(mom, @"Can not create MOM from main bundle");

    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
    STAssertNotNil(psc, @"Can not create persistent store coordinator");
    NSPersistentStore *store = [psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:0];
    STAssertNotNil(store, @"Can not create In-Memory persistent store");

    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:concurrencyType];
    moc.persistentStoreCoordinator = psc;

    return moc;
}

And here's the failure message when trying to use the method in a test method:

Undefined symbols for architecture i386:
  "_NSInMemoryStoreType", referenced from:
      -[CrosswordItemTests managedObjectContextWithConcurrencyType:] in CrosswordItemTests.o
  "_OBJC_CLASS_$_NSEntityDescription", referenced from:
      objc-class-ref in CrosswordItemTests.o
      "_OBJC_CLASS_$_NSManagedObjectContext", referenced from:
      objc-class-ref in CrosswordItemTests.o
  "_OBJC_CLASS_$_NSManagedObjectModel", referenced from:
      objc-class-ref in CrosswordItemTests.o
  "_OBJC_CLASS_$_NSPersistentStoreCoordinator", referenced from:
      objc-class-ref in CrosswordItemTests.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I thought I imported the needed classes like that in the .h file:

#import <CoreData/CoreData.h>

What am I missing?


回答1:


In my experience i386 errors tend to come from missing libraries. Its not enough to just #import them in your file, you must also add them to the project libraries. Go into the Project Target -> Build Phases -> Link Binary with Libraries and in your case add the libraries associated with Core Data.




回答2:


You probably haven't linked the library to the target.

Taken from: http://yannickloriot.com/wp-content/uploads/2012/03/Link-CoreData-Framework-To-The-Project.png




回答3:


Changing 'No Common Blocks' from Yes to No (under Targets->Build Settings->Apple LLVM - Code Generation) fixed the problem. This fixed my problem. Hope it will help to you.



来源:https://stackoverflow.com/questions/11312915/apple-mach-o-linker-error-using-core-data-classes-in-ocunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!