2013-06-28 23 views
0

時,我有以下代碼:IOS:NSNull LLDB錯誤使用substringToIndex

  NSString *subtitleFull = [dictionary valueForKey:@"distance"];     
      NSString *subtitle = [subtitleFull substringToIndex:2]; 

當我運行代碼,我得到了以下錯誤:

  -[NSNull substringToIndex:]: unrecognized selector sent to instance 0x3a61a080 

我檢查,如果字典返回的NSString它與:

   id idont = [dictionary valueForKey:@"distance"]; 

       if ([idont isKindOfClass:[NSString class]]){ 
        NSLog(@"*****************YEA THIS IS IT %@", idont); 
       } 

該字符串看起來像這樣:「12.0093778471967」它變成鍵入NSString。也許我需要使用int值方法?

爲什麼我得到這個錯誤的任何原因?

回答

0

錯誤代碼非常簡單,subtitleFull爲空。因爲通常如果一個關鍵不存在,返回應該是零。所以在這種情況下,我猜爲什麼字典中的值是null是因爲dictionary來自JSON字符串或XML。只需在使用之前檢查該值是否爲空。

+0

好的,謝謝我會試試這個。 – wwjdm