我試圖創建一個屬性爲getter - 暫時我只是用的方法從靜態數組建立一個NSMutableObject但最終這些都將是動態的配置設置。在以前的應用程序,從getter and setters not working objective c我這樣做:自吸氣不叫目標C
#import "ViewController.h"
@interface ViewController()
@property (nonatomic) NSMutableDictionary *questions;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self questions] setValue:@"FOO" forKey:@"bar"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
+ (NSMutableDictionary *)questions
{
static NSMutableDictionary *_questions;
if (_questions== nil)
{
NSArray *genders = @[@"male", @"female"];
NSArray *ages = @[@"<10", @">60", @"Other"];
_questions = [[NSMutableDictionary alloc] init];
[_questions setValue:genders forKey:@"gender"];
[_questions setValue:ages forKey:@"age"];
}
return _questions;
}
當我在viewDidLoad中的線,我嘗試使用「問題」屬性,它不使用自定義的吸氣劑(它只是分配欄:FOO到零字典)。我錯過了什麼?
事實上,我懷疑操作系統的意圖是使它成爲一個實例方法:'(NSMutableDictionary *)questions'(將領先的「+」改爲「 - 」 –