开发者

Get & Edit NSMutableArray from different class file

开发者 https://www.devze.com 2023-02-15 17:38 出处:网络
I am trying to access and change a array from a different class file. When using a NSLog, I get a result of (null). Below is my code:

I am trying to access and change a array from a different class file. When using a NSLog, I get a result of (null). Below is my code:

RootViewController.h

NSMutableArray *listOfItems;
@property (nonatomic, retain) NSMutableArray *listOfItems;

RootViewController.m

@synthesize listOfItems;
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:@"One"];
[listOfItems addObject:@"Two"];
[listOfItems addObject:@"Three"];

SecondViewController.m

RootViewController *test = [[RootViewController alloc] init];
NSLog(@"Results: %@", test.listOfItems);

I get the following results in my console: Results: (null)

Thanks in advance, Coulton

P.S. Obviously I have left out a bunch of code. I just tried to make it easier to read. If you need to see anything else, I would be more than happy to post more. Just ask

EDIT #1:

I am getting hundreds of NSLog Messages that look something like this:

*** __NSAutoreleaseNoPool(): Object 0x4e39020 of class __NSArrayI autoreleased with no pool in place - just leaking

And here's my init cod开发者_JAVA百科e:

- (id) init {

//NSLog(@"%@", theUserID);
// Set up database connection
NSString *myDB = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"];
database = [[Sqlite alloc] init];
[database open:myDB];

//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];

// Add to array to display in the tableView
NSArray *listOfItemsTwo = [database executeQuery:@"SELECT * FROM albums"];  
for (NSDictionary *rowone in listOfItemsTwo) {
    NSString *getName = [rowone valueForKey:@"name"];
    if (getName != NULL) {
        [listOfItems addObject:getName];
        [getName release];
    }
}
return self;

}


I guess you reversed RootViewController.m and RootViewController.h snippets right?

Are you sure that the

listOfItems = [[NSMutableArray alloc] init];

gets called? Maybe you can put a breakpoint there.

EDIT: Order of RootViewController.m and RootViewController.h has been fixed in the question. It's not clear from the question where the above line is in the code. That's a important piece of information.

EDIT2: Example of init method.

@implementation RootViewController
- (id) init
{
    listOfItems = [[NSMutableArray alloc] init];
    [listOfItems addObject:@"One"];

    return self;
}
@end
0

精彩评论

暂无评论...
验证码 换一张
取 消