For the below code snippet:
error occurs at setObject: forKey:
response <--is an NSDictionary
if(response){
NSMutableArray *lifeArray = [[NSMutableArray alloc] init];
NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init];
responseDict = (NSMutableDictionary *)[response mutableCopy];
[responseDict setObject:#"life" forKey:#"label"]; <-- Error here
}
Note that variable responseDict is NSMutableDictionary since copied via mutableCopy.
Error message:
2014-11-12 12:16:08.534 Lifetape[84381:508759] -[__NSCFArray setObject:forKey:]: unrecognized selector sent to instance 0x7a296700
2014-11-12 12:16:08.536 Lifetape[84381:508759] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray setObject:forKey:]: unrecognized selector sent to instance 0x7a296700'
*** First throw call stack:
(
0 CoreFoundation 0x028fbdf6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x02557a97 objc_exception_throw + 44
2 CoreFoundation 0x02903a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x0284c9c7 ___forwarding___ + 1047
4 CoreFoundation 0x0284c58e _CF_forwarding_prep_0 + 14
5 Lifetape 0x000dfb83 __50-[LTTimeLineTableViewController getTimeIntervals:]_block_invoke + 307
6 Lifetape 0x0014cedb -[MethodInvoker maybeInvokeCallback] + 395
7 Lifetape 0x0014d19f -[MethodInvoker receiveResultWithError:andResult:] + 399
8 Lifetape 0x00143e38 -[MeteorClient(Connection) livedata_result:] + 1544
9 Lifetape 0x00138674 __54-[MeteorClient(Connection) initConnectionWithOptions:]_block_invoke_3 + 2660
10 Lifetape 0x0014b2b5 -[MeteorClient didReceiveMessage:] + 197
11 Lifetape 0x0014eb7d -[ObjectiveDDP webSocket:didReceiveMessage:] + 269
12 Lifetape 0x0015d986 __30-[SRWebSocket _handleMessage:]_block_invoke + 102
13 libdispatch.dylib 0x031e941a _dispatch_call_block_and_release + 15
14 libdispatch.dylib 0x03209e1f _dispatch_client_callout + 14
15 libdispatch.dylib 0x031f0981 _dispatch_main_queue_callback_4CF + 610
16 CoreFoundation 0x02855f3e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
17 CoreFoundation 0x02814d40 __CFRunLoopRun + 2256
18 CoreFoundation 0x028141ab CFRunLoopRunSpecific + 443
19 CoreFoundation 0x02813fdb CFRunLoopRunInMode + 123
20 GraphicsServices 0x051a524f GSEventRunModal + 192
21 GraphicsServices 0x051a508c GSEventRun + 104
22 UIKit 0x00f41e16 UIApplicationMain + 1526
23 Lifetape 0x000ce36d main + 141
24 libdyld.dylib 0x03235ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here your response dictionary is a NSDictionary type collection so it does not have any selector like setObject, that's why it is giving you this error use
if(response && [response isKindOfClass:[NSDictionary class]]){
NSMutableArray *lifeArray = [[NSMutableArray alloc] init];
NSMutableDictionary *responseDict = [NSMutableDictionary dictionaryWithDictionary:response];
[responseDict setObject:#"life" forKey:#"label"];
}
and it will work but make sure response is your dictionary type object. as the error you have posted clearly says that your response is an array not a dictionary.
Your response looks like NSArray class so try below code.
if([response isKindOfClass:[NSArray Class]]){
NSLog(#"Array");
}
else if([response isKindOfClass:[NSDictionary Class]]){
NSLog(#"Dictionary");
}
Now manage your code as per log.
Related
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.
I am creating one app in which I am saving my array of custom object in NSUserDefaults using this below code :
[Util setSubProductsArrayPreference:myArray forKey:productId];
//Util.m
+(void)setSubProductsArrayPreference:(NSMutableArray *)subProducts forKey:(NSString *)string
{
for (int i=0; i<subProducts.count; i++) {
SubProducts *po=[subProducts objectAtIndex:i];
NSString *st=[NSString stringWithFormat:#"%d",i];
[Util setSubProductsPreference:po forKey:st];
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:subProducts];
[prefs setObject:myEncodedObject forKey:string];
[prefs synchronize];
}
+(void)setSubProductsPreference:(SubProducts *)subProducts forKey:(NSString *)key
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:subProducts];
[prefs setObject:myEncodedObject forKey:key];//here it is crashing
[prefs synchronize];
}
This code is working fine with iOS7 and prior..The problem is when I am running my same code with iOS8 in xCode6 then I am getting the below error :
-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000f23
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000f23'
*** First throw call stack:
(
0 CoreFoundation 0x00000001042fbf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000103bbabb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010430304d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010425b27c ___forwarding___ + 988
4 CoreFoundation 0x000000010425ae18 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x00000001042bc790 _CFPrefsEncodeKeyValuePairIntoMessage + 64
6 CoreFoundation 0x00000001042fe286 -[CFPrefsPlistSource sendMessageSettingValue:forKey:] + 102
7 CoreFoundation 0x000000010423cef7 -[CFPrefsPlistSource alreadylocked_setValue:forKey:] + 215
8 CoreFoundation 0x000000010423cdee -[CFPrefsSource setValue:forKey:] + 62
9 CoreFoundation 0x00000001041fa1f8 +[CFPrefsSource withSourceForIdentifier:user:byHost:container:perform:] + 1112
10 CoreFoundation 0x000000010423cd63 _CFPreferencesSetValueWithContainer + 227
11 Foundation 0x000000010374099b -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 46
12 HP ProTrain 0x0000000101186787 +[Util setSubProductsArrayPreference:forKey:] + 487
13 HP ProTrain 0x0000000101115a38 __74-[DesktopProductsViewController fetchingProductSubCategoryDataFromServer:]_block_invoke_2 + 3096
14 libdispatch.dylib 0x00000001051b4ba6 _dispatch_call_block_and_release + 12
15 libdispatch.dylib 0x00000001051d27f4 _dispatch_client_callout + 8
16 libdispatch.dylib 0x00000001051bb8fb _dispatch_main_queue_callback_4CF + 949
17 CoreFoundation 0x0000000104263fe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
18 CoreFoundation 0x0000000104226eeb __CFRunLoopRun + 2043
19 CoreFoundation 0x0000000104226486 CFRunLoopRunSpecific + 470
20 GraphicsServices 0x00000001082c29f0 GSEventRunModal + 161
21 UIKit 0x0000000102463420 UIApplicationMain + 1282
22 HP ProTrain 0x00000001010c1a87 main + 151
23 libdyld.dylib 0x0000000105207145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Do any one else experience the same problem?
Change this line
[Util setSubProductsArrayPreference:myArray forKey:[NSString stringWithFormat:#"%#",productId]];
Simple, your productId is not returning string. Just typecast productId to NSString before setObject.
I'm trying to get simple login with email and password on iOS. I got the following error when logging in.
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary
initWithObjects:forKeys:count:]: attempt to insert nil object from
objects[0]'
* First throw call stack: ( 0 CoreFoundation 0x02eee1e4 exceptionPreprocess + 180 1 libobjc.A.dylib
0x02c6d8e5 objc_exception_throw + 44 2 CoreFoundation
0x02eb4376 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]
+ 390 3 CoreFoundation 0x02ee1c29 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 73 4 app 0x000ed30c ParseUserData + 428 5 app
0x0009e6e1 __37-[MenuViewController checkAuthStatus]_block_invoke +
161 6 app 0x0011f89f
__82-[FirebaseSimpleLogin attemptAuthWithToken:provider:userData:account:andCallback:]_block_invoke
+ 461 7 app 0x00199df7 __43-[FRepo callOnComplete:withStatus:andData:]_block_invoke + 33 8
libdispatch.dylib 0x06e457b8
_dispatch_call_block_and_release + 15 9 libdispatch.dylib 0x06e5a4d0 _dispatch_client_callout + 14 10 libdispatch.dylib
0x06e48726 _dispatch_main_queue_callback_4CF + 340 11 CoreFoundation
0x02f5343e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 14 12
CoreFoundation 0x02e945cb __CFRunLoopRun + 1963
13 CoreFoundation 0x02e939d3
CFRunLoopRunSpecific + 467 14 CoreFoundation
0x02e937eb CFRunLoopRunInMode + 123 15 GraphicsServices
0x03d535ee GSEventRunModal + 192 16 GraphicsServices
0x03d5342b GSEventRun + 104 17 UIKit
0x0192df9b UIApplicationMain + 1225 18 app
0x0010623d main + 141 19 libdyld.dylib
0x0708f701 start + 1 20 ???
0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught
exception of type NSException
My code:
Firebase *ref = [[Firebase alloc] initWithUrl:FIREBASE];
FirebaseSimpleLogin *authClient = [[FirebaseSimpleLogin alloc] initWithRef:ref];
[authClient loginWithEmail:#"useremail" andPassword:#"userPassword"
withCompletionBlock:^(NSError* error, FAUser* user) {
if (error != nil) {
NSLog(#"Success!");
// There was an error logging in to this account
} else {
NSString *message = [error.userInfo valueForKey:#"NSLocalizedDescription"];
if (message == nil) message = #"Please try again";
[self performSelectorOnMainThread:#selector(showError:) withObject:message waitUntilDone:NO];
}
}];
Getting this error, my app don't get data form database anymore with crash.. Anybody have any idea?
Look at the stack trace. The error is in your method ParseUserData, which you didn't show. In that method you are trying to create a dictionary, but the first of the objects that you pass in is nil.
In Xcode, set a breakpoint that triggers when exceptions are thrown, so the next time you can check in your running program what happens.
At GitHub I have a simple iPhone app, which fetches user information (by using OAuth) from the social network Mail.ru:
It does fetch and print the information, but then crashes.
As an iOS programming newbie I'm confused by the output below (also please see the full output at PasteBin):
2014-01-21 21:21:10.873 oauthMailru[8228:3307] -[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0x8a97290
2014-01-21 21:21:10.875 oauthMailru[8228:3307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0x8a97290'
*** First throw call stack:
(
0 CoreFoundation 0x01aa65e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018298b6 objc_exception_throw + 44
2 CoreFoundation 0x01b43903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01a9690b ___forwarding___ + 1019
4 CoreFoundation 0x01a964ee _CF_forwarding_prep_0 + 14
5 oauthMailru 0x00003a62 __47-[ViewController fetchMailruWithToken:ForUser:]_block_invoke + 402
6 Foundation 0x01545695 __67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2 + 151
7 Foundation 0x014a5945 -[NSBlockOperation main] + 88
8 Foundation 0x014fe829 -[__NSOperationInternal _start:] + 671
9 Foundation 0x0147b558 -[NSOperation start] + 83
10 Foundation 0x01500af4 __NSOQSchedule_f + 62
11 libdispatch.dylib 0x021344b0 _dispatch_client_callout + 14
12 libdispatch.dylib 0x02121018 _dispatch_async_redirect_invoke + 202
13 libdispatch.dylib 0x021344b0 _dispatch_client_callout + 14
14 libdispatch.dylib 0x02122eeb _dispatch_root_queue_drain + 287
15 libdispatch.dylib 0x02123137 _dispatch_worker_thread2 + 39
16 libsystem_pthread.dylib 0x024c0dab _pthread_wqthread + 336
17 libsystem_pthread.dylib 0x024c4cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Does anybody please know what's happening and how to read such crashes?
My source files: ViewController.m, which displays UIWebView and then segues to DetailViewController.m
Your code thinks the JSON deserializes to an object (dictionary), but in fact it deserializes to an array containing one object. Try this:
NSMutableArray *topLevelArray = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers error:nil];
NSDictionary *dict = topLevelArray[0];
If you want to check what you're getting, you can use isKindOfClass: like this:
id jso = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers error:nil];
if (jso == nil) {
// Error. You should probably have passed an NSError ** as the error
// argument so you could log it.
} else if ([jso isKindOfClass:[NSArray class]]) {
NSArray *array = jso;
// process array elements
} else if ([jso isKindOfClass:[NSDictionary class]]) {
NSDictionary *dict = jso;
// process dictionary elements
} else {
// Shouldn't happen unless you use the NSJSONReadingAllowFragments flag.
}
Im trying to create an infinite scroll type thing with Instagram using UITableView and AFNetworking however I get this error when I get to the bottom of the View:
CRASH :
-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object
2014-01-05 20:51:41.627 Floadt[1579:70b] STACK TRACE :
(
0 CoreFoundation 0x02b775e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x027a38b6 objc_exception_throw + 44
2 CoreFoundation 0x02b773bb +[NSException raise:format:] + 139
3 CoreFoundation 0x02bfe365 -[__NSCFArray insertObject:atIndex:] + 101
4 CoreFoundation 0x02b3b2d0 -[NSMutableArray insertObjects:count:atIndex:] + 208
5 CoreFoundation 0x02b3af69 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 425
6 CoreFoundation 0x02b3ad15 -[NSMutableArray addObjectsFromArray:] + 661
7 Floadt 0x0003f5ec __42-[StreamViewController nextInstagramPage:]_block_invoke + 284
8 Floadt 0x0001edfb __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke91 + 43
9 libdispatch.dylib 0x035247f8 _dispatch_call_block_and_release + 15
10 libdispatch.dylib 0x035394b0 _dispatch_client_callout + 14
11 libdispatch.dylib 0x0352775e _dispatch_main_queue_callback_4CF + 340
12 CoreFoundation 0x02bdca5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
13 CoreFoundation 0x02b1d6bb __CFRunLoopRun + 1963
14 CoreFoundation 0x02b1cac3 CFRunLoopRunSpecific + 467
15 CoreFoundation 0x02b1c8db CFRunLoopRunInMode + 123
16 GraphicsServices 0x0453c9e2 GSEventRunModal + 192
17 GraphicsServices 0x0453c809 GSEventRun + 104
18 UIKit 0x01936d3b UIApplicationMain + 1225
19 Floadt 0x0006646d main + 141
20 libdyld.dylib 0x037cb70d start + 1
21 ??? 0x00000001 0x0 + 1
)
Here is how I am making my code, what confuses me is the fact that all my Arrays are Mutable but it states that the mutating method was sent to an immutable object:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y == roundf(scrollView.contentSize.height-scrollView.frame.size.height)) {
NSDictionary *page = instagramResponse[#"pagination"];
NSString *nextPage = page[#"next_url"];
[[InstagramClient sharedClient] getPath:[NSString stringWithFormat:#"%#",nextPage] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
instagramResponse = [responseObject mutableCopy];
[instagramResponse addEntriesFromDictionary:responseObject];
[instaPics addObjectsFromArray:responseObject[#"data"]];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", error);
}];
}
}
before addEntriesFromDictionary the copy of Dictionarylike this
NSDictionary *copyOfDic = [[NSDictionary alloc] initWithDictionary: originalDic copyItems: YES];
if (copyOfDic ) {
[destination addEntriesFromDictionary: copyOfDic ];
[copyOfDic release];
}
or
[instaPics addObjectsFromArray:[responseObject[#"data"]mutable copy]];
or
Your #property yourarray is either initialized with an NSArray or declared copy.
because both would be lead to this error/exception message since the backing ivar would point to an (immutable) NSArray.
If you have declared your property to be of NSMutableArray type, use strong as the storage modifier instead of copy.