5

好的,这是我的测试类中的代码:

 - (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;
}

这是尝试在测试方法中使用该方法时的失败消息:

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)

我以为我在 .h 文件中导入了所需的类:

#import <CoreData/CoreData.h>

我错过了什么?

4

3 回答 3

15

根据我的经验,i386 错误往往来自缺少库。仅将它们#import 到您的文件中是不够的,您还必须将它们添加到项目库中。进入 Project Target -> Build Phases -> Link Binary with Libraries 并在您的情况下添加与 Core Data 关联的库。

于 2012-07-03T14:38:54.123 回答
8

您可能尚未将库链接到目标。

在此处输入图像描述

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

于 2012-07-03T14:24:22.567 回答
0

将“No Common Blocks”从 Yes 更改为 No(在 Targets->Build Settings->Apple LLVM - Code Generation 下)解决了这个问题。这解决了我的问题。希望对您有所帮助。

于 2017-07-11T07:26:53.647 回答