0

I have defined this printDetails method for the Friend and Foe objects which inherit the Character class.

-(void) printDetails
{
NSLog (@"\n%@: \nStrength is %ld \nIntelligence is %ld \nSpell is %@\n\n", self.name, self.strength, self.intelligence, self.spell);
}

In main I print my array "characters"

  NSLog(@"%@", characters);

But my output comes out as what looks like a series of memory addresses.

(
"<Friend: 0x100204aa0>",
"<Friend: 0x100600320>",
"<Friend: 0x100600170>",
"<Foe: 0x100500330>",
"<Foe: 0x100205b50>",
"<Foe: 0x100102270>"
)
1
  • Override -(NSString *)description of the class Friend and Foe, and/or Character. Commented May 8, 2017 at 16:13

2 Answers 2

1

It appears that you are never calling your printDetails function in your main. The result you are seeing are the memory addresses of the pointers to the characters in the NSArray.

Not the most elegant solution, but to test your function, you can simply loop over the elements and call printDetails on each (granted your function is local to the Character class.

for (Character *character in characters) {
    [character printDetails];
}

This should output the result of your function in the console.

Sign up to request clarification or add additional context in comments.

2 Comments

Friend, Foe and Character all have the printDetails method. But the version in Friend and Foe should override the one in Character because they print slightly different attributes. I couldn't get your suggestion to work. I did get it to work with the following code though. int num = 1; for (Character *theCharacter in characters) { NSLog (@"%d : ", num++); [theCharacter printDetails]; }
@Lee The code you just posted is exactly the same, with the exception of printing out an arbitrary counter. And assigning a type to the element, I've been in swift too long ;) I will update my answer, but the integer display has nothing to do with the actual solution. Also, don't try to use that num variable as a subscript accessor to your array, Objective-C is 0 indexed, and you would end up getting a runtime-exception on the final element.
0

I got it to work with the following code.

int num = 1;
for (Character *theCharacter in characters)
{
    NSLog (@"%d : ", num++);
    [theCharacter printDetails];
}

Comments

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.