• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSMutableArray Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableArray Question


  • Subject: Re: NSMutableArray Question
  • From: Roarke Lynch <email@hidden>
  • Date: Fri, 31 Oct 2003 03:47:08 -0500

On Oct 31, 2003, at 3:06 AM, John Mullins wrote:

Hey,
This is a dumb question but I haven't found any good examples of this
on the internet, and I learn best by example than reference.

I have a class...

@interface Sample : NSObject {
AudioFilePlayID mPlayID;
}

@end

And I would like to make an NSMutableArray of a group of Sample
classes. I can't really figure out how to make an NSMutableArray with
objects. And how would I go about iterating through them.

NSMutableArray * mySampleArray = [NSMutableArray arrayWithObjects: obj1, obj2, obj3, nil];
or without using the class method (i.e making an object that is not autoreleased)
NSMutableArray * mySampleArray = [[NSMutableArray alloc] initWithObjects: obj1, obj2, obj3, nil];

Notice that the list of objects to initialize your array with should be a comma separated and nil terminated list.

as to iterating, you can do it two ways simply:

NSEnumerator * enumerator = [mySampleArray objectEnumerator];
Sample * currentObject;

while(currentObject = [enumerator nextObject]) {
// do something
}

nextObject will return nil at the end of the array
oh yeah, and nextObject returns as id type, i only set currentObject to your Sample class b/c i knew what i'd be getting back

-or-

Sample * currentObject
int index = 0;

for(index = 0; index < [mySampleArray count]; index++) {
currentObject = [mySampleArray objectAtIndex:index];
//do something
}

the same issue applies, objectAtIndex returns id, but again I knew i'd be getting back a Sample

And once I have like ObjectAtIndex: how do I call mPlayID from it?? I
need to pass mPlayID as a reference.. like &sample.mPlayID would be in
C.... to a method.

as to this, just add an accessor method, i.e...

@interface Sample : NSObject {
AudioFilePlayID mPlayID;
}
- (AudioFilePlayID *)playID;
@end

@implementation Sample

- (AudioFilePlayID *)playID {
return &mPlayID;
}
@end

then just call:

[[mySampleArray objectAtIndex:0] playID];

or something along those lines.

Roarke Lynch
-------------------------------
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >NSMutableArray Question (From: John Mullins <email@hidden>)

  • Prev by Date: XCode or code problems?
  • Next by Date: Re: PB under Panther
  • Previous by thread: NSMutableArray Question
  • Next by thread: Re: NSMutableArray Question
  • Index(es):
    • Date
    • Thread