我是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];
…
我在另外两个文件中导入AppDelegate.h .m:MasterViewController.m&amp; 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文件是否在文件编辑器中的目标成员资格中,以及在编译源中(在构建阶段) 我试着:
当我在MasterViewController或DetailViewController中使用并单击全局常量时,右侧的帮助窗口显示它在右侧文件中声明。
你有什么想法吗? 提前谢谢。
编辑22/07/2014
我尝试在一个简单的应用程序中定义&amp;使用全局常量。它适用于:
但我不知道如何在我的AppDelegate.m文件的@implementation之外添加我的代码。我将调查这一点。
答案 0 :(得分:0)
如果您尝试从另一个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];
答案 1 :(得分:0)
我用以下代码解决了我的问题。我不能在我的情况下使用常量,因为该值是在运行时设置的。我没有找到解决这个问题的任何解决方案。所以我选择了全局变量。
在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。
它对我有用。 希望它有所帮助