Search code examples
iosxcodenslog

NSLog not working when I make a method call in objective c


I'm working on my first ground-up iOS app. I'm sure I'm doing something wrong here, but I can't seem to ferret out the problem.

I have an app with some IBActions in it, which do two things:

1) Outputs some simple text via NSLog (to let me know the action worked) - this works okay (it outputs my text via NSLog)

2) Makes a call to a method in a custom class which should also output an NSLog statement. - this doesn't work (no text output via NSLog)

I'm also struggling a bit with where to create the instances of my classes so that they are accessible elsewhere in my code.

Here's the code:

//
//  ViewController.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Player.h"
#import "Orc.h"

@interface ViewController : UIViewController {
    // Variables that you want to access globally go here?
    Player *wizard;
    Orc *grunty;

}

-(IBAction)takePie:(id)sender;
-(IBAction)castFireball:(id)sender;
-(IBAction)attack:(id)sender;



@end

and

//
//  ViewController.m
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import "ViewController.h"
#import "Player.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)takePie:(id)sender{
    // Player attempts to take the pie
    // If Orc's health is > 0, don't let the player take the pie
    // If the Orc's health is <= zero, let the player take the pie
    NSLog(@"IBAction - player attempted to take the pie.");
    [wizard takePie];

}

-(IBAction)castFireball:(id)sender{
    NSLog(@"IBAction - player cast fireball.");
    [wizard castFireball];
}
-(IBAction)attack:(id)sender{
    NSLog(@"IBAction - player attacked.");
    [wizard attackOrc];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Player *wizard = [[Player alloc]init];
    Orc *grunty = [[Orc alloc]init];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

and

//
//  Player.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Player : NSObject

@property int maxHealth;
@property int currentHealth;
@property int armorClass;
@property int meleeToHitModifier;


-(void)setHeathBar;
-(void)attackOrc;
-(void)castFireball;
-(void)takePie;


@end

and

//
//  Player.m
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import "Player.h"

@implementation Player

@synthesize maxHealth;
@synthesize currentHealth;
@synthesize armorClass;
@synthesize meleeToHitModifier;

-(void)setHealthBar {
    NSLog(@"Player health is 15");
    return;

}

-(void)attackOrc {
    // roll a d20
    // add the "to hit" modifier
    // compare the result to the orc's armor class
    NSLog(@"Player - Player attached the Orc");
    return;

}
-(void)castFireball{
    NSLog(@"Player - Player casts Fireball");
    return;

}
-(void)takePie{
    NSLog(@"Player - Player attempts to take pie");
    return;

}

@end

Note: I also have an Orc class defined, but it looks fairly identical to the player class shown.

Thanks in advance for the help!

-- Eddie


Solution

  • Your ViewController.h (which you should call by some more distinctive name, FYI) should look like this:

    //
    //  ViewController.h
    //  OrcAndPie
    //
    //  Created by me on 4/27/13.
    //  Copyright (c) 2013 me. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "Player.h"
    #import "Orc.h"
    
    @interface ViewController : UIViewController
    
    @property Player *wizard;
    @property Orc *grunty;
    
    -(IBAction)takePie:(id)sender;
    -(IBAction)castFireball:(id)sender;
    -(IBAction)attack:(id)sender;
    
    @end
    

    Then in your ViewController.m, your viewDidLoadMethod should look like:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.wizard = [[Player alloc]init];
        self.grunty = [[Orc alloc]init];
    
    }
    

    This should at least ensure you have an instantiated instance of Player that you can call methods on, so your NSLog should be reached.