简体   繁体   中英

Object pointer is set to null after object is alloc and init

I have a custom object class (Ship.h and Ship.m), and I am trying to create a new Ship type object with this code:

Ship *PlayerShip = [[Ship alloc] init];

The code is currently in my First view controller.m and under

- (void)viewDidLoad{

but I have tried it in

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

as well.

I create the PlayerShip object just fine with no problems, then I log it with

NSLog(@"Player ship: %@",PlayerShip);

It logs just fine as well.

Then in another part of my code (anywhere other than the place I put it) like for example an NSTimer I try the same NSLog line and it returns

Player Ship: Null

Is the PlayerShip object being deleted for some reason?

I would appreciate any help.

Below is my shipViewController.h

#import <UIKit/UIKit.h>
#import "Ship.h"
@interface ShipViewController : UIViewController{
IBOutlet UILabel *PlayerShipLabel;
IBOutlet UIProgressView *PlayerHullBar;
IBOutlet UIProgressView *PlayerShieldBar;
IBOutlet UILabel *PlayerCreditsLabel;
IBOutlet UIImageView *PlayerShipImage;
IBOutlet UIButton *PlayerRepairBreachButton;
}


@end
Ship *PlayerShip;

And here is ShipViewController.m

#import "ShipViewController.h"

@interface ShipViewController ()

@end

@implementation ShipViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    Ship *PlayerShip = [[Ship alloc] init];
    NSLog(@"Player ship: %@",PlayerShip);
}



- (IBAction)ShieldSwitch:(id)sender {
    NSLog(@"Ship is %@ ",PlayerShip);

}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Here is my Output from the program when the app first starts:

2014-06-12 19:02:27.503 First Game[5193:60b] Created a new ship!
2014-06-12 19:02:27.504 First Game[5193:60b] Player ship: <Ship: 0x8c30f70>

And here is the output from when I press the button to verify PlayerShip:

2014-06-12 19:02:29.472 First Game[5193:60b] Ship is (null) 

There are several problems.

  1. Why do you declare Ship *PlayerShip; in your .h file after the @end statement? That makes it a global variable. Make it an instance variable like the others but putting it in the curly braces of the @interface statement.
  2. In viewDidLoad you create a local variable with the same name as the (soon-to-be) instance variable. Don't declare a new variable. Simply assign the new object to the (soon-to-be) instance variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM