Changing integer values in NSMutableArray, IOS Objective-C - ios

I created an array that would hold some integers. However I seem to get an error on a line of code which is trying to alter one of the values in the array. I have created an array as follows:
NSMutableArray *lockedArray;
lockedArray = [NSMutableArray arrayWithCapacity:50];
I have added some values into the array:
[lockedArray addObject:#10];
[lockedArray addObject:#20];
[lockedArray addObject:#30];
[lockedArray addObject:#40];
[lockedArray addObject:#50];
[lockedArray addObject:#60];
[lockedArray addObject:#70];
[lockedArray addObject:#80];
Now I want to change one of the values. For example if I want to change the 6th value then I use the following code...
[lockedArray replaceObjectAtIndex:5 withObject:#1];
In Xcode I do not get any warning and it all looks fine. When I run the code and initiate replacing an object it crashes and gives me the following error...
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: '-[__NSCFArray
replaceObjectAtIndex:withObject:]: mutating method sent to immutable
object'

We don't know what you are doing exactly, but at the point where replaceObjectAtIndex is called, lockedArray is not a mutable array but an immutable array.
Since an array cannot be changed from being mutable to immutable, you most likely assigned a different (immutable) array to lockedArray after you added the other elements.

Have u allocated and initialized ur array like this,
lockedArray = [[NSMutableArray alloc]init];
Try it, Hope it will work for u

Make sure you check that you haven't pointed lockedArray to a non mutable array or you haven't initiated it as such. Use Xcode's find function to search for
"lockedArray = [NSArray"
or
"lockedArray ="

Related

iOS: syntax to retrieve value of attribute in array?

I have an array of user objects, one of which is stored in a property _selectedUser. When I log it out, it displays as :
{
uid = 5;
uname = xxxx;
}
However, when I try to access the uid with the following, I get an error:
NSNumber *useridnum = _selectedUser.uid;
Error:
[NSKnownKeysDictionary1 uid]: unrecognized selector sent to instance
What is the proper syntax to retrieve value for uid?
You are trying to access the key as a property on the dictionary, not as a value it contains.
As D.C. suggested in the comments, use this code:
NSNumber *useridnum = _selectedUser[#"uid];
Or:
NSNumber *useridnum = [_selectedUser objectForKey:#"uid"];
You said in the comments that the object is an NSManagedObject. I'm not familiar with Core Data, but it looks like you need to use valueForKey::
NSNumber *useridnum = [_selectedUser valueForKey:#"uid"];
An NSDictionary does not store the keys directly as properties; it stores them in some kind of private data structure. It wouldn't be possible to dynamically store keys as properties without messing with the runtime, and then only a very limited set of strings could be used as keys (instead of basically every possible object), because the keys would have to be C identifiers.
You get this error because when you tried to access the key as a property, the system sent a message to the object saying to call the getter method for the property, but that getter method didn't exist, so the app crashed.

NSString from NSString to set as key in NSMutableDictionary

I am getting a initWithObjects:count:]: attempt to insert nil object from objects[0] at the following line:
[contentsOfCocktails setObject:[NSMutableArray arrayWithObject: recipeTitleA] forKey:cocktailsTitleA];
recipeTitleA is the string I'm creating from the cocktails.recipeID class property that equals A. However, I am getting a recipeTitleA equals nil in the debug window.
Here is where I set cocktails.recipeID equal to recipeTitleA:
if ([cocktails.recipeID isEqualToString:#"A"]){
recipeTitleA = cocktails.recipeID;
}
Is this the correct way to set a string equal to another string in order to use it as a key in a NSMutableDictionary?
Long story short: I am trying to extract the recipeIDs that equal A and set them as a key in a dictionary. I will be doing this with other letters as keys as well. I was then going to store them in an array in which I could create sections in the tableview. Data is brought in with FMDB.
I'm new to obj-c and new to data formatting with arrays and dictionaries. Any help would be greatly appreciated!
It looks like you are doing the string comparison correctly. Set a breakpoint at if ([cocktails.recipeID isEqualToString:#"A"]){ and see what you are getting for cocktails.recipeID, if it's nil go back into your cocktails.recipeID property and look if its being set correctly.

Couchbase iOS:How to convert a CBLDocument to NSDictionary?

What I am trying to do is convert a document to an dictionary and then iterate that dictionary to see what's inside, but I am having the following problem when trying to convert the document.
I have the following code:
CBLDocument *document = row.value;
NSDictionary *dict = document.properties;
the xCode is always complaining "[__NSCFDictionary currentRevision]: unrecognized selector sent to instance 0x90c4150" when running to the second statement.
I have also tried NSDictionary *dict = document.currentRevision.properties;
It's not working either.
Could anyone help me with that?
OK, it turns out row.value is not a document(is actually a dictionary), but nobody tells me that when I emit(#[somekey], doc); The doc (which is the value) is actually a dictionary instead of a document.
But weird though the compiler did not complain when assigning the dictionary to a CBLDocument.

Need help understanding a conditional crash when accessing my NSDictionary

I am keeping my data in a property called practiceRecords (an NSArray of dictionaries).
I check to see if the data already exists in the documents folder.
If yes, I load the data into self.practiceRecords.
If not, I build the array of dictionaries (using literal syntax), keeping this data in the self.practiceRecords property, and then write the data out to the documents folder.
(I am NOT reloading the data after writing it out)
As far as I am able to tell, there are no problems occurring during this process.
Then I have a step where I modify my data as follows ...
-(void)incNumberOfTriesFor:(NSString *)stringOfIndex {
if (self.practiceRecords)
{
int index = [stringOfIndex intValue];
int numberOfTries = [(NSNumber *)(self.practiceRecords[index][#"tries"]) intValue] + 1;
//CRASHING on this next line.
self.practiceRecords[index][#"tries"] = #(numberOfTries);
//message to helper method
[self writePracticeRecords];
}
}
So the first time through (when the array is built and written out) I get a crash at the indicated line.
The error is:
-[__NSDictionaryI setObject:forKeyedSubscript:]: unrecognized selector sent to instance
I quit the app, check the documents folder and see the data file written out with no issues.
I re-run the app, and then get no crash and the data file still looks great.
This is repeatable.
If the data file exists, no crash.
If the data first needs to be created, then a crash.
(In all cases, I manually look inside the resulting data file and see exactly what I expect to see - no issues there)
I'm not sure where to even begin squashing this bug, and would really like to understand the details of why this is happening.
Thanks very much for any help!
Just to recap the correct comments above:
-[__NSDictionaryI setObject:forKeyedSubscript:]: unrecognized selector sent to instance
NSDictionary does not implement any of the set... methods because it is immutable. You state that you're creating with literals syntax when the data is not found on disk. The literal syntax creates immutable containers
Instead, try...
// try to initialize from disk, but if not
// we can still use literal (immutable) syntax, but in a mutable container
self.practiceRecords = [NSMutableDictionary
dictionaryWithDictionary:#{ #"key" : #"value" }];

Unable to append String NSMutableString - Error

I have the following code
NSMutableString *ibmcountryCodeLabel=[[NSMutableString alloc]init];
NSMutableString *imobileNumberValue=[[NSMutableString alloc]init];
ibmcountryCodeLabel=#"555";
imobileNumberValue=#"333";
NSLog(#"Done1");
[ibmcountryCodeLabel appendString:imobileNumberValue];
NSLog(#"Done2");
Though both the string are mutable when I try to append one with another I am getting "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'"
read through all the threads. But not able to find a solution. Kindly help me. Thanks for your time
Here is the problem: this assignment produces a mutable string object:
NSMutableString *ibmcountryCodeLabel=[[NSMutableString alloc]init];
After this assignment, however, the object pointed to by ibmcountryCodeLabel is no longer mutable:
ibmcountryCodeLabel=#"555";
This is because the #"555" constant is an immutable subclass of NSString. Trying to modify it leads to runtime errors.
Change the code as follows to fix this problem:
NSMutableString *ibmcountryCodeLabel=[NSMutableString stringWithString:#"555"];
NSMutableString *imobileNumberValue=[NSMutableString stringWithString:#"333"];

Resources