1

So i have this block of code it adds players to an NSMutableArray in my ViewController playerList. For some reason i cannot print all the playernames to the log. Am I doing something wrong? I keep getting an error that says member refrence struc objc_object is a pointer. Can anyone see what im doing wrong?

p1,p2,p3,p4 are all NSString Objects that just have the players names.

the addPlayer method creates a new player object with a property named playerName.

- (IBAction)addPlayerButton:(id)sender {
    [self.playerList addObject:[self addPlayer:p1]];
    [self.playerList addObject:[self addPlayer:p2]];
    [self.playerList addObject:[self addPlayer:p3]];
    [self.playerList addObject:[self addPlayer:p4]];
    for (id element in playerList) {
        NSLog(element.playerName);
    }
}

2 Answers 2

4
for (id element in playerList) {
    NSLog(element.playerName);
}

The compiler warning/error is because element is of type id and you can't use the dot syntax with object references of type id (a specific design choice when creating that feature, btw).

Fixed code:

for (Player *element in playerList) {
    NSLog(@"%@", element.playerName);
}

Two (unrelated) problems fixed:

  1. explicitly type element to be a reference to your player class (I assumed the name). This'll allow the dot syntax to work.

  2. Use a format string with NSLog. If a player's name were ever to contain a formatting sequence -- %@, for example -- then NSLog() would try to expand the next (non-existent) argument to NSLog and your app would crash or print garbage (say, if the player's name were "Bob %f %f %f").


doesnt look like they are getting added to the array properly

Make sure you allocate an array and assign it to playerList somewhere:

self.playerList = [NSMutableArray array];
Sign up to request clarification or add additional context in comments.

1 Comment

you are right, the array is null, i made it a property of my view controller, thats ok right?
0

Use this instead:

NSLog(@"%@", element.playerName);

NSLog is sort of like printf() and friends, but not exactly. You must provide a first argument that is a string literal with the format you want to use, followed by any variables represented in the format. In Objective-C, the special format %@ means "use the obeject's description method to fill in a value (if there is one)." Sometimes you get a debugger-like output for an object that does not have that method, e.g. or some such, which isn't too useful of course.

In your case, assuming playerName is an NSString, you'll see it's name output if you use the format %@ in NSLog's first argument.

EDIT:

You should be able to use a for statement like this:

for(Player *p in playerList) {
    NSLog(@"%@", p.playerName); 
}

Just because you use addObject: to add the objects doesn't mean you have to give up using the objects' type when you look at them from the array.

If in fact the objects in playerList are just NSStrings, then your loop can simply be

for(NSString *name in playerList) {
    NSLog(@"%@", name); 
}

5 Comments

I tried that but its still doing the same thing, it wants me to replace the . with a ->
Down voted? Really? I think the essence of the problem is that the OP doesn't fully understand some basics here about objects and working with them. I think my answer provides quite a bit of detail and clarity to what is ultimately a simple issue. Whatever.
i didnt downvote. ok, so this actually stopped the error but now its not printing. if i put an NSLog in my initalizer i see that the objects are being created but it doesnt look like they are getting added to the array properly.
Down voted because I saw the answer prior to the edit. Note that you need a @ in front of those constant strings. And the formatting string is %@ not @%, which could also be the reason for a lack of output.
@bbum Thanks for the explanation (and for undoing it). My fingers are too fast for my brain sometimes. :-)

Your Answer

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.