Adding null object to SQLite - ios

I implemented SQLite in xcode. I have 2 columns. When the first column has a row, then the second one is set to null, and vice versa. I'm trying to go through both columns and check if it has an object at that index.
NSLog(#"%# and %#", firstArray, secondArray); // It gives the right risults, but there aren't any null objects in any of them.
for (int i = 0; i < [firstArray count]; i++)
{
if (![[firstArray objectAtIndex:i] length] && [[secondArray objectAtIndex:i] length]) {
...
}
}
I have two issues.
First: There aren't any null objects in the rows, even though I set some to null. So what should I do to check if there is a null object, or if there's another way to add some kind null object that will actually add to the tables row?
Second: When I compile and run the app, it crashes with the error below. But when I remove the if statement, it works flawlessly.
-[__NSArrayM length]: unrecognized selector sent to instance 0x7fbfeb53b0a0
2015-03-30 11:25:04.731 MyApp [610:15600] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x7fbfeb53b0a0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000110874f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000011050dbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000011087c04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001107d427c ___forwarding___ + 988
4 CoreFoundation 0x00000001107d3e18 _CF_forwarding_prep_0 + 120
5 MyApp 0x000000010fec3b59 -[ViewController ingredientAction] + 889
6 MyApp 0x000000010feba253 -[ViewController menuAction:] + 195
7 UIKit 0x0000000110ea58be -[UIApplication sendAction:to:from:forEvent:] + 75
8 UIKit 0x0000000110fac410 -[UIControl _sendActionsForEvents:withEvent:] + 467
9 UIKit 0x00000001110269ea -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 570
10 UIKit 0x0000000111028a0f -[UISegmentedControl touchesEnded:withEvent:] + 143
11 UIKit 0x0000000111252540 _UIGestureRecognizerUpdate + 9487
12 UIKit 0x0000000110eeaff6 -[UIWindow _sendGesturesForEvent:] + 1041
13 UIKit 0x0000000110eebc23 -[UIWindow sendEvent:] + 667
14 UIKit 0x0000000110eb89b1 -[UIApplication sendEvent:] + 246
15 UIKit 0x0000000110ec5a7d _UIApplicationHandleEventFromQueueEvent + 17370
16 UIKit 0x0000000110ea1103 _UIApplicationHandleEventQueue + 1961
17 CoreFoundation 0x00000001107aa551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 CoreFoundation 0x00000001107a041d __CFRunLoopDoSources0 + 269
19 CoreFoundation 0x000000011079fa54 __CFRunLoopRun + 868
20 CoreFoundation 0x000000011079f486 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x0000000114c759f0 GSEventRunModal + 161
22 UIKit 0x0000000110ea4420 UIApplicationMain + 1282
23 MyApp 0x000000010fec11b3 main + 115
24 libdyld.dylib 0x00000001122ad145 start + 1
25 ??? 0x0000000000000001 0x0 + 1
Edit
I put #"" instead of null into the row. Then I change the if statement (in the for loop) to the following:
NSStirng *objectAtIndexFromArray = [NSString stringWithFormat:#"%#", [firstArray objectAtIndex:i]];
if ([objectAtIndexFromArray isEqualToString:#""])
{
NSLog(#"Hello");
}
I don't get any NSLogs of Hello.

I'm pretty new to ios development, but I know that if you want to add null object to you data collection you have to use NSNULL class.
NSArray can't hold nils, if you want to check if null, please use this code
id object = myArray[0];// similar to [myArray objectAtIndex:0]
if(![object isEqual:[NSNull null]])
{
//do something if object is not equals to [NSNull null]
}

Related

App crashes while using NSURL URLWithString in a "for" loop

Here's my code,
for (int i=0; i<=[selfLinksArray count]; i++) {
NSString *temp = selfLinksArray[i];
NSURL *tempURL = [NSURL URLWithString:temp ];
NSLog(#"NSURL:%#",tempURL);
NSData *tempData = [NSData dataWithContentsOfURL:tempURL];
NSError *error = nil;
NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:tempData options:0 error:&error];
NSDictionary *volumeInfoDict = [tempDict objectForKey:#"volumeInfo"];
titleArray[i]=[volumeInfoDict objectForKey:#"title"];
}
Xcode shows me this error
[__NSArrayI length]: unrecognized selector sent to instance 0x7fdf334f0bc0
2016-01-19 16:12:34.937 BookFindr[7775:449799] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7fdf334f0bc0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010f34be65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010edc4deb objc_exception_throw + 48
2 CoreFoundation 0x000000010f35448d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010f2a190a ___forwarding___ + 970
4 CoreFoundation 0x000000010f2a14b8 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010f32e261 _CFURLCreateWithURLString + 81
6 Foundation 0x000000010e97a465 -[NSURL(NSURL) initWithString:relativeToURL:] + 349
7 Foundation 0x000000010e97a2e2 +[NSURL(NSURL) URLWithString:relativeToURL:] + 59
8 BookFindr 0x000000010e8bf9ab -[tabResTableViewController viewDidLoad] + 219
9 UIKit 0x000000010f88ef98 -[UIViewController loadViewIfRequired] + 1198
10 UIKit 0x000000010f894f4f -[UIViewController __viewWillAppear:] + 120
11 UIKit 0x000000010f8c4e44 -[UINavigationController _startCustomTransition:] + 1203
12 UIKit 0x000000010f8d523f -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
13 UIKit 0x000000010f8d63af -[UINavigationController __viewWillLayoutSubviews] + 57
14 UIKit 0x000000010fa7cff7 -[UILayoutContainerView layoutSubviews] + 248
15 UIKit 0x000000010f7af4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
16 QuartzCore 0x000000011327b59a -[CALayer layoutSublayers] + 146
17 QuartzCore 0x000000011326fe70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
18 QuartzCore 0x000000011326fcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
19 QuartzCore 0x0000000113264475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
20 QuartzCore 0x0000000113291c0a _ZN2CA11Transaction6commitEv + 486
21 UIKit 0x000000010f6f2f7c _UIApplicationHandleEventQueue + 7329
22 CoreFoundation 0x000000010f277a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
23 CoreFoundation 0x000000010f26d95c __CFRunLoopDoSources0 + 556
24 CoreFoundation 0x000000010f26ce13 __CFRunLoopRun + 867
25 CoreFoundation 0x000000010f26c828 CFRunLoopRunSpecific + 488
26 GraphicsServices 0x0000000112b08ad2 GSEventRunModal + 161
27 UIKit 0x000000010f6f8610 UIApplicationMain + 171
28 BookFindr 0x000000010e8c101f main + 111
29 libdyld.dylib 0x0000000111a8792d start + 1
30 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I have imported selfLinkArray from another viewController. I want to fetch various titles which are present in VolumeInfo dictionary. This dictionary is present in each individual link in selfLinksArray. Please help me with this error. Thanks in Advance.
Let's analyze your error and your backtrace :
-[__NSArrayI length]: unrecognized selector sent to instance 0x7fdf334f0bc0'
The issue is written here: the length method is called on an NSArray object.
Wait, this method is not even written in this sample! Don't worry, let's analyze it.
6 Foundation 0x000000010e97a465
-[NSURL(NSURL) initWithString:relativeToURL:] + 349
7 Foundation 0x000000010e97a2e2 +[NSURL(NSURL) URLWithString:relativeToURL:]
The crash log indicates that the crash occurs when you try to init the NSURL object. The only parameter of this init method is a NSString object. But are we sure that temp is really a NSString object ?
This variable is casted without checking if it's really an NSString object. It can be anthing, for example... the NSArray which receive a call to the length method. Also, this method is existing for a NSString object, so the unrecognizer selector error look totally related.
Where do you we call length on this NSString object ? My guess is, according to the backtrace, that URLWithString: call this method to check the NSString length in order to initialize properly the NSURL object.

Fetching data from `plist`

I am using for in loop to fetch data from plist. But it is showing following exception:
2015-07-16 11:16:43.597 plistNeha[640:60b] -[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x8d671b0
2015-07-16 11:16:43.721 plistNeha[640:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x8d671b0'
*** First throw call stack:
(
0 CoreFoundation 0x017ed1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0156c8e5 objc_exception_throw + 44
2 CoreFoundation 0x0188a243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x017dd50b ___forwarding___ + 1019
4 CoreFoundation 0x017dd0ee _CF_forwarding_prep_0 + 14
5 plistNeha 0x00002b7d -[sdViewController plistbtn:] + 333
6 libobjc.A.dylib 0x0157e880 -[NSObject performSelector:withObject:withObject:] + 77
7 UIKit 0x0022e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
8 UIKit 0x0022e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
9 UIKit 0x0032fbd1 -[UIControl sendAction:to:forEvent:] + 66
10 UIKit 0x0032ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
11 UIKit 0x0032f243 -[UIControl touchesEnded:withEvent:] + 641
12 UIKit 0x0026dddd -[UIWindow _sendTouchesForEvent:] + 852
13 UIKit 0x0026e9d1 -[UIWindow sendEvent:] + 1117
14 UIKit 0x002405f2 -[UIApplication sendEvent:] + 242
15 UIKit 0x0022a353 _UIApplicationHandleEventQueue + 11455
16 CoreFoundation 0x0177677f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
17 CoreFoundation 0x0177610b __CFRunLoopDoSources0 + 235
18 CoreFoundation 0x017931ae __CFRunLoopRun + 910
19 CoreFoundation 0x017929d3 CFRunLoopRunSpecific + 467
20 CoreFoundation 0x017927eb CFRunLoopRunInMode + 123
21 GraphicsServices 0x037e15ee GSEventRunModal + 192
22 GraphicsServices 0x037e142b GSEventRun + 104
23 UIKit 0x0022cf9b UIApplicationMain + 1225
24 plistNeha 0x00002e1d main + 141
25 libdyld.dylib 0x01e34701 start + 1
26 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Code:
NSString *path=[[NSBundle mainBundle]pathForResource:#"x" ofType:#"plist"];
ald=[[NSDictionary alloc]initWithContentsOfFile:path];
for(NSDictionary *awq in ald){
NSLog(#"check");
NSString *qqw=#"";
qqw=[awq objectForKey:#"qq"];
NSLog(#"%#",qqw);
}
But, if I am fetching data one by one then it is showing on debugger area. for in loop is also working as it is printing check. May be the line after NSLog(#"check"); is creating some exceptions.
Try this
NSString *path=[[NSBundle mainBundle]pathForResource:#"x" ofType:#"plist"];
NSDictionary *ald=[[NSDictionary alloc]initWithContentsOfFile:path];
for(NSDictionary *awq in [ald allValues]){
NSLog(#"check");
NSString *qqw=#"";
qqw=[awq objectForKey:#"qq"];
NSLog(#"%#",qqw);
}
for in on a dictionary is enumerating its keys, not values. See this question for each loop in objective c for accessing NSMutable dictionary
Try this:
NSString *path=[[NSBundle mainBundle]pathForResource:#"x" ofType:#"plist"];
NSDictionary *plistDictionary = [[NSDictionary alloc]initWithContentsOfFile:path];
for(NSString *key in plistDictionary){
NSDictionary *dictionary = [plistDictionary objectForKey:key];
NSLog(#"%#",[dictionary objectForKey:#"qq"]);
}
I had to change your variable names because it was too confusing to work with 'ald', 'awq', 'qqw' , etc.

NSUserDefaults setObject method causes the app to crash in ios8

I am having an app in which I am passing some data between viewControllers with NSUserDefaults.
This is the code below I am using.
[[NSUserDefaults standardUserDefaults]setObject:selectedLists forKey:UserID];
Where selectedLists is NSMutableArray and UserID is NSString.
This code works fine until ios7 and xCode5.
Since I have updated xCode6 and now I am using ios8, my app works fine with xCode 6 and ios 7.1 but the app crashes when I run the app in ios8 devices with the error below.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x7a027060'
I just don't understand what is the problem here.
If anyone has faced this problem with ios8 then please help me.
Thanks in advance.
Value of selectedLists
{
CardFace = Word;
ImportFlag = Import;
ListID = 1;
ListName = "Adjectives - Appearance";
QuizBy = Definition;
UserID = 1;
}
and UserId is 1.
EDITED
*** First throw call stack:
(
0 CoreFoundation 0x06a24df6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x03ac8a97 objc_exception_throw + 44
2 CoreFoundation 0x06a2ca75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x069759c7 ___forwarding___ + 1047
4 CoreFoundation 0x0697558e _CF_forwarding_prep_0 + 14
5 CoreFoundation 0x068df30f CFStringGetLength + 143
6 CoreFoundation 0x069de510 _CFPrefsEncodeKeyValuePairIntoMessage + 64
7 CoreFoundation 0x06a275cf -[CFPrefsPlistSource sendMessageSettingValue:forKey:] + 111
8 CoreFoundation 0x06954f3a -[CFPrefsPlistSource alreadylocked_setValue:forKey:] + 250
9 CoreFoundation 0x06954e22 -[CFPrefsSource setValue:forKey:] + 82
10 CoreFoundation 0x06954dc3 ___CFPreferencesSetValueWithContainer_block_invoke + 51
11 CoreFoundation 0x0690ef0a +[CFPrefsSource withSourceForIdentifier:user:byHost:container:perform:] + 1274
12 CoreFoundation 0x06954d61 _CFPreferencesSetValueWithContainer + 225
13 CoreFoundation 0x06a0aa62 _CFPreferencesSetAppValueWithContainer + 66
14 Foundation 0x0347f53f -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 59
15 Foundation 0x0353eeba -[NSUserDefaults(NSKeyValueCoding) setValue:forKey:] + 68
16 Vocab 0x000eaecc Vocab + 720588
17 libobjc.A.dylib 0x03ade7cd -[NSObject performSelector:withObject:withObject:] + 84
18 UIKit 0x022a079d -[UIApplication sendAction:to:from:forEvent:] + 99
19 UIKit 0x022a072f -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
20 UIKit 0x023d3a16 -[UIControl sendAction:to:forEvent:] + 69
21 UIKit 0x023d3e33 -[UIControl _sendActionsForEvents:withEvent:] + 598
22 UIKit 0x023d309d -[UIControl touchesEnded:withEvent:] + 660
23 UIKit 0x022f0aba -[UIWindow _sendTouchesForEvent:] + 874
24 UIKit 0x022f1595 -[UIWindow sendEvent:] + 791
25 UIKit 0x022b6aa9 -[UIApplication sendEvent:] + 242
26 UIKit 0x022c68de _UIApplicationHandleEventFromQueueEvent + 20690
27 UIKit 0x0229b079 _UIApplicationHandleEventQueue + 2206
28 CoreFoundation 0x069487bf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
29 CoreFoundation 0x0693e2cd __CFRunLoopDoSources0 + 253
30 CoreFoundation 0x0693d828 __CFRunLoopRun + 952
31 CoreFoundation 0x0693d1ab CFRunLoopRunSpecific + 443
32 CoreFoundation 0x0693cfdb CFRunLoopRunInMode + 123
33 GraphicsServices 0x0436424f GSEventRunModal + 192
34 GraphicsServices 0x0436408c GSEventRun + 104
35 UIKit 0x0229ee16 UIApplicationMain + 1526
36 Vocab 0x0004c9bc Vocab + 72124
37 libdyld.dylib 0x03ee6ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Edited
On my tableview's didselect method...
UserID = [[users objectAtIndex:indexPath.row] valueForKey:#"UserID"];
where users is an array.
You should check the UserID type because crash is not related with changing NSArray to NSMutableArray.
You must be confusing NSNumber with NSString on your userID:
Change your code and instead of userID use the variable userIDString
NSString userIDString;
//If UserID is class of NSNumber turn its value to NSString
if ([UserID isKindOfClass:[NSNumber class]])
{
userIDString = [UserID stringValue];
}
else
userIDString = UserID ;
During runtime UserId is probably of type NSNumber, even if in your code it is typed NSString.
Check how UserId is created. Use the debugger to check the runtime type.
The newly posted code shows that your retrieve the UserId from the users array which probably consists of dictionaries. In those dictionaries there's a value for the key UserID. It depends on how the array of dictionaries is created (json deserialization?) but it seems that the type of object referred to by the UserID key is a number type, not a string. So all you have to do is convert this number into a string:
UserID = [[[users objectAtIndex:indexPath.row] valueForKey:#"UserID"] stringValue];
Now your key is always a string.
Try to encode your data in NSData and save it in NSUserDefaults and retrieved back, once needed. Have a look at this thread and this one.
Should not use NSUSerdefault to keep a collection of custom objects.
You should save them using NSkeyedUnarchiver/NSkeyedarchiver.
Ref: NSCoding.
I think can't store NSMutableArrays in NSUserDefaults since iOS8 (according to different posts). Try casting it into an array before (or create a new one).
Like this for example :
NSArray *arr = [NSArray arrayWithArray:selectedLists];
And store arr in NSUserDefaults.

How is my code producing this crash: [NSConcreteData count]: unrecognized selector sent to instance

I am getting this crash showing up in BugSense:
-[NSConcreteData count]: unrecognized selector sent to instance 0x14e57f10 - NSInvalidArgumentException
in this code:
+ (NSMutableArray *)applyFilters:(NSMutableArray *)theItems fromFilter:(NSDictionary *)filters
{
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:theItems];
if ([[filters allKeys] count] > 0)
{
NSMutableArray *tempFilteredItems = [[NSMutableArray alloc] init];
for (NSString *key in [filters allKeys])
{
NSMutableString *convertedKey = [NSMutableString stringWithString:key];
[convertedKey replaceOccurrencesOfString:#" "
withString:#"_"
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [convertedKey length])];
NSArray *tempFilterAttributes = [NSArray arrayWithArray:filters[key]];
for (NSDictionary *item in items)
{
if (![[item[convertedKey] componentsSeparatedByString:#"*"] firstObjectCommonWithArray:tempFilterAttributes])
{
if (![tempFilteredItems containsObject:item])
{
[tempFilteredItems addObject:item];
}
}
}
[items removeObjectsInArray:tempFilteredItems];
[tempFilteredItems removeAllObjects];
}
}
return items;
}
Why/how is this happening?
Stack trace is:
CoreFoundation <redacted> + 130
1 libobjc.A.dylib objc_exception_throw + 38
2 CoreFoundation <redacted> + 202
3 CoreFoundation <redacted> + 706
4 CoreFoundation _CF_forwarding_prep_0 + 24
5 CoreFoundation <redacted> + 28
6 Video Games +[Helper applyFilters:fromFilter:] (Helper.m:2862)
7 Video Games -[WishlistViewController loadUp] (WishlistViewController.m:409) + 29303
8 Video Games -[WishlistViewController viewWillAppear:] (WishlistViewController.m:363) + 27955
9 UIKit <redacted> + 374
10 UIKit <redacted> + 612
11 UIKit <redacted> + 572
12 UIKit <redacted> + 44
13 UIKit <redacted> + 184
14 UIKit <redacted> + 380
15 QuartzCore <redacted> + 142
16 QuartzCore <redacted> + 350
17 QuartzCore <redacted> + 16
18 QuartzCore <redacted> + 228
19 QuartzCore <redacted> + 314
20 UIKit <redacted> + 126
21 CoreFoundation <redacted> + 20
22 CoreFoundation <redacted> + 286
23 CoreFoundation <redacted> + 738
24 CoreFoundation CFRunLoopRunSpecific + 524
25 CoreFoundation CFRunLoopRunInMode + 106
26 GraphicsServices GSEventRunModal + 138
27 UIKit UIApplicationMain + 1136
28 Video Games main (main.m:16) + 888911
29 libdyld.dylib <redacted> + 2
The problem has nothing to do with the code that you provided. The error messages says that the message count was sent to the class NSConcreteData, which is essentially the same as NSData, and such a class method does not exist.
I suggest that you set an exception breakpoint (in Xcode select left the breakpoint navigator, click + left bottom, and select add exception breakpoint).
When you execute the app, it will stop at the faulty instruction.
EDIT (due to the stack trace you provided now):
The stack trace indeed says that your method applyFilters:fromFilter: is responsible for the crash. The only place where the message count is sent in this method, is the statement [[filters allKeys] count]. It looks then that count is sent to an NSConcreteData object, and not to an NSArray object as you might have expected. One reason could be that the argument filters, that should be a NSDictionary, is an object of a different type.
So when the app stops at the exception breakpoint, please check the class of the instance filters.

Parsing JSON and sometimes getting number instead of a string

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).

Resources