我做了一个非常简单的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
这个类基本上只保存数据。然后我尝试在我的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);
}
不幸的是,我的NSLog说numberOfPlayers等于0.我的GameSetUpData出了什么问题?我被告知在iOS5中我们不需要调用alloc / init或者使用dealloc方法。我还需要GameSetUpData中的-(void)init
方法吗?谢谢大家的时间!
编辑:请分配/初始化您的对象 - ARC仅处理发布/保留/自动释放。你仍然需要创建一个Object的实例!我为错误的信息道歉。我下次会确保RTFM ......
答案 0 :(得分:2)
当然你必须分配/初始化你的对象。编译器应该如何知道何时这样做?使用ARC,您无需保留或释放。
在某处添加_gameSetUp = [[GameSetUpData alloc] init];
。