Reputation: 13
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)
Upvotes: 1
Views: 605
Reputation: 318804
There are several problems.
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.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.Upvotes: 2