【问题标题】:Compiled error when trying to use global constants : "undefined symbols for architecture i386"尝试使用全局常量时出现编译错误:“架构 i386 的未定义符号”
【发布时间】:2014-09-12 02:26:05
【问题描述】:

我是 iOs 开发的初学者。我搜索了几个小时并尝试了很多东西,但仍然无法正常工作。我很孤独。

我有一个项目,其中包含 2 个故事板(iPhone + iPad)和 3 对文件 .h/.m(AppDelgate、MasterViewController、DetailViewController)。

我在我的 AppDelegate.m 中下载了我需要的所有内容,并希望将结果存储在我的 AppDelegate.h 中声明的全局常量中,如下所示:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//javascript & css files
extern NSData *const JQUERY_FILE;
extern NSData *const JQUERYUI_FILE;
extern NSData *const JQUERY_MOBILE_FILE;
extern NSData *const JQUERY_MOBILE_CSS_FILE;
extern NSData *const APPLICATION_FILE;
extern NSData *const FEED_FILE;
@end

在 AppDelegate.m 中:

#import "AppDelegate.h"
@implementation AppDelegate
  NSURL *url = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"];
  NSString *path = [NSString stringWithFormat:@"%@/Library/Application Support/jquery.js", NSHomeDirectory()];
  NSData *const JQUERY_FILE = [NSData dataWithContentsOfURL:url];
  [JQUERY_FILE writeToFile:path options:NSDataWritingAtomic error:&error];
 …

我在其他 2 个文件中导入 AppDelegate.h .m:MasterViewController.m 和 DetailViewController.m

我在尝试构建项目时遇到编译错误。

Ld /Users/admin/Library/Developer/Xcode/DerivedData/Test_JSON_V2-avjvupkzamqjszcpvufoeqrdbjin/Build/Products/Debug-iphonesimulator/Test_JSON_V2.app/Test_JSON_V2 normal i386
cd /Users/Karine/Projects/Test_JSON_V2/IOS-ManualJson
export IPHONEOS_DEPLOYMENT_TARGET=7.1
export        PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk -L/Users/admin/Library/Developer/Xcode/DerivedData/Test_JSON_V2-avjvupkzamqjszcpvufoeqrdbjin/Build/Products/Debug-iphonesimulator -F/Users/admin/Library/Developer/Xcode/DerivedData/Test_JSON_V2-avjvupkzamqjszcpvufoeqrdbjin/Build/Products/Debug-iphonesimulator -filelist /Users/admin/Library/Developer/Xcode/DerivedData/Test_JSON_V2-avjvupkzamqjszcpvufoeqrdbjin/Build/Intermediates/Test_JSON_V2.build/Debug-iphonesimulator/Test_JSON_V2.build/Objects-normal/i386/Test_JSON_V2.LinkFileList -Xlinker -objc_abi_version -Xlinker 2 -fobjc-arc -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=7.1 -framework CoreGraphics -framework UIKit -framework Foundation -Xlinker -dependency_info -Xlinker /Users/admin/Library/Developer/Xcode/DerivedData/Test_JSON_V2-avjvupkzamqjszcpvufoeqrdbjin/Build/Intermediates/Test_JSON_V2.build/Debug-iphonesimulator/Test_JSON_V2.build/Objects-normal/i386/Test_JSON_V2_dependency_info.dat -o /Users/admin/Library/Developer/Xcode/DerivedData/Test_JSON_V2-avjvupkzamqjszcpvufoeqrdbjin/Build/Products/Debug-iphonesimulator/Test_JSON_V2.app/Test_JSON_V2

  Undefined symbols for architecture i386:
 "_APPLICATION_FILE", referenced from:
     -[MasterViewController viewDidLoad] in MasterViewController.o
 "_JQUERYUI_FILE", referenced from:
  -[DetailViewController webViewDidFinishLoad:] in DetailViewController.o
 "_JQUERY_FILE", referenced from:
     -[DetailViewController webViewDidFinishLoad:] in DetailViewController.o
 "_JQUERY_MOBILE_CSS_FILE", referenced from:
     -[DetailViewController webViewDidFinishLoad:] in DetailViewController.o
 "_JQUERY_MOBILE_FILE", referenced from:
     -[DetailViewController webViewDidFinishLoad:] in DetailViewController.o
 ld: symbol(s) not found for architecture i386
 clang: error: linker command failed with exit code 1 (use -v to see invocation)

我想给你一个 printScreen 但我不能,因为我太初学者了:/

我检查了我的 3 m 文件是否在文件编辑器中的 Target Membership 和 Compile Sources 中(在 Build 阶段) 我尝试:

  • 创建一个包含所有常量的 .h 文件并通过执行编辑器 -> 添加构建阶段 -> 添加副本头构建阶段来添加它
  • 将 AppDelegate.h 添加到 Link Binary With Libraries 中
  • 在构建设置中添加新的用户标题搜索路径

当我使用并单击 MasterViewController 或 DetailViewController 中的全局常量时,右侧的帮助窗口显示它是在正确的文件中声明的。

请问您有什么想法吗? 提前致谢。


编辑 22/07/2014

我尝试在一个简单的应用程序中定义和使用全局常量。它适用于:

  • 标头中的声明位于@interface 块之外
  • m 文件中的赋值在 @implementation 块之外

但我不知道如何将我的代码添加到我的 AppDelegate.m 文件的 @implementation 之外。我将调查这一点。

