“根” AppDelegate 无法识别 plist 中的密钥?
我有一个简单的问题:为什么“originArray”返回(null)?我发现,如果我将所有代码放入 RootViewController 中,它就可以工作,但如果我将其放入 AppDelegate 中(因为它位于示例代码中,我不知道哪种方式更好?),它无法识别“ Root”键:
- (id)init {
self = [super init];
if (self){
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"origine.plist"];
origine = [[NSDictionary dictionaryWithContentsOfFile:finalPath]retain];
}
return self;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"origine data : %@", origine);
NSArray *origineArray = [origine objectForKey:@"Root"];
NSLog(@"origineArray data : %@", origineArray);
感谢您的帮助
i have a simple question : why does "origineArray" return (null) ? i found out that if i put all the code in the RootViewController it works, but if i put it in the AppDelegate (as it is in a sample code, i don't which way is better?) , it does not recognize the "Root" key :
- (id)init {
self = [super init];
if (self){
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"origine.plist"];
origine = [[NSDictionary dictionaryWithContentsOfFile:finalPath]retain];
}
return self;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"origine data : %@", origine);
NSArray *origineArray = [origine objectForKey:@"Root"];
NSLog(@"origineArray data : %@", origineArray);
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的应用程序委托是在 nib 文件中创建的,则不会调用
init
方法。笔尖中的项目已存档并已初始化。如果您想要初始化从 NIB 加载的对象,请实现
- (void) awakeFromNib
方法。If your application delegate is created in a nib file, the
init
method is not called. Items in nibs are archived already initialized.If you want initialization of an object loaded from a NIB, implement the
- (void) awakeFromNib
method.您正在记录“原始”字典,它会记录到控制台什么?如果“Root”键不存在,那么显然您没有打开正确的文件。如果字典为 NULL,则需要修复路径。
您确定 finalPath 包含正确的路径吗?似乎 origin.plist 位于资源文件夹中,但您正在查找器中使用应用程序包的位置。
You are logging the "origine" dictionary, what does it log to the console? If the "Root" key is not there then you are not opening the right file obviously. If the dictionary is NULL then you need to fix the path.
Are you sure that finalPath contains the right path? It seems that the origin.plist is in the resources folder but you are using the location of your app bundle in the finder.