开发者

NSMutableArray initialization

I'm very newby on Objective C. I've read a lot of topics related but couldn't get solution. I'm using a NSMutabl开发者_JAVA百科eArray, and alloc and init by the following mode:

events = [[NSMutableSet alloc] init];

Is it right? In this way, I can add objects to the array without problems, but when I iterate or to read, I got SIGABRT: unrecognized selector sent to instance. Attempting several modifications, the best I could get was another exception: EXC_BAD_ADDRESS. The line I'm using to read the array is:

Event *event = [events objectAtIndex:1];

Thanks in advance.

Junior


you have said you are using NSMutableArrays, but then initialised a set, try using:

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

sets don't respond to objectAtIndex, because they are unordered. if you want an object out of a set, you can call anyObject on it. or you can use enumeration to go through all objects. eg

id obj=[events anyObject];

or

for(id obj in events){
    NSLog(@"%@",obj);
}        


What's going on in between? Remember that arrays are zero-indexed; there may not be two elements in there. Try using the count method.


events = [[NSMutableArray alloc] init];
[events addObject:event1];
[events addObject:event2]

for (int i = 0; i < [events count]; i++) {
    Event *event = (Event)[events objectAtIndex:i];
    NSLog("Event: %@", event);
    [event release];
}

Like this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