Having problems with adding objects to NSMutableArray

后端 未结 3 1855
予麋鹿
予麋鹿 2021-01-27 12:03

I am having problem with adding objects to NSMutableArray *array.

//  Controller.m
#import \"Controller.h\"
@implementation Controller
- (void)parser:(NSString          


        
相关标签:
3条回答
  • 2021-01-27 12:18

    NSMutabaleArray starts at index 0

    0 讨论(0)
  • 2021-01-27 12:27

    First off, you're over-retaining the array.

    Second, you didn't provide the code for initializing the array, so I guess it's not allocated and initialized. This will cause the code to message a nil object and thus return nil.

    You should create an init method for the Controller object, and allocate a new NSMutableArray object (and retain it).

    Also, a proper dealloc to release the array.

    0 讨论(0)
  • 2021-01-27 12:28

    Here is the method I added to Controller class:

    - (id)init {
        self = [super init];
        if(self){
            array = [[NSMutableArray alloc] init];
        }
        return self;
    }
    - (void)dealloc {
        [array release];
        [super dealloc];
    }
    
    0 讨论(0)
提交回复
热议问题