NSMutableArray addObject not working

烈酒焚心 提交于 2019-11-25 22:25:30

问题


I have declared an NSMutableArray *categories in my view controller .h file, and declared a property for it.

In the parser:foundCharacters: method of the NSXMLParser delegate in my .m file, I have this code:

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string  
{  
    if (elementFound)  
    {  
        element = string;  
        [self.categories addObject:element];  
    }  
}

But when I hover over the [self.categories addObject:element] line after stepping into it in debug mode, XCode tells me the size is 0x0, 0 objects. There are 3 elements in my XML file so 3 items should be in the array.

I\'m missing something really obvious and I can\'t figure out what.


回答1:


The "0x0" part is a memory address. Specifically, "nil", which means your mutable array doesn't exist at the time this is being called. Try creating it in your -init method:

categories = [[NSMutableArray alloc] init];

Don't forget to release it in your -dealloc.




回答2:


Initialize an empty array using

categories = [NSMutableArray array];

The array class method are autoreleased so no need to release.




回答3:


@property (Strong, nonatomic) NSMutableArray * yourArray;

you have to use Strong.



来源:https://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!