Note that there are some explanatory texts on larger screens.

plurals
  1. POTraversing a NSMutableArray?
    text
    copied!<p>To start let me tell you I am a total Objective-C beginner. This is my problem:</p> <p>I have a NSMutableArray that stores objects, (Player) that has the name of the player and his/her score.</p> <p>I am able to add objects to the array using <code>addObject</code>, but I am having trouble traversing this array. This is how I do it:</p> <pre><code>// Get the reference to the array NSMutableArray *myarray = [delegate getArray]; // Create a numerator NSEnumerator *e = [myarray objectEnumerator]; id object; while (object = [e nextObject]) { [object printPlayer]; } </code></pre> <p>The method printPlayer belongs to the Player class and it just prints the name and the score. </p> <p>The problem is when I have three players in the array and I am trying to print the content, it reaches this error inside the printPlayer method:</p> <pre><code>Thread 1: EXC_BAD_ACCESS(code=1, address=0x0000008) </code></pre> <p>Strangely if I use <code>NSLog(@"%@", object);</code> instead of <code>[object printPlayer];</code> it prints a reference to the object and does not reach any error.</p> <p>Anyone could point me what could be the problem when I try to use <code>[object printPlayer]</code></p> <p>Cheers</p> <p><strong>Update 1:</strong> This is my printPlayer method:</p> <pre><code>-(void) printPlayer { NSLog(@"\n\nName: %@\nScore: %d", playerName, playerScore); } </code></pre> <p><strong>Update 2:</strong></p> <p>Player.h:</p> <pre><code>@interface PROGPlayer : NSObject @property (nonatomic, assign) NSString *playerName; @property (nonatomic, assign) int playerScore; -(id) init: (NSString *) n; -(void) printPlayer; @end </code></pre> <p>Player.m: </p> <pre><code>#import "PROGPlayer.h" @implementation PROGPlayer @synthesize playerName; @synthesize playerScore; /** * Player's class constructor * @param n Player's name * @param s Player's score */ -init: (NSString *) n { if (!(self = [super init])) return nil; else { playerName = n; playerScore = 0; } return self; } -(void) printPlayer { NSLog(@"\n\nName: %@\nScore: %d", playerName, playerScore); } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload