简体   繁体   English

对象分配和初始化后,对象指针设置为null

[英]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.h和Ship.m),并且我正在尝试使用以下代码创建一个新的Ship类型对象:

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

The code is currently in my First view controller.m and under 该代码当前在我的First view controller.m中

- (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 我创建了PlayerShip对象,没有任何问题,然后用

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 然后在代码的另一部分(我放置它的地方以外的地方),例如NSTimer,我尝试使用相同的NSLog行并返回

Player Ship: Null 玩家飞船:空

Is the PlayerShip object being deleted for some reason? 是否出于某种原因删除了PlayerShip对象?

I would appreciate any help. 我将不胜感激任何帮助。

Below is my shipViewController.h 以下是我的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 这是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: 这是我按下按钮验证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; 为什么要声明Ship *PlayerShip; in your .h file after the @end statement? @end语句的.h文件中? 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. 使其与其他实例变量一样,但将其放在@interface语句的花括号中。
  2. In viewDidLoad you create a local variable with the same name as the (soon-to-be) instance variable. viewDidLoad您将创建一个与(即将成为)实例变量同名的局部变量。 Don't declare a new variable. 不要声明新变量。 Simply assign the new object to the (soon-to-be) instance variable. 只需将新对象分配给实例实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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