【问题讨论】:

  • 您需要一个.m 文件,其中包含您在.h 文件中声明的所有变量的非外部 定义。通过使用 extern,您是说变量的定义可以在其他地方找到,但无论该定义在哪里,它都不会链接到应用程序
  • 你好@Petesh。我的常量在我的AppDelegate.m 中分配,这个文件正在添加到编译源中。如果我在 .m 文件中删除 NSData *const JQUERY_FILE = [NSData dataWithContentsOfURL:url]; by JQUERY_FILE = [NSData dataWithContentsOfURL:url]; 我有一个错误:只读变量不可分配。

标签: objective-c ios7 reference xcode5 constants


【解决方案1】:

如果您尝试从另一个 UIViewController 访问 AppDelegate 中的属性,这可能会有所帮助:

AppDelegate.h 中你会声明一个属性

@property (strong, nonatomic) NSData *someData;

在您的 AppDelagate.m

中生成数据
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

 [self generateData];
 //any other code
 }

-(void) generateData{
//Populate your property
_someData = [NSData dataWithContentsOfURL:url];
}

然后要从另一个 UIView 控制器访问数据,您可以这样做:

SomeViewController.m

NSData * localSomeData = [(AppDelegate *)[[UIApplication sharedApplication] delegate] someData];

【讨论】:

  • 感谢您的回复,但我更喜欢使用全局常量而不是使用方法。您的解决方案使我的问题误入歧途。我的目标是使用全局常量来减少我的代码。文件已下载并保存在设备上,但我不想每次需要时都搜索
【解决方案2】:

我用下面的代码解决了我的问题。在我的情况下,我不能使用常量,因为该值是在运行时设置的。我没有找到解决此问题的任何解决方案。所以我选择全局变量。

在 AppDelegate.h 中:

// GLOBAL VARIABLES
// javascript & css files
extern NSData *JQUERY_FILE;
extern NSData *JQUERYUI_FILE;
extern NSData *JQUERY_MOBILE_FILE;
extern NSData *JQUERY_MOBILE_CSS_FILE;
// json files
extern NSData *APPLICATION_FILE;
extern NSData *FEED_FILE;
// END GLOBAL VARIABLES

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

在 AppDelegate.m 中:

#import "AppDelegate.h"

NSData *JQUERY_FILE;
NSData *JQUERYUI_FILE;
NSData *JQUERY_MOBILE_FILE;
NSData *JQUERY_MOBILE_CSS_FILE;
NSData *APPLICATION_FILE;
NSData *FEED_FILE;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;
}

#pragma Download & save javascript & css files
NSLog(@"%@", NSHomeDirectory());

NSError *error = [[NSError alloc] init];

// Get the Bundle identifier for creating dynamic path for storing all files
NSString *bundle = [[NSBundle mainBundle] bundleIdentifier];
// NSHomeDirectory returns the application's sandbox directory
// FileManager is using for creating the Application Support Folder
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *appliSupportDir = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
[appliSupportDir URLByAppendingPathComponent:bundle isDirectory:YES];

// Get jquery files in network
NSURL *url = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"];
NSString *path = [NSString stringWithFormat:@"%@/Library/Application Support/jquery.js", NSHomeDirectory()];
//JQUERY_FILE = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
JQUERY_FILE = [NSData dataWithContentsOfURL:url];
[[NSData dataWithContentsOfURL:url] writeToFile:path options:NSDataWritingAtomic error:&error];

url = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"];
path = [NSString stringWithFormat:@"%@/Library/Application Support/jqueryUI.js", NSHomeDirectory()];
//JQUERYUI_FILE = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
JQUERYUI_FILE = [NSData dataWithContentsOfURL:url];
[[NSData dataWithContentsOfURL:url] writeToFile:path options:NSDataWritingAtomic error:&error];

url = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.js"];
path = [NSString stringWithFormat:@"%@/Library/Application Support/jqueryMobile.js", NSHomeDirectory()];
//JQUERY_MOBILE_FILE = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
JQUERY_MOBILE_FILE = [NSData dataWithContentsOfURL:url];
[[NSData dataWithContentsOfURL:url] writeToFile:path options:NSDataWritingAtomic error:&error];

url = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.css"];
path = [NSString stringWithFormat:@"%@/Library/Application Support/jqueryMobileCSS.css", NSHomeDirectory()];
//JQUERY_MOBILE_CSS_FILE = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
JQUERY_MOBILE_CSS_FILE = [NSData dataWithContentsOfURL:url];
[[NSData dataWithContentsOfURL:url] writeToFile:path options:NSDataWritingAtomic error:&error];


#pragma Download & save Json Files
// Read Json file in network. WARNING : ID EN DUR !!
// Get Application File
url = [NSURL URLWithString:@"http://testapp.visionit.lan:8087/api/application/79e45c6e-9e87-4576-b028-609ae2902f00"];
path = [NSString stringWithFormat:@"%@/Library/Application Support/myApplication.json", NSHomeDirectory()];
APPLICATION_FILE = [NSData dataWithContentsOfURL:url];
[APPLICATION_FILE writeToFile:path options:NSDataWritingAtomic error:&error];
// Get Feed File
url = [NSURL URLWithString:@"http://testapp.visionit.lan:8087/api/feed/79e45c6e-9e87-4576-b028-609ae2902f00"];
path = [NSString stringWithFormat:@"%@/Library/Application Support/myFeed.json", NSHomeDirectory()];
FEED_FILE = [NSData dataWithContentsOfURL:url];
[FEED_FILE writeToFile:path options:NSDataWritingAtomic error:&error];


return YES;
}

接下来,导入要使用全局变量的 AppDelegate.h。

它对我有用。 希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多