NSMutableArray remove Object is cloning another object in the Array - ios

I have an strange error after removing an object in a NSMutableArray.
If i remove an object with
[Array removeObjectAtIndex:2];
the array changed to this:
why are there now 2 ContactViewControllers??

lldb lies sometimes. It gets confused and will display the incorrect information. Try doing
po Array
you should see the correct array contents then.

Related

Changes in NSMutableDictionary not reflecting in parent NSDictionary

Question 1)
I am creating a NSMutableDictionary from a NSDictionary using the following code:
_allKeysDictionary = [receivedDictionary mutableCopy];
and I am updating the values from the _allKeysDictionary using the following code:
[_allKeysDictionary setObject:[textField text] forKey:#"field"];
But my parent NSDictionary that is receivedDictionary is not reflecting those changes made in _allKeysDictionary.
I need the values of receivedDictionary to be updated.
Question 2)
I am using
(__bridge CFMutableDictionaryRef)
to keep a pointer to one of the NSDictionary in my JSON response. but when I am trying to regain the above CFMutableDictionaryRef, I am still getting NSDictionary. I don't know what is wrong. I am using the following code to regain the Dictionary from the reference
NSMutableDictionary *getPointedDict = (__bridge NSMutableDictionary*) dataRefValue;
Q1: But the method mutableCopy even says so: A copy is created! The receivedDictionary logically won't be changed.
To update (after you're done):
receivedDictionary = _allKeysDictionary;
or to be safe that no changes can be made later:
receivedDictionary = [NSDictionary dictionaryWithDictionary: _allKeysDictionary];
By the way, you can write:
_allKeysDictionary[#"field"] = textField.text;
Q2: In Obj-C (and many other languages) casting does not change the structure of the object. If it's an NSDictionary, it will always stay an NSDictionary. So the actual cast will work (i.e. the compiler won't tell you), but once you start doing things with the object that are not implemented in the original class, your app will crash or you'll get other undefined results.
But my parent NSDictionary that is receivedDictionary is not reflecting those changes made in _allKeysDictionary
Of course they are not, you made a mutableCopy - that is a copy. You've now got two dictionaries with the same keys and values.
I need the values of receivedDictionary to be updated
If it is an NSDictionary you cannot do this, that is what immutable means. You can replace the reference stored in receivedDictionary to one which references your new mutable dictionary (or an immutable copy of it), but that doesn't do what you wish you have other variables storing references to the original dictionary.
However if the values in the dictionary are themselves mutable then you can alter those values. E.g. if you NSDictionary contains NSMutableArray values then you can change the elements of those arrays, but you cannot change which array is associated with which key in the dictionary.
Q2: You cannot simply cast a reference to an immutable dictionary to make it into a mutable one. Casting doesn't change the referenced object in anyway, it just changes the type the compiler treats it as (and if the type is not compatible with the actual type your program breaks).
It looks like you are unsure about objects, references and mutability; probably time to do some studying.
HTH
MutableCopy & copy both do deep copy on NSDictionary. The only difference is that mutableCopy makes your new dict mutable.
When you do deep copy, you are copying the value, not the reference, which mean whatever thing you do on your new dictionary, it won't affect the old value.
Plus, your original dict is immutable, even if you copy the reference, its value won't change.

Issue related with modifying the value in an NSMutablearray

I am facing an issue with the values in NSMutablearray. I have two NSMutablearray, both are holding the same content using mutablecopy. The issue is that when I modify a value in one array, the corresponding value in the second array also get modified. How to resolove this. Please help me.
mutableCopy copies by reference, not value. So, any change to one of those objects affects for both arrays.
You could realize different methods to overcome this situation.
// first method
nameArray2 = [NSMutableArray new];
[nameArray2 addObjectsFromArray:nameArray1];
// second method
nameArray2 = [[NSMutableArray alloc] initWithArray:nameArray1 copyItems:YES];
Best regards.

NSArray containsObject method inconsistent results

I have an NSMutableArray called selectedUsers to which I am adding objects using a method called addUser. The objects being added are most often of type PLManagedUser (a core data managed object) although sometimes the object could be a string. In any case, if the array already contains the object, I do not want to add it to the array. Here is the code:
- (void)addUser:(id)user withTitle:(NSString *)title {
if (![_selectedUsers containsObject:user]) {
[_selectedUsers addObject:user];
}
}
I have noticed that if I try to add the same user back to back using the above method, the containsObject catches it, and duplicates are not added. However, if I add the same user (with the same memory address) after having added other objects in between, the duplicate will be added.
I am printing the contents of the array each time I add something to confirm that the duplicate objects are in the array.
My question is, is there any obvious reason why containsObject isn't consistently working here?
You need to provide the ability for an object to identify itself as equal to another object of the same class, and to do this you implement the isEqual: and hash methods.
Having said that, the explanation in your question is the opposite of what I would have expected.

NSMutable Array returning unrecognised values

I am using an NSMutable Array as a datasource to UITableView. I am fetching the values from database and adding it to mutable array. When the view loads I populate the table view with the mutable array and it loads fine. Later when I click a cell it throws an error saying unrecognised selector sent to instance
Inside array I am storing a person object with a name property. I am loading all cells with person.name Later when I try to disolay person.name as the user clicks each cell it shows error.
This is how I do
Person *per = (Person*)[my arr objectAtIndex:indexPath.row];
NSlog(#"%#",per.name);
Here it crashes. But I am using the same code inside cellforRowAtIndexPath for displaying the person name.
Sometimes the error says Unrecognised selector name sent to NSArray
again sometime it says Unrecognised selector name sent to
UIDeviceRGBColor
So it means I am not getting correct object back from my NSMutable Array
How can I do this properly?
Thanks
This is due to the array is dealloacated. Make sure that the array is retained properly. In ARC have a strong reference. In non-ARC make sure you retain the array (I recommend to use property).

Core Data object fault - NSManagedObject attributes returns nil

Recently I had a problem with such scenario:
In My NSManagedObject I stored longName attribute, called save on NSManagedObjectContext and after a while I close the application. Save ended without an error.
I restarted the application, I fetched the object and I tried to get [object longName]. The value which was returned was nil. When I stopped there with breakpoint and when I tried to display the value by po [object longName] proper value was returned and printed. ALl next calles to [object longName] in sources where correct. Whenever I don't use breakpoints there the value was always nil.
I think I hit the same problem recently in other application. I fetched the user object which contains NSSet of flights objects. When I tried to use [[flight validFrom] intValue] it returns number 0, but when I tried to print it with #"%#" format I've got (null).
The strange thing is that problem occurred not on every object.

Resources