unique naming of objects as they are added to an NSMutableArray - objective-c

how would i go about uniquely naming an object and adding it to an nsmutablearray most likely in a for loop?

You're confusing objects and variables. Variables have names; objects don't unless you give them some name instance variable. More to the point, the same variable can reference different objects at different times. Given some collection of objects collection:
NSMutableArray *array = [NSMutableArray array];
for (id item in collection) {
[array addObject:item];
}
This will create a mutable array with all the objects in collection, with the item variable pointing to a different object from collection on each iteration of the loop.

If you want to uniquely name instances then instead of an array how about using a NSDictionary? Then you can grab the array of keys from it. Run through this key array to get the name of a particular instance and then use that key to get the actual object instance from the dictionary.

Related

Objective C - difference between dictionaries and arrays?

in Objective C - what is the difference between dictionaries and arrays? Are dictionaries used with key : value (where Key can be any object), and arrays are id : value (where id is an integer)?
thanks
Array
In Objective-C, an array is a type of collection which can store object types. It can store any type of objects. Objects stored in an array are linked to their index number.
eg. if you create an array and insert the first object, it will be stored in "index 0"
and, the index number will keep on increasing from 0,1,2....n
Use "NSMutableArray" to create an array that can be modified.
example,
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:#"Tom"];
[array addObject:#"Cat"];
So, at index 0, you will have "Tom". And, at index 1, you will have "Cat".
Dictionary
In Objective-C, a dictionary is a type of collection that stores "key-value" pairs.
The "key" is of type ID, so you can enter any object as key for a value.
Use "NSMutableDictionary" to create a dictionary that can be modified.
example,
NSMutableDictionary *dictionary = [NSMutableDictionary alloc] init];
[dictionary setObject:#"Tom" forkey:#"name"];
[dictionary setObject:#"Cat" forKey:#"animal"];
The key difference between array and dictionary is the sequence of the objects gets changed in a dictionary, while in an array the sequence of objects stored is SEQUENTIAL.
[EDIT]
Since there has been quite a discussion with regard to this answer, I will make it clear that the array does NOT get re-created as some comments say.
The size of array/dictionary gets dynamically increased to accomodate the new elements in the colletion.

Why does accessing my NSMutableArray become faster once I've added more objects to it?

Something very odd is going on. I populate my array as follows:
self.workingWithItemCollectionArray = [NSMutableArray arrayWithCapacity:self.itemCollectionArray.count];
self.workingWithItemCollectionArray = [[self.itemCollectionArray mutableCopy]autorelease];
It take a mutable copy of the original NSArray and pass it to the NSMutableArray. When accessing the information contained in this array by the click of a UIButton, there is a slight delay in retrieving the information.
But when I then change the original array to add more items, and then pass this onto the mutable array:
NSMutableArray *editedOriginalArray = [NSMutableArray arrayWithArray:self.itemCollectionArray];
[editedOriginalArray addObjectsFromArray:extraObjectsToAdd];
self.itemCollectionArray = [NSArray arrayWithArray:editedOriginalArray];
self.workingWithItemCollectionArray = [NSMutableArray arrayWithCapacity:self.itemCollectionArray.count];
self.workingWithItemCollectionArray = [[self.itemCollectionArray mutableCopy]autorelease];
It is then after this that I am able to press the button and information is accessed instantly (whereas before the button would stay pressed for a very short time).
Any ideas on why this could be?
It has to do with how NSMutableArray is implemented vs NSArray.
Because NSArray is immutable, the objects are literally internally stored in an array, e.g.:
id *objects = malloc(sizeof(id) * count);
However, when you deal with NSMutableArray, you are dealing with instead, a linked list, as NSMutableArray expects many modifications on the array. So, the lookup on a linked list is much longer, because your objects are not stored in a way where there is a set distance in memory between them.
For more information on linked lists, check here.

Creating an array from properties of objects in another array

Is there any convenient way to take an array/set of objects and create a new array/set containing some property of each item in the first array?
For example, an array contains Car objects. I need an array of licensePlates, where each car has an NSObject car.licensePlate.
Currently I just iterate through the first array adding objects to my mutable results array, but was wondering if there is an instantiation method that exists for this (checked the docs for NSArray).
This will return an array containing the value of licensePlate from each item in the myCars array:
NSArray *licensePlates = [myCars valueForKeyPath:#"licensePlate"]
If you want only unique items (for example), you can do something like this:
NSArray *licensePlates = [myCars valueForKeyPath:#"#distinctUnionOfObjects.licensePlate"];
For more possibilities, see the Collection Operators documentation in the Key-Value Coding Programming Guide.

taking out objects from NSMutableArray

I have an array which contains objects some may be same and some are different.
How can I take each same objects and different objects separately ?
Below is the array
NSMutableArray *items = [[NSMutableArray alloc]
initWithArray:[NSArray arrayWithObjects:#"rat", #"rat", #"cat",#"Lion", #"cat", #"dog", #"dog", nil]];
I want to have four arrays which will contains these items :
First array with two rats
2nd array with two cats
3rd array with one lion
4th array with two dogs
What could be the best way to take the objects out ? Identical object should be placed in same array.
Here's a general answer:
Put the array into an NSCountedSet - that will store each object and a count of the number of times it has been added.
Then - for each object in this counted set create an array with that object repeated according to the count of each object.
It will work in your case, because you are using static strings, which are going to be the same if they are the same string. This will take more work if you are using custom objects.
But the real question we have to ask is why you need to create these repetitive structures. If we could know what you are doing with it, we could give you better advice about how to go about it. For example, if you just need to keep a running count of the number of each type of object you have, you could just use the NSCountedSet directly (it descends from NSMutableSet, so it is already mutable) and not bother with creating the arrays.

objective-c: array enumeration using keypath

I have an array of Person objects (which has a number of attributes). I want to make another array with just the Person's "fullname" attribute. Is there a simple way to do this, other than the obvious one: iterate over the original array, and copy over the fullname one-by-one into another array? Can we do this initWithArray: and tell it to use the object's fullname property when copying?
NSArray indeed has built-in method for that:
NSArray *nameArray = [personArray valueForKey:#"fullname"];