xcode 使用 OCUnit 中的核心数据类的 Apple Mach-O 链接器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11312915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:46:10  来源:igfitidea点击:

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

iosxcodecore-dataocunit

提问by CGee

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:

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

#import <CoreData/CoreData.h>

What am I missing?

我错过了什么?

回答by Karoly S

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.

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

回答by Stavash

回答by Harish Mahajan

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.

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