我正在尝试从plist文件中的特定key
读取数据!但我不知道为什么应用程序崩溃!这是我的代码:
NSString *loadData = [[NSBundle mainBundle]pathForResource:@"Dinosaurs" ofType:@"plist"];
NSDictionary *dinoDictionary = [[NSDictionary alloc]initWithContentsOfFile:loadData];
NSDictionary *dinosaurs = (NSDictionary*)[[dinoDictionary allKeys]objectAtIndex:0];
//app crashes at this line
NSString *DINOIMAGE = (NSString*)[dinosaurs valueForKey:@"DINOIMAGE"];
NSLog(@"%@ , %@",dinosaurs , DINOIMAGE);
崩溃日志:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7ff7faf0f790> valueForUndefinedKey:]: this class is not key value coding-compliant for the key DINOIMAGE.'
*** First throw call stack:
答案 0 :(得分:1)
你正在抓取02-24 20:35:10.645 15999-15999/? I/art: Late-enabling -Xcheck:jni
02-24 20:35:10.683 15999-15999/com.davidcleary.numberguesser W/ResourceType: Found multiple library tables, ignoring...
02-24 20:35:10.784 15999-16028/com.davidcleary.numberguesser D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-24 20:35:10.790 15999-15999/com.davidcleary.numberguesser D/Atlas: Validating map...
02-24 20:35:10.849 15999-16028/com.davidcleary.numberguesser I/Adreno- EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.01.00.042.030_msm8974_LA.BF.1.1.1_RB1__ release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.06
Build Date: 07/13/15 Mon
Local Branch: mybranch11906725
Remote Branch: quic/LA.BF.1.1.1_rb1.26
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.01.00.042.030 + 6151be1 + a1e0343 + 002d7d6 + 7d0e3f7 + NOTHING
02-24 20:35:10.850 15999-16028/com.davidcleary.numberguesser I/OpenGLRenderer: Initialized EGL, version 1.4
02-24 20:35:10.866 15999-16028/com.davidcleary.numberguesser D/OpenGLRenderer: Enabling debug mode 0
02-24 20:35:10.937 15999-15999/com.davidcleary.numberguesser I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@23edbb88 time:9305943
02-24 20:35:14.498 15999-15999/com.davidcleary.numberguesser D/AndroidRuntime: Shutting down VM
02-24 20:35:14.501 15999-15999/com.davidcleary.numberguesser E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.davidcleary.numberguesser, PID: 15999
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClic k(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at java.lang.Integer.<init>(Integer.java:102)
at com.davidcleary.numberguesser.MainActivity.randomGenerator(MainActivity.java:22)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
中的第一个密钥并将其用作字典(它是一个NSString),如下所示:
dinoDictionary
这就是错误消息所说的内容:无法查询密钥NSDictionary *dinosaurs = (NSDictionary*)[[dinoDictionary allKeys]objectAtIndex:0];
//app crashes at this line
NSString *DINOIMAGE = (NSString*)[dinosaurs valueForKey:@"DINOIMAGE"];
的NSString类。
您只需使用密钥DINOIMAGE
查询dinoDictionary
,如下所示:
DINOIMAGE
答案 1 :(得分:1)
您隐瞒了所请求的词典是一个带有键SCELIDOSAURUS
NSString *loadData = [[NSBundle mainBundle]pathForResource:@"Dinosaurs" ofType:@"plist"];
NSDictionary *dinoDictionary = [[NSDictionary alloc] initWithContentsOfFile: loadData];
NSDictionary *scelidosaurus = (NSDictionary *)dinoDictionary[@"SCELIDOSAURUS"]
NSString *DINOIMAGE = (NSString *)scelidosaurus[@"DINOIMAGE"];
NSLog(@"%@, %@", dinoDictionary, DINOIMAGE);