NSMutableArrays

I have NSMutableArray

both a member variable for a class.

In the file .h

:

@interface bleh {
NSMutableArray *list;
}

@property (readonly, assign) NSMutableArray *list;

@end

      

In the file .m

:

@implementation bleh
@synthesize list;
-(void)init;
{
    list = [NSMutableArray arrayWithCapacity:30];
}
@end

      

Now I'm not an objective-C programmer, so maybe I'm missing some nuances, but when I do the following:

NSMutableString *listItem = [NSMutableString stringWithString:@"Foobar"];
[list addObject:listItem];

      

I am getting strange behavior. Namely, I use this to save a list of files that I ultimately want to attach to an email and then open the collector. I am getting SIGABRT and after debugging I find out that whenever I work on the list, I get nothing. AddObject messages don't grow in size NSMutableArray

at all.

Am I missing something? Can anyone show me a complete implementation of customization NSMutableArray

for control inside an Objective C class?

Thank.

PS. Suppose I am smart enough to put manipulations with NSMutableArray

inside a member function for a class containing a member variable.

+2


source to share


8 answers


How do you actually create your array? Is it possible that it is auto-implemented and leaves? Remember, if you create it with a convenience method (for example, array

or whatever), you need retain

it.



+3


source


in the latest SDK, arrayWithCapacity is bad practice. but in your code you are creating an array that no one owns, mask your array properly.

don't forget to initialize the array



NSMutableArray *array = [[NSMutableArray alloc] init];

fix (readonly, assign),

+4


source


You are creating an array c arrayWithCapacity:

that returns an array that you don't have and you never claim ownership of it. Use the accessor property to save the array:

self.list = [NSMutableArray arrayWithCapacity:30];

      

I would recommend reading the Cocoa docs on memory management . Once you know the rules there, it will be clear what to do in such a situation. They are not very difficult, but they are very necessary if you are going to program Cocoa.

+3


source


Your variable list

was automatically released and de-allocated, so your program crashes when you try to access it.

There are two ways to create objects in Cocoa:

NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* array2 = [NSMutableArray arrayWithCapacity:10];

      

array1

was created using alloc + init, so you own it. It will stick until you do release

.

array2

was not created using alloc + init, so you don't own it. You are not responsible for releasing it, but it will go away on its own. You have to save array2

if you want it to stick.

+3


source


Your property declaration list

does not allow you to save correctly NSMutableArray

. By calling arrayWithCapacity

, you are effectively putting the array in the autostart pool, which means it can be freed at any time if the object is not interested in maintaining it. Until you, the way you stated does not reflect this:

@property (readonly, assign) NSMutableArray *list;

      

The above expression just sets that pointer as a copy of another pointer - there is no memory management for you. Instead, it should read:

@property (readonly, retain) NSMutableArray *list;

      

... and you have to assign the list like this:

self.list = [NSMutableArray arrayWithCapacity:64];

      

Since you are specifying an attribute retain

on a property, whenever a new value is assigned to it, a message retain

will be posted to that new value, telling the memory manager that you do not want this object to be freed. To get this full circle out, you need release

an object when the class containing the class is deallocated:

- (void)dealloc
{
    [list release];
    [super dealloc];
}

      

+3


source


Are you initializing your list correctly? Those. do you have something like the following in your code?

list = [[NSMutableArray alloc] init];

      

+1


source


The problem ehre (assuming you initialize your array properly) could be that @"Foobar"

assings a NSString

not a NSMutableString

, so its failing because if different types you have to do

 NSMutableString *listItem = [NSMutableString stringWithString:@"Foobar"];
    [list addObject:listItem];

      

or

  NSString *listItem =@"FooBar";
        [list addObject:listItem];

      

+1


source


It doesn't look like you actually initialized NSMutableArray

.

In the init event of the object, just say

 [self setList:[[NSMutableArray alloc] initWithCapacity:10]]];

      

(I would just say init, but I don't remember if that works. It doesn't matter what power you start with)

Before actually allocating the array, the "list" variable will be nil.

0


source







All Articles