0

NSLog is returning the output 'Null" instead of a string that I would have expected. I suspect that this is a problem with private instance variables and such, but since I am not familiar with Object-oriented programming I cannot determine the cause.

//The viewDidLoad method in MainGameDisplay.m:
- (void)viewDidLoad
{
    [super viewDidLoad];
    Engine *engine = [[Engine alloc] init];
    [engine setPlayerName: viewController];
}

The string is entered by a UITextField, the property being

//ViewController.h
 @property (strong, nonatomic) IBOutlet UITextField *PlayerNameTextView;

The method works fine and returns the correct string if [engine setPlayerName: self] is placed into ViewController, but anywhere outside the location that *PlayerNameTextView is causes this problem.

//Engine.m
@implementation Engine
{
    ViewController *firstPage;
}

NSString *Player;

-(void) setPlayerName: (ViewController *) name
{
    Player = [[name PlayerNameTextView] text];
    NSLog(@"%@", Player);
}
yeesterbunny
  • 1,847
  • 2
  • 13
  • 17
PappaSmalls
  • 349
  • 2
  • 13

2 Answers2

0

NSLog return type is void as you can see in it's documentation. There is no reason to expect any return value for a call to it, since it does not return anything.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
0

Make sure that 'name' is properly initialized. Try putting an assert(name != nil) right before the NSLog. Or better yet, set a breakpoint at the NSLog and inspect the variables.

Another suggestion: Why not make the method -(void) setPlayerName:(NSString*)name? This is more straightforward than passing around pointers to view controllers, and would be easier to debug.

boxel
  • 639
  • 5
  • 14
  • 2
    Better to make sure that "name" isn't nil. We already know that "Player" is nil, and assert(Player != nil) adds no information. – Hot Licks Nov 06 '12 at 03:32
  • This can't be the reason because if I run setPlayerName in ViewController itself, the program works perfectly. – PappaSmalls Nov 06 '12 at 08:31