Need your insight here...
This keeps crashing. Im calling a table on certain site, some say its the site that is causing but I can also make an exception. Can I get your two cents here?
NSRange blockStartRange = [pageContent rangeOfString:#"\t<table border=\"0\" cellpadding=\"4\" cellspacing=\"0\" id=\"table"];
NSRange blockEndRange = [pageContent rangeOfString:#"\t</table>" options:NSBackwardsSearch range:NSMakeRange(blockStartRange.location, [pageContent length] - blockStartRange.location)];
[HTMLString appendString:[pageContent substringWithRange:NSMakeRange(blockStartRange.location, blockEndRange.location + blockEndRange.length - blockStartRange.location)]];
// [HTMLString replaceOccurrencesOfString:#"width: 700px;" withString:#"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [HTMLString length])];
}
[webView loadHTMLString:HTMLString baseURL:[NSURL URLWithString:#"http://bowlatrabs.com/"]];
}
Error Log
2014-08-01 15:52:22.092 Bowl at Rabs[61401:90b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFString rangeOfString:options:range:locale:]: Range {2147483647, 2147542871} out of bounds; string length 59222'
*** First throw call stack:
(
0 CoreFoundation 0x0270b1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x020178e5 objc_exception_throw + 44
2 CoreFoundation 0x0270afbb +[NSException raise:format:] + 139
3 Foundation 0x000c38cd -[NSString rangeOfString:options:range:locale:] + 185
4 Foundation 0x000cf156 -[NSString rangeOfString:options:range:] + 69
5 Bowl at Rabs 0x0002d9e8 -[LeagueController viewDidLoad] + 920
6 UIKit 0x005dc33d -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x005dc5d9 -[UIViewController view] + 35
8 UIKit 0x005f6942 -[UINavigationController _startCustomTransition:] + 778
9 UIKit 0x006038f7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
10 UIKit 0x006044e9 -[UINavigationController __viewWillLayoutSubviews] + 57
11 UIKit 0x007450d1 -[UILayoutContainerView layoutSubviews] + 213
12 UIKit 0x0052c964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
13 libobjc.A.dylib 0x0202982b -[NSObject performSelector:withObject:] + 70
14 QuartzCore 0x04d9b45a -[CALayer layoutSublayers] + 148
15 QuartzCore 0x04d8f244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
16 QuartzCore 0x04d8f0b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
17 QuartzCore 0x04cf57fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
18 QuartzCore 0x04cf6b85 _ZN2CA11Transaction6commitEv + 393
19 QuartzCore 0x04cf7258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
20 CoreFoundation 0x026d336e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
21 CoreFoundation 0x026d32bf __CFRunLoopDoObservers + 399
22 CoreFoundation 0x026b1254 __CFRunLoopRun + 1076
23 CoreFoundation 0x026b09d3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x026b07eb CFRunLoopRunInMode + 123
25 GraphicsServices 0x035765ee GSEventRunModal + 192
26 GraphicsServices 0x0357642b GSEventRun + 104
27 UIKit 0x004bdf9b UIApplicationMain + 1225
28 Bowl at Rabs 0x0000239c main + 76
29 libdyld.dylib 0x0325d725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
You need to handle the case where the strings do not exist. Apple's documentation for the rangeOfString: method says: "Returns {NSNotFound, 0} if aString is not found or is empty (#"")."
So you need to check to see if the returned ranges have range.location == NSNotFound and handle that in whatever way is best.
Other way to check is using NSRangeFromString
NSRangeFromString: Returns a range from a textual representation.
If the string passed into NSRangeFromString does not represent a valid range, it will return a range with its location and length set to 0.
NSString *string = #"invalid";
NSRange range = NSRangeFromString(string);
// {.location=0, .length=0}
You might check nshipster/range
Related
Code:
NSString *Query=[NSString stringWithFormat:#"select *from peopleinfm where peopleinfmID=%d",TextEdit];
NSArray *Results=[[NSArray alloc]initWithArray:[DBManger loadDataFromDB:Query]];
self.FirstnameText.text=[[Results objectAtIndex:1]objectAtIndex:[self.DBManger.ColumnArray indexOfObject:#"firstname"]];
self.LastnameText.text=[[Results objectAtIndex:1]objectAtIndex:[self.DBManger.ColumnArray indexOfObject:#"lastname"]];
self.AgeText.text=[[Results objectAtIndex:1]objectAtIndex:[self.DBManger.ColumnArray indexOfObject:#"age"]];
Logs:
2016-02-12 19:03:36.068 SampleFMDB[9641:762577] no such table: peopleinfm
2016-02-12 19:03:36.077 SampleFMDB[9641:762577] _UIConstraintBasedLayoutLogUnsatisfiable is OFF
2016-02-12 19:03:37.934 SampleFMDB[9641:762577] no such table: peopleinfm
2016-02-12 19:03:37.978 SampleFMDB[9641:762577] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'
*** First throw call stack:
(
0 CoreFoundation 0x00b23a94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x005e2e02 objc_exception_throw + 50
2 CoreFoundation 0x00aca4a0 __CFArrayGetTypeID_block_invoke + 0
3 SampleFMDB 0x00010003 -[AddViewController loadInfoToEdit] + 291
4 SampleFMDB 0x0000f9bc -[AddViewController viewDidLoad] + 396
5 UIKit 0x01070d74 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44
6 UIKit 0x010758c2 -[UIViewController loadViewIfRequired] + 1556
7 UIKit 0x0107bf0e -[UIViewController __viewWillAppear:] + 114
8 UIKit 0x0109ecf0 -[UIViewController(UIContainerViewControllerProtectedMethods) beginAppearanceTransition:animated:] + 202
9 UIKit 0x010b0d63 -[UINavigationController _startCustomTransition:] + 1375
10 UIKit 0x010c1c0b -[UINavigationController _startDeferredTransitionIfNeeded:] + 801
11 UIKit 0x010c2d05 -[UINavigationController __viewWillLayoutSubviews] + 68
12 UIKit 0x0128cd9f -[UILayoutContainerView layoutSubviews] + 252
13 UIKit 0x00f7916b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
14 libobjc.A.dylib 0x005f7059 -[NSObject performSelector:withObject:] + 70
15 QuartzCore 0x048ae60c -[CALayer layoutSublayers] + 144
16 QuartzCore 0x048a228e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
17 QuartzCore 0x048a20f2 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
18 QuartzCore 0x04894c2b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
19 QuartzCore 0x048c8c23 _ZN2CA11Transaction6commitEv + 589
20 QuartzCore 0x048ca484 _ZN2CA11Transaction17flush_transactionEv + 50
21 UIKit 0x00eadb55 _UIApplicationHandleEventQueue + 7921
22 CoreFoundation 0x00a3de7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
23 CoreFoundation 0x00a33b0b __CFRunLoopDoSources0 + 523
24 CoreFoundation 0x00a32f28 __CFRunLoopRun + 1032
25 CoreFoundation 0x00a32866 CFRunLoopRunSpecific + 470
26 CoreFoundation 0x00a3267b CFRunLoopRunInMode + 123
27 GraphicsServices 0x04148664 GSEventRunModal + 192
28 GraphicsServices 0x041484a1 GSEventRun + 104
29 UIKit 0x00eb3cc1 UIApplicationMain + 160
30 SampleFMDB 0x000108ea main + 138
31 libdyld.dylib 0x030f6a21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
This is the error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 18446744073709551615 beyond bounds [0 .. 1]'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104b09f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000104edbdeb objc_exception_throw + 48
2 CoreFoundation 0x00000001049f8b14 -[__NSArrayI objectAtIndex:] + 164
3 MyApp 0x000000010234ae4b -[AppManager homePage] + 203
4 MyApp 0x000000010234a437 -[AppManager init] + 503
5 MyApp 0x000000010234a222 __28+[AppManager sharedInstance]_block_invoke + 50
6 libdispatch.dylib 0x00000001061da49b _dispatch_client_callout + 8
7 libdispatch.dylib 0x00000001061c5e28 dispatch_once_f + 543
8 MyApp 0x000000010234a1cb +[AppManager sharedInstance] + 107
9 MyApp 0x000000010238494b -[SplashViewController viewDidLoad] + 379
10 UIKit 0x0000000103652cc4 -[UIViewController loadViewIfRequired] + 1198
11 UIKit 0x00000001036970ee -[UINavigationController _layoutViewController:] + 54
12 UIKit 0x00000001036979c2 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 462
13 UIKit 0x0000000103697b34 -[UINavigationController _startTransition:fromViewController:toViewController:] + 126
14 UIKit 0x0000000103698d8d -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
15 UIKit 0x0000000103699cea -[UINavigationController __viewWillLayoutSubviews] + 57
16 UIKit 0x000000010383fc85 -[UILayoutContainerView layoutSubviews] + 248
17 UIKit 0x0000000103574e40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
18 QuartzCore 0x000000010324059a -[CALayer layoutSublayers] + 146
19 QuartzCore 0x0000000103234e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
20 QuartzCore 0x0000000103234cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
21 QuartzCore 0x0000000103229475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
22 QuartzCore 0x0000000103256c0a _ZN2CA11Transaction6commitEv + 486
23 QuartzCore 0x000000010325737c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24 CoreFoundation 0x0000000104a35947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
25 CoreFoundation 0x0000000104a358b7 __CFRunLoopDoObservers + 391
26 CoreFoundation 0x0000000104a2ae2c CFRunLoopRunSpecific + 524
27 UIKit 0x00000001034bb4f5 -[UIApplication _run] + 402
28 UIKit 0x00000001034c030d UIApplicationMain + 171
29 MyApp 0x000000010239b63f main + 111
30 libdyld.dylib 0x000000010620e92d start + 1
31 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I'm not accessing any object inside NSArray which leads to following error, however certainly my app giving me this error.
The error is in your homePage method, which you can see here:
2 CoreFoundation ... -[__NSArrayI objectAtIndex:] + 164
3 MyApp ... -[AppManager homePage] + 203 <----THIS CALLS objectAtIndex: ABOVE
According to the error message, it is trying to access the index 18446744073709551615. This is the value of NSNotFound, which means you are probably handling NSNotFound incorrectly!
Issue with -[AppManager homePage] method kindly check if there is any array accessed there
I really get tired of trying to fix this so I think I need some help. I am filling each Cells of my iOS from an API which I successfully parsed (Can retrieve simple things such as texts, or numbers with no problem)
But when I tried to set the images for the thumbnails I am having some errors.
This is my code:
NSString *imageUrl = [tempDictionary valueForKeyPath:#"files.url_thumb"];
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *thumbNailImage = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.ThumbImage setImage:thumbNailImage];
});
and this are the nightmares in my console:
2015-04-08 13:14:26.450 WebTableView[4976:173380] -[__NSArrayI length]:
unrecognized selector sent to instance 0x79e18b90
2015-04-08 13:14:26.509 WebTableView[4976:173380] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x79e18b90'
*** First throw call stack:
(
0 CoreFoundation 0x036c9466 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x03352a97 objc_exception_throw + 44
2 CoreFoundation 0x036d12c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x03619bc7 ___forwarding___ + 1047
4 CoreFoundation 0x0361978e _CF_forwarding_prep_0 + 14
5 CoreFoundation 0x035836cf CFStringGetLength + 143
6 CoreFoundation 0x036a9c8d _CFURLCreateWithURLString + 77
7 CoreFoundation 0x035952d3 CFURLCreateWithString + 35
8 Foundation 0x02f17999 -[NSURL(NSURL) initWithString:relativeToURL:] + 371
9 Foundation 0x02f17807 +[NSURL(NSURL) URLWithString:relativeToURL:] + 80
10 Foundation 0x02f177b1 +[NSURL(NSURL) URLWithString:] + 48
11 WebTableView 0x00111a8b -[TableViewController tableView:cellForRowAtIndexPath:] + 571
12 UIKit 0x01c26c9c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 473
13 UIKit 0x01c26d7e -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 77
14 UIKit 0x01c0054b -[UITableView _updateVisibleCellsNow:isRecursive:] + 3034
15 UIKit 0x01c1aeb1 -[UITableView layoutSubviews] + 222
16 UIKit 0x01b907b1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 608
17 libobjc.A.dylib 0x03368771 -[NSObject performSelector:withObject:] + 70
18 QuartzCore 0x009421cf -[CALayer layoutSublayers] + 152
19 QuartzCore 0x00936055 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 397
20 QuartzCore 0x00935eb0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
21 QuartzCore 0x008941b6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
22 QuartzCore 0x0089558a _ZN2CA11Transaction6commitEv + 392
23 QuartzCore 0x00895c56 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24 CoreFoundation 0x035ec18e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
25 CoreFoundation 0x035ec0d0 __CFRunLoopDoObservers + 400
26 CoreFoundation 0x035e1b0a __CFRunLoopRun + 1226
27 CoreFoundation 0x035e137b CFRunLoopRunSpecific + 443
28 CoreFoundation 0x035e11ab CFRunLoopRunInMode + 123
29 GraphicsServices 0x04b3d2c1 GSEventRunModal + 192
30 GraphicsServices 0x04b3d0fe GSEventRun + 104
31 UIKit 0x01b049b6 UIApplicationMain + 1526
32 WebTableView 0x000dd6cd main + 141
33 libdyld.dylib 0x03e94ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I think valueForKeyPath: returning an array of objects. Try logging imageUrl and check what you are getting.
If you are confident that there would be only one url then you can try this -
NSString *imageUrl = [[tempDictionary valueForKeyPath:#"files.url_thumb"] lastObject];
Else try something like this -
valueForKey:#"files"] valueForKey:#"url_thumb]
This line [self.label setFont:[MyUtilityClass fontWithSize:13.0f]]; of code is giving me the -[NSNull length]: crash. label as many of you guys can guess is a UILabel. The only logical explanation I can think of is UILabel text is null. I will post my utility class below just in case it helps.
MyUtilityClass
+ (UIFont *) fontWithSize : (CGFloat) size
{
UIFont *font = [UIFont fontWithName:#"Roboto-Regular" size:size];
return font;
}
edit: This is the error message I've received. I can copy and paste the raw data if you need me to. This is a crash a user received according to crashlytics.
Fatal Exception: NSInvalidArgumentException
-[NSNull length]: unrecognized selector sent to instance 0x378e23f0
This is the raw data of the crash.
Thread : Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x297c3c1f __exceptionPreprocess + 126
1 libobjc.A.dylib 0x36f97c8b objc_exception_throw + 38
2 CoreFoundation 0x297c9039 __methodDescriptionForSelector
3 CoreFoundation 0x297c6f57 ___forwarding___ + 714
4 CoreFoundation 0x296f8df8 _CF_forwarding_prep_0 + 24
5 UIKit 0x2cc8b1b5 -[UILabel _setFont:] + 100
6 MyApp 0x00137c29 -[MyViewController initUI] (MyViewController.m:90)
7 MyApp 0x0013765b -[MyViewController viewDidLoad] (MyViewController.m:51)
8 UIKit 0x2cc82f8f -[UIViewController loadViewIfRequired] + 602
9 UIKit 0x2cd2cd95 -[UINavigationController _layoutViewController:] + 32
10 UIKit 0x2cd2ccbd -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 228
11 UIKit 0x2cd2c253 -[UINavigationController _startTransition:fromViewController:toViewController:] + 74
12 UIKit 0x2cd2bf83 -[UINavigationController _startDeferredTransitionIfNeeded:] + 578
13 UIKit 0x2cd2bced -[UINavigationController __viewWillLayoutSubviews] + 44
14 UIKit 0x2cd2bc81 -[UILayoutContainerView layoutSubviews] + 184
15 UIKit 0x2cc804d7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514
16 QuartzCore 0x2c6a8a0d -[CALayer layoutSublayers] + 136
17 QuartzCore 0x2c6a43e5 CA::Layer::layout_if_needed(CA::Transaction*) + 360
18 QuartzCore 0x2c6a426d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
19 QuartzCore 0x2c6a3c51 CA::Context::commit_transaction(CA::Transaction*) + 224
20 QuartzCore 0x2c6a3a55 CA::Transaction::commit() + 324
21 UIKit 0x2cc78965 _afterCACommitHandler + 132
22 CoreFoundation 0x2978a3b5 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
23 CoreFoundation 0x29787a73 __CFRunLoopDoObservers + 278
24 CoreFoundation 0x29787e7b __CFRunLoopRun + 914
25 CoreFoundation 0x296d6211 CFRunLoopRunSpecific + 476
26 CoreFoundation 0x296d6023 CFRunLoopRunInMode + 106
27 GraphicsServices 0x30a8f0a9 GSEventRunModal + 136
28 UIKit 0x2cce21d1 UIApplicationMain + 1440
29 MyApp 0x000a07e7 main (main.m:16)
30 libdyld.dylib 0x37517aaf start + 2
It's very likely that you are trying to get the length of what you think is an NSString but is actually NSNull.
I got this issue with my code & resolved it by using system fonts. The case was it couldn't load the font which I had specified into the code. Try & let me know if it help.
Happy Coding, cheers,
Ratneshwar
I have an iPhone app which fetches user information (first name, last name, city) in JSON format from different social networks.
This works mostly well, but one of the social networks returns the city as a number instead of a string (actually I should made one more REST call to map this number to a city name... but for now I just want to display the number).
And when I try to display that number in a UILabel I get the exception (here fullscreen):
2014-02-15 11:24:16.194 MyAuth[8872:a0b] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb074f90
2014-02-15 11:24:16.203 MyAuth[8872:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb074f90'
*** First throw call stack:
(
0 CoreFoundation 0x01bb15e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x019348b6 objc_exception_throw + 44
2 CoreFoundation 0x01c4e903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01ba190b ___forwarding___ + 1019
4 CoreFoundation 0x01ba14ee _CF_forwarding_prep_0 + 14
5 Foundation 0x015798ed -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 39
6 Foundation 0x0157a55a -[NSConcreteMutableAttributedString initWithString:attributes:] + 293
7 UIKit 0x0084fbc6 -[UILabel _setText:] + 97
8 UIKit 0x0084fd84 -[UILabel setText:] + 40
9 MyAuth 0x0000a296 -[UserViewController viewDidLoad] + 678
10 UIKit 0x007b6318 -[UIViewController loadViewIfRequired] + 696
11 UIKit 0x007b65b4 -[UIViewController view] + 35
12 UIKit 0x007d03e2 -[UINavigationController _startCustomTransition:] + 778
13 UIKit 0x007dd0c7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
14 UIKit 0x007ddcb9 -[UINavigationController __viewWillLayoutSubviews] + 57
15 UIKit 0x00917181 -[UILayoutContainerView layoutSubviews] + 213
16 UIKit 0x0070d267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
17 libobjc.A.dylib 0x0194681f -[NSObject performSelector:withObject:] + 70
18 QuartzCore 0x054e12ea -[CALayer layoutSublayers] + 148
19 QuartzCore 0x054d50d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
20 QuartzCore 0x054d4f40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
21 QuartzCore 0x0543cae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
22 QuartzCore 0x0543de71 _ZN2CA11Transaction6commitEv + 393
23 QuartzCore 0x0543e544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24 CoreFoundation 0x01b794ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
25 CoreFoundation 0x01b7941f __CFRunLoopDoObservers + 399
26 CoreFoundation 0x01b57344 __CFRunLoopRun + 1076
27 CoreFoundation 0x01b56ac3 CFRunLoopRunSpecific + 467
28 CoreFoundation 0x01b568db CFRunLoopRunInMode + 123
29 GraphicsServices 0x031a09e2 GSEventRunModal + 192
30 GraphicsServices 0x031a0809 GSEventRun + 104
31 UIKit 0x006a2d3b UIApplicationMain + 1225
32 MyAuth 0x0000ab7d main + 141
33 libdyld.dylib 0x02518725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
So I go to my JSON parsing code:
- (User*)createUserFromJson:(id)json
{
if (![json isKindOfClass:[NSDictionary class]]) {
NSLog(#"Parsing response failed");
return nil;
}
NSDictionary *dict = json[#"response"][0];
User *user = [[User alloc] init];
user.key = kVK;
user.userId = dict[#"uid"];
user.firstName = dict[#"first_name"];
user.lastName = dict[#"last_name"];
user.city = dict[#"city"]; // THE PROBLEMATIC LINE
user.avatar = dict[#"photo_big"];
user.female = (2 == [dict[#"female"] intValue]);
return user;
}
and try to change it to:
user.city = [NSString stringWithFormat:#"%d", dict[#"city"]];
but then I get the compile-time warning (here fullscreen):
Format specifies type 'int' but the argument has type 'id'
So my question is how to solve this issue cleanly (w/o Xcode warnings) and robust (when fetched JSON data happens to be a string)?
The fastest solution is:
[NSString stringWithFormat:#"%#", dict[#"city"]];
which will take the description of the string or the number and convert that into a string.
In the future you may want to use:
if ([1dict[#"city"] isKindOfClass:[NSNumber class]]) { ...
to check what you have received and work with it specifically. i.e. to do your lookup and to not use stringWithFormat: when you actually already have a string (because it's inefficient).