Here's a little test:
NSDictionary * test = # { #"centers" : #[
#{ #"id":#"12345", #"favorite":#NO },
#{ #"id":#"2345", #"favorite":#NO },
#{ #"id":#"345", #"favorite":#YES },
#{ #"favorite":#NO }
]};
NSMutableArray * test2 = [test mutableArrayValueForKeyPath:#"centers.id"];
NSLog(#"%#",test2);
The log gives:
(
12345,
2345,
345,
"<null>"
)
That's great, now, I wish to get rid of the NSNull values... According to How will I be able to remove [NSNull Null] objects from NSMutableArray? this is the way:
[test2 removeObjectIdenticalTo:[NSNull null]];
NSLog(#"%#",test2);
Unfortunately, it crashes:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x7fe2ee01e970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key id.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e47df35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010e116bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010e47db79 -[NSException raise] + 9
3 Foundation 0x000000010dcb37b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 Foundation 0x000000010dd44be0 -[NSArray(NSKeyValueCoding) setValue:forKey:] + 186
5 Foundation 0x000000010dd471cc -[NSKeyValueSlowMutableArray removeObjectAtIndex:] + 89
6 CoreFoundation 0x000000010e3d75a3 -[NSMutableArray removeObjectIdenticalTo:] + 131
7 TestImageSizeReduction 0x000000010dbe6d35 -[ViewController viewDidLoad] + 1397
8 UIKit 0x000000010ebd2a90 -[UIViewController loadViewIfRequired] + 738
9 UIKit 0x000000010ebd2c8e -[UIViewController view] + 27
10 UIKit 0x000000010eaf1ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010eaf2041 -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010eafe72c -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010eaa9061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
14 UIKit 0x000000010eaabd2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
15 UIKit 0x000000010eaaabf2 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x0000000111f582a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
17 CoreFoundation 0x000000010e3b353c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x000000010e3a9285 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x000000010e3a9045 __CFRunLoopRun + 2389
20 CoreFoundation 0x000000010e3a8486 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010eaaa669 -[UIApplication _run] + 413
22 UIKit 0x000000010eaad420 UIApplicationMain + 1282
23 TestImageSizeReduction 0x000000010dbe81f3 main + 115
24 libdyld.dylib 0x0000000110c50145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
So, I know many ways of removing NSNull instances from the array (predicates, indexesPassingTests, etc.) but I really want to understand why this fails with so little explanations.
Thanks in advance.
Edit:
To prevent the proxy problem given in Ian's answer the simplest solution is:
NSMutableArray * test2 = [[test valueForKeyPath:#"centers.id"] mutableCopy];
The NSMutableArray returned from mutableArrayValueForKeyPath is not a copy of the values, but instead a proxy mutable array that does a lookup to the original object on each access.
Telling it to remove objects will act upon your original object. Telling it to remove objects whose centers.id is [NSNull null] causes a failure because those objects do not respond to the key-value mapping for id. It's strange to think that it would have given you any result in the first place, but you cannot use this [NSNull null] result in the way that you want.
If you just want the values and you don't want any operations available to allow modification of the original object, I suggest that you construct your array in a different way to begin with; perhaps with a predicate.
NSNull is a null object type which allows to add NULL object to array because array doesn't accept nil value as input. If anything as a NULL we have to feed into array we use [NSNull null] singleton type object.
In your case is generated because of empty memory location it is not [NSNull null] object type. You cannot remove it using [test2 removeObjectIdenticalTo:[NSNull null]];
It can be removed by using NSPredicate Search
Try this Use NSPredicate Search to filter NSArray. You can remove null by using NSPredicate
NSDictionary * test = # { #"centers" : #[
#{ #"id":#"12345", #"favorite":#NO },
#{ #"id":#"2345", #"favorite":#NO },
#{ #"id":#"345", #"favorite":#YES },
#{ #"favorite":#NO }
]};
NSMutableArray * test2 = [test mutableArrayValueForKeyPath:#"centers.id"];
NSLog(#"%#",test2);
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"SELF!=nil"];
NSArray *test3=[test2 filteredArrayUsingPredicate:predicate];
NSLog(#"%#",test3);
Related
This my code ,i am removing multiple values on my condition
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCan *cell=[collectionView cellForItemAtIndexPath:indexPath];
UIImage *getimg=[dicPhotoStore objectForKey:#(indexPath.row)];
BOOL integer=[dic objectForKey:#(indexPath.row)];
if(integer)
{
for(id img in arrFinalStore)
{
if(img==getimg)
{
NSLock *arrayLock = [[NSLock alloc] init];
[arrayLock lock];
[arrFinalStore removeObject:img];
NSLog(#"crash inside");
[arrayLock unlock];
}
}
#synchronized(self)
{
[dic removeObjectForKey:#(indexPath.row)];
[dicPhotoStore removeObjectForKey:#(indexPath.row)];
}
NSLog(#"crashoutside");
NSLog(#"inside false");
}
else
{
[dicPhotoStore setObject:cell.imgView.image forKey:#(indexPath.row)];
[arrFinalStore addObject:cell.imgView.image];
[dic setObject:#"1" forKey:#(indexPath.row)];
}
[_collection reloadData];
}
I am understood something goes wrong trying to find out this error ,please help me to overcome this .
This is error :
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x7f881a6b1900> was mutated while being enumerated.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010299fe65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000102418deb objc_exception_throw + 48
2 CoreFoundation 0x000000010299f7c4 __NSFastEnumerationMutationHandler + 132
3 PhotoCollageCanvas 0x000000010056878f -[photoAssets collectionView:didSelectItemAtIndexPath:] + 767
4 UIKit 0x0000000103afb4a7 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 701
5 UIKit 0x0000000103b1d049 -[UICollectionView touchesEnded:withEvent:] + 574
6 UIKit 0x00000001034b6ef7 forwardTouchMethod + 349
7 UIKit 0x00000001034b6fc0 -[UIResponder touchesEnded:withEvent:] + 49
8 UIKit 0x00000001034b6ef7 forwardTouchMethod + 349
9 UIKit 0x00000001034b6fc0 -[UIResponder touchesEnded:withEvent:] + 49
10 UIKit 0x0000000103783ede _UIGestureRecognizerUpdate + 10279
11 UIKit 0x0000000103319f8a -[UIWindow _sendGesturesForEvent:] + 1137
12 UIKit 0x000000010331b1c0 -[UIWindow sendEvent:] + 849
13 UIKit 0x00000001032c9b66 -[UIApplication sendEvent:] + 263
14 UIKit 0x00000001032a3d97 _UIApplicationHandleEventQueue + 6844
15 CoreFoundation 0x00000001028cba31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x00000001028c195c __CFRunLoopDoSources0 + 556
17 CoreFoundation 0x00000001028c0e13 __CFRunLoopRun + 867
18 CoreFoundation 0x00000001028c0828 CFRunLoopRunSpecific + 488
19 GraphicsServices 0x00000001058d8ad2 GSEventRunModal + 161
20 UIKit 0x00000001032a9610 UIApplicationMain + 171
21 Photo 0x0000000100566a5f main + 111
22 libdyld.dylib 0x000000010697b92d start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
What do you think that lock would do? It does exactly nothing. You created a lock, locked it, unlocked it. Since that code is the only code with access to the lock, that lock cannot possibly prevent anyone from doing anything.
And it wouldn't solve your problem anyway. Someone is enumerating that array. Any attempt to modify it, as you are trying, while it is enumerated, will crash.
Your code is (with lines removed)
for(id img in arrFinalStore)
{
[arrFinalStore removeObject:img];
}
That code is absolutely going to crash, and there is nothing that can stop it from crashing. You cannot remove an object from an array while iteration.
You cannot remove element while iterating through array
[arrFinalStore removeObject:img];
just use [arrFinalStore removeObject:img];
You cannot remove element while iterating through array, so you can create temp array with all unnecessary objects and then call - (void)removeObjectsInArray:(NSArray<ObjectType> *)otherArray
So your code is:
NSMutableArray *tempArray = [NSMutableArray array];
for(id img in arrFinalStore)
{
if(img==getimg)
{
[tempArray addObject:img];
}
}
[arrFinalStore removeObjectsInArray:tempArray];
also you can use simple for (int i = arrfinalstoer.count - 1; i > 0; --i) for enumeration.
Its not the issue with the lock. But the issue is while iterating you are trying to remove the element which means you doing concurrent modification which throws exception.
here is an example you can use
for (item in originalArrayOfItems) {
if ([item shouldBeDiscarded])
[discardedItems addObject:item];}
[originalArrayOfItems removeObjectsInArray:discardedItems];
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]
}
I have this code:
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var array: AnyObject = []
for obj in Category.allObjects() {
if var add = obj as? Category {
array.addObject(add.name)
println(add.name)
}
}
return String(array[section] as String)
}
Im using a Realm database and I am trying to get one of the columns of the database to print in the section headers. Im also using a the same process for all the other required UITableView methods eg/ numberOfSectionsInTable etc etc. The code is giving me this error:
2014-10-26 20:47:33.479 Project[14631:937721] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7fdaf270ddb0
2014-10-26 20:47:33.481 Project[14631:937721] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7fdaf270ddb0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109960f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001095f9bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010996804d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001098c027c ___forwarding___ + 988
4 CoreFoundation 0x00000001098bfe18 _CF_forwarding_prep_0 + 120
5 BuildersUtility 0x0000000108d7f720 _TFC15BuildersUtility29ProductsDetailsViewController27numberOfSectionsInTableViewfS0_FCSo11UITableViewSi + 1408
6 BuildersUtility 0x0000000108d7f8ca _TToFC15BuildersUtility29ProductsDetailsViewController27numberOfSectionsInTableViewfS0_FCSo11UITableViewSi + 58
7 UIKit 0x000000010a478a7e -[UITableViewRowData _updateNumSections] + 84
8 UIKit 0x000000010a479474 -[UITableViewRowData invalidateAllSections] + 69
9 UIKit 0x000000010a2cdb03 -[UITableView _updateRowData] + 214
10 UIKit 0x000000010a2e300f -[UITableView numberOfSections] + 27
11 UIKit 0x000000010a4e0645 -[UITableViewController viewWillAppear:] + 97
12 UIKit 0x000000010a327821 -[UIViewController _setViewAppearState:isAnimating:] + 487
13 UIKit 0x000000010a352960 -[UINavigationController _startTransition:fromViewController:toViewController:] + 776
14 UIKit 0x000000010a353487 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
15 UIKit 0x000000010a353f47 -[UINavigationController __viewWillLayoutSubviews] + 43
16 UIKit 0x000000010a499509 -[UILayoutContainerView layoutSubviews] + 202
17 UIKit 0x000000010a277973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
18 QuartzCore 0x000000010a089de8 -[CALayer layoutSublayers] + 150
19 QuartzCore 0x000000010a07ea0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
20 QuartzCore 0x000000010a07e87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
21 QuartzCore 0x0000000109fec63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
22 QuartzCore 0x0000000109fed74a _ZN2CA11Transaction6commitEv + 390
23 QuartzCore 0x0000000109feddb5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
24 CoreFoundation 0x0000000109895dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
25 CoreFoundation 0x0000000109895d20 __CFRunLoopDoObservers + 368
26 CoreFoundation 0x000000010988bb53 __CFRunLoopRun + 1123
27 CoreFoundation 0x000000010988b486 CFRunLoopRunSpecific + 470
28 GraphicsServices 0x000000010dec79f0 GSEventRunModal + 161
29 UIKit 0x000000010a1fe420 UIApplicationMain + 1282
30 BuildersUtility 0x0000000108d9589e top_level_code + 78
31 BuildersUtility 0x0000000108d958da main + 42
32 libdyld.dylib 0x000000010baaf145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
BTW if you have a better way for me to extract the code from the database for a project like this please chirp in! :)
Thanks in Advance
allObjects will return you an instance of RLMResults, which is one of the two list types in Realms, the other being RLMArray. They are both made to be similar to NSArrays or swift arrays, and implement a protocol called RLMCollection. Among other things, that means you can access its elements with normal [i] notation. So you can simply write
return Category.allObjects()[section].name
although you might want to do some checking before you return. Also, it is generally not advisable to repeat the allObjects() query more than you need to, so you could cache in a lazy instance variable or similar. In this case there probably aren't that many categories/section headers, so it shouldn't be an issue.
More importantly, keep in mind that although the RLMResults list you get back is ordered in the sense that it is an ordered list, there is no inherent order among the Category instances in the Realm, so the next time you do Category.allObjects() you are not really guaranteed to receive the objects in the same order. So what you should do is really to create an RLMArray of Category objects and make that a property of another object. Then the order will be preserved.
It is because AnyObject does not support the method addObject
I think what you have in mind is that you want the variable array to be an array of type AnyObject.
You should declare it this way:
var array:[AnyObject] = []
And then when you want to add anything to the array, do this:
array.append(add.name)
In addition to what #AnthonyKong is pointing out, it is a bit unclear from the above code why you would want an array of AnyObject in the first place.
var array = [AnyObject]()
It seems what you actually want is an array of String?
var array = [String]()
then your return statement is simplified down to:
return array[section]
When feeling up to it you might also look into filter to rip out some of that almost-boiler-plate-logic in your code.
I had a similar problem but with an NSMutableArray. Thanks to #Gusutafu's comment above that setting an array to #[] converts it to an NSArray.
My incorrect code (selectedTags is an NSMutableArray):
selectedTags = ([theInputParameters valueForKey:kAcronymTagsSelectedTags] != nil)
? [theInputParameters valueForKey:kAcronymTagsSelectedTags]
: #[];
This gave the error -[__NSArrayI addObject:]: unrecognized selector sent to instance when I tried to add an object.
My corrected code
selectedTags = ([theInputParameters valueForKey:kAcronymTagsSelectedTags] != nil)
? [theInputParameters valueForKey:kAcronymTagsSelectedTags]
: [NSMutableArray arrayWithArray:#[]];
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.
int x = 5;
int t = 6;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:#"books.book", #"og:type",
#"www.goodreads.com", #"og:url",
#"Snow Crash", #"og:title",
#"978-3-16-148410-0",#"books:isbn",
#"http://en.wikipedia.org/wiki/File:Snowcrash.jpg", #"og:image",
#"www.google.com",#"og:audio:url",
#"www.facebook.com",#"al:windows_universal:url",
#"www.facebook.com",#"al:windows_phone:url",
#"www.facebook.com",#"al:windows:url",
#"www.facebook.com",#"al:iphone:url",
#"www.facebook.com",#"al:ipad:url",
x,#"books:rating:value",
t,#"books:rating:scale",
#"In reality, Hiro Protagonist delivers pizza for Uncle Enzo’s CosoNostra Pizza Inc., but in the Metaverse he’s a warrior prince. Plunging headlong into the enigma of a new computer virus that’s striking down hackers everywhere, he races along the neon-lit streets on a search-and-destroy mission for the shadowy virtual villain threatening to bring about infocalypse. Snow Crash is a mind-altering romp through a future America so bizarre, so outrageous…you’ll recognize it immediately.", #"og:description",
#"Science Fiction",#"books:genre",
#"eu_es",#"books:language:locale",
#[#"en_us",#"ca_es",#"cs_cz"],#"books:language:alternate",nil];
As you can see above I'm trying to create a Dictionary, however whenever i run the code i get error like this.
2014-06-11 23:50:50.338 TestApp[8374:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil. Or, did you forget to nil-terminate your parameter list?'
*** First throw call stack:
(
0 CoreFoundation 0x00000001019ef495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010174e99e objc_exception_throw + 43
2 CoreFoundation 0x00000001019d27e3 +[NSDictionary dictionaryWithObjectsAndKeys:] + 1043
3 TestApp 0x000000010000724d -[CYCViewController postBooks:] + 1021
4 UIKit 0x00000001002fcf06 -[UIApplication sendAction:to:from:forEvent:] + 80
5 UIKit 0x00000001002fceb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
6 UIKit 0x00000001003d9880 -[UIControl _sendActionsForEvents:withEvent:] + 203
7 UIKit 0x00000001003d8dc0 -[UIControl touchesEnded:withEvent:] + 530
8 UIKit 0x0000000100333d05 -[UIWindow _sendTouchesForEvent:] + 701
9 UIKit 0x00000001003346e4 -[UIWindow sendEvent:] + 925
10 UIKit 0x000000010030c29a -[UIApplication sendEvent:] + 211
11 UIKit 0x00000001002f9aed _UIApplicationHandleEventQueue + 9579
12 CoreFoundation 0x000000010197ed21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
13 CoreFoundation 0x000000010197e5f2 __CFRunLoopDoSources0 + 242
14 CoreFoundation 0x000000010199a46f __CFRunLoopRun + 767
15 CoreFoundation 0x0000000101999d83 CFRunLoopRunSpecific + 467
16 GraphicsServices 0x0000000103b66f04 GSEventRunModal + 161
17 UIKit 0x00000001002fbe33 UIApplicationMain + 1010
18 TestApp 0x0000000100008153 main + 115
19 libdyld.dylib 0x00000001020875fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I've followed solutions by adding NILbut still doesn't work. Everything is well placed. I checked and checked.
The values which can be nil in your dictionary are x and t. The above error says the object can't be nil, you can't put nil in place of an object of a key in a dictionary, instead you can try to put #"" or even you can supply [NSNull null].
Update
As you are trying to put
int x = 5;
int t = 6;
the above values are non-object values, a dictionary requires object, try put.
[NSNumber numberWithInt:x]
[NSNumber numberWithInt:t]