I've gone crazy trying to find a leak with an NSMutableArray:
NSMutableArray *mutablearray =[[[[NSMutableArray alloc] initWithArray: array] mutableCopy] autorelease];
Finally I understood that I need to autorelease twice my mutablearray because initWithArray is +1 and mutableCopy is +1 too.
Then I'm doing:
NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array] autorelease];
mutablearray = [[mutablearray mutableCopy] autorelease];
But, it's correct to do?:
NSMutableArray *mutablearray = [[[[[NSMutableArray alloc] initWithArray: array] autorelease] mutableCopy] autorelease];
Thanks
The first question comes in mind is WHY you are initializing and also making a mutable copy at the same time initWithArray gives a new instance with new memory and you can use it.Then why creating a mutable copy of it?
Use
NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array]autorelease];
This gives you a mutable instance .So no need for calling mutablecopy anyway
OR
NSMutableArray *mutablearray = [[array mutableCopy]autorelease];
Related
Can someone help me out on this:
Im creating a property in my TableVC.m file :
#property NSMutableArray *savingBeaconSpecs;
In my Viewdidload I instantiate the array:
NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];
Now I do requests to the server, and I want to save the returned JSON into objects and save these each time in the array. So I did the following in the ConnectionDidFinishLaunching:
self.artworkArray = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:&err];
NSLog(#"Log ArtworkArray in ConnectionDidFinishLoading%#", self.artworkArray);
And:
Artwork *artwork = [[Artwork alloc]init];
artwork.title = [self.artworkArray valueForKey:#"name"];
artwork.artist = [[self.artworkArray objectForKey:#"artist"] valueForKey:#"name"];
artwork.CreationYear = [self.artworkArray valueForKey:#"creationYear"];
artwork.categorie = [[self.artworkArray objectForKey:#"exposition"] valueForKey:#"name"];
Now I want to save this object into the savingBeaconSpecs NSMutableArray
[self.savingBeaconSpecs addObject:artwork];
But the NSMUtableArray savingBeaconSpecs always returns 0 when i try log his content
Anyone please?
Because you declare it locally in your viewDidLoad :
NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];
you should use
self.savingBeaconSpecs = [[NSMutableArray alloc]init];
and
[self.savingBeaconSpecs addObject:artwork];
and declare your property as (without the first capital S)
#property NSMutableArray *savingBeaconSpecs;
To instantiate the array, you should do:
self.savingBeaconSpecs = [[NSMutableArray alloc] init];
or equally good:
self.savingBeaconSpecs = [NSMutableArray array];
NSMutableArray *thisArray = [[NSMutableArray alloc] initWithObjects:#"one", #"two", #"three", nil];
Do I need to delete any objects before deleting thisArray?
thisArray = NULL;
A NSArray/NSMutableArray only keeps a reference to the objects it contains, and by setting that array to NULL it will automatically releases all those references. You don't have to cycle through them all to release them manually.
I been searching for this but failed to find a proper solution. Is it possible to create an array for each object present in an array?Lets say I have an array 'fruits'
NSMutableArray *fruits=[[NSMutableArray alloc]init];
[fruits addObject:#"apple"];
[fruits addObject:#"banana"];
[fruits addObject:#"mango"];
Now there are three objects in my array. IS it possible to create an array for each of the object present in the array 'fruit'.
Can I do something like
for(int i=0;i<fruits.count;i++){
NSMutableArray *fruits_%d=[[NSMutableArray alloc]init];
}
I know it is a blunder. Is there any way I can do that?
Thanks in advance.
You could use a dictionary instead:
NSMutableDictionary *fruitDict=[[NSMutableDictionary alloc]init];
[fruitDict setObject:[[NSMutableArray alloc]init] forKey:#"apple"];
[fruitDict setObject:[[NSMutableArray alloc]init] forKey:#"banana"];
[fruitDict setObject:[[NSMutableArray alloc]init] forKey:#"mango"];
Or a little cleaner syntax:
NSMutableArray *fruits=[[NSMutableArray alloc]init];
[fruits addObject:#"apple"];
[fruits addObject:#"banana"];
[fruits addObject:#"mango"];
NSMutableDictionary *fruitDict = [[NSMutableDictionary alloc] init];
for (NSString *fruit in fruits)
{
[fruitDict setObject:[[NSMutableArray alloc]init] forKey:fruit];
}
Then when you want to retrieve the array:
NSMutableArray *myArray = fruitDict[#"banana"]
The above code will yield the array for the banana item.
I don't know what exactly you want but maybe you can do something like this:
NSMutableArray *fruits = [[NSMutableArray alloc] init];
[fruits addObject:#"apple"];
[fruits addObject:#"banana"];
[fruits addObject:#"mango"];
NSMutableArray *arrayOfFruits = [[NSMutableArray alloc] initWithCapacity:fruits.count];
for (int i = 0; i < fruits.count; i++) {
[arrayOfFruits addObject:#[fruits[i]]];
}
What is the difference between
NSMutableArray* p = [[NSMutableArray alloc] initWithObjects:...]
and
NSMutableArray* p = [NSMutableArray arrayWithObjects:...]
In the first one, you have the ownership of array object & you have to release them.
NSMutableArray* p = [[NSMutableArray alloc] initWithObjects:...];
[p release];
& last one you dont need to release as you don't have the ownership of array object.
NSMutableArray* p = [NSMutableArray arrayWithObjects:...]; //this is autoreleased
If you call release in this, then it will crash your application.
[NSMutableArray arrayWithObjects:] is the same as [[[NSMutableArray alloc] initWithObjects:] autorelease]
In practice, there is no difference if you're on ARC.
The latter basically is just a shorthand for [[NSMutableArray alloc] initWithObjects: ...], except the returned array is autoreleased (which is important if you're still doing manual reference counting).
What I think the difference is that: initWithObjects is a instance method, and arrayWithObject is a class method.
This is very basic but I don't know how to do it.
How can a NSMutableArray object be cleared from all elements. I want to set the array to the state when it was created:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
Thanks
[myArray removeAllObjects];
Docs can be found here