Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
Apple Mobile Products: iPhone, iPad, iPod
iOS and Apps
Help with Obj C - creating multiple objects
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="capturenow" data-source="post: 1300162" data-attributes="member: 215881"><p>As newbie, Im stuck on getting the values from objects I have put in a NSMutableArray. To give a full picture of my problem I would appreciate you reading the following:</p><p></p><p>So lets say I produce a new object as follows (writing to console):</p><p></p><p>Person *player = [[Person alloc] init];</p><p></p><p>player.age = 10;</p><p>player.height = 10;</p><p>player.name = @"player";</p><p></p><p>As I continue to write my programme I can change the above values of player (not at run time) i.e</p><p></p><p>player.age = 23; etc.....</p><p></p><p>If I want to create another Person (player2) I repeat the above like this:</p><p></p><p>Person *player1 = [[Person alloc] init];</p><p></p><p>player1.age = 13;</p><p>player1.height = 4;</p><p>player1.name = @"player1";</p><p></p><p>In my programme I can now change and compare values of the 2 objects i.e.</p><p></p><p>if (player.age == player1.age) bla bla bla</p><p></p><p>My problem starts if I want to create 20+ Person objects - I know how to place all the objects in a loop/NSMutableArray as follows:</p><p></p><p>for (int i = 0; i < 20; i++)</p><p></p><p>{ Person *player = [[Person alloc] init];</p><p></p><p>player.age = 10;</p><p>player.height = 10;</p><p>player.name = @"player";</p><p>[myArray addObjectlayer]; </p><p>[player release]; </p><p>}</p><p></p><p>All the objects in myArray are individual but have the same values. Thats fine ! But how to get or change the objects in myArray ? (not at run time)</p><p></p><p>If [myArray objectAtIndex:4]; holds the following values:</p><p></p><p>player.age = 10;</p><p>player.height = 10;</p><p>player.name = @"player";</p><p></p><p>how do I get to the object(s) in myArray so I can compare/sort/add etc..</p><p></p><p>Heres example of what I want to do but I know is wrong:</p><p></p><p>if (player.age == [myArray personAtIndex:15.age]; // wrong !</p><p></p><p>or</p><p></p><p>NSLog(@"@ has a height of %i",[myArray personAtIndex:15:name:height]; </p><p></p><p>prints out >> person has a height of 10</p><p></p><p>I really would appreciate you helping me on this - As a newbie I'm finding it hard to move on until I get an understanding of this problem. Thanks again.</p></blockquote><p></p>
[QUOTE="capturenow, post: 1300162, member: 215881"] As newbie, Im stuck on getting the values from objects I have put in a NSMutableArray. To give a full picture of my problem I would appreciate you reading the following: So lets say I produce a new object as follows (writing to console): Person *player = [[Person alloc] init]; player.age = 10; player.height = 10; player.name = @"player"; As I continue to write my programme I can change the above values of player (not at run time) i.e player.age = 23; etc..... If I want to create another Person (player2) I repeat the above like this: Person *player1 = [[Person alloc] init]; player1.age = 13; player1.height = 4; player1.name = @"player1"; In my programme I can now change and compare values of the 2 objects i.e. if (player.age == player1.age) bla bla bla My problem starts if I want to create 20+ Person objects - I know how to place all the objects in a loop/NSMutableArray as follows: for (int i = 0; i < 20; i++) { Person *player = [[Person alloc] init]; player.age = 10; player.height = 10; player.name = @"player"; [myArray addObjectlayer]; [player release]; } All the objects in myArray are individual but have the same values. Thats fine ! But how to get or change the objects in myArray ? (not at run time) If [myArray objectAtIndex:4]; holds the following values: player.age = 10; player.height = 10; player.name = @"player"; how do I get to the object(s) in myArray so I can compare/sort/add etc.. Heres example of what I want to do but I know is wrong: if (player.age == [myArray personAtIndex:15.age]; // wrong ! or NSLog(@"@ has a height of %i",[myArray personAtIndex:15:name:height]; prints out >> person has a height of 10 I really would appreciate you helping me on this - As a newbie I'm finding it hard to move on until I get an understanding of this problem. Thanks again. [/QUOTE]
Verification
Post reply
Forums
Apple Mobile Products: iPhone, iPad, iPod
iOS and Apps
Help with Obj C - creating multiple objects
Top