I have made a very simple NSObject:
GameSetUpData.h
@interface GameSetUpData : NSObject
@property (readwrite, nonatomic) NSUInteger numberOfPlayers;
@property (strong, nonatomic) NSMutableArray *playerNames;
@property (strong, nonatomic) NSString *gameType;
@property (readwrite, nonatomic) NSUInteger numberOfMinutes;
@property (readwrite, nonatomic) NSUInteger numberOfTurns;
@property (readwrite, nonatomic) CGSize boardSize;
@end
GameSetUpData.m
#import "GameSetUpData.h"
@implementation GameSetUpData
@synthesize numberOfPlayers = _numberOfPlayers;
@synthesize playerNames = _playerNames;
@synthesize gameType = _gameType;
@synthesize numberOfMinutes = _numberOfMinutes;
@synthesize numberOfTurns = _numberOfTurns;
@synthesize boardSize = _boardSize;
@end
This class basically just holds data. I then try to use this object in my viewcontroller:
MainMenu.h
#import <UIKit/UIKit.h>
@class GameSetUpData;
@interface MainMenu : UIViewController
@property (strong, nonatomic) GameSetUpData *gameSetUp;
-(IBAction)tappedNewGame:(id)sender;
-(IBAction)tappedTwoPlayers:(id)sender;
...
MainMenu.m
#import "MainMenu.h"
#import "MJViewController.h"
#import "GameSetUpData.h"
@implementation MainMenu
@synthesize gameSetUp = _gameSetUp;
...
-(IBAction)tappedTwoPlayers:(id)sender {
_gameSetUp.numberOfPlayers = 2;
NSLog(@"number of Players: %d", _gameSetUp.numberOfPlayers);
}
Unfortunately, my NSLog says that numberOfPlayers is equal to 0. What is wrong with my GameSetUpData? I was told that in iOS5 we do not need to call alloc/init or make a dealloc method. Do I still need a -(void)init method in GameSetUpData. Thank you all for your time!
Edit: Please alloc/init your objects — ARC ONLY deals with release/retain/autorelease. You still need to make an instance of an Object! I apologize for the mis-information. I will make sure to RTFM next time…
Of course you have to alloc/init your object. How should the compiler know when to do that? With ARC you just don’t need to retain or release.
Add
_gameSetUp = [[GameSetUpData alloc] init];somewhere.