2

I'm very newby on Objective C. I've read a lot of topics related but couldn't get solution. I'm using a NSMutableArray, 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

rid
63.9k31 gold badges159 silver badges201 bronze badges
asked Jul 16, 2011 at 23:18
1
  • 2
    Hm. You are talking about NSMutableArray but do NSMutableSet alloc... ? Commented Jul 16, 2011 at 23:22

3 Answers 3

3

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);
} 
answered Jul 16, 2011 at 23:23
0

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.

answered Jul 16, 2011 at 23:21
0
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.

answered Jul 16, 2011 at 23:23
1
  • You're welcome. If this solved your question, please tick it as "correct". : ) Commented Jul 17, 2011 at 14:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.