所以我創建了一個簡單的UITableViewController
,我推入NavigationController
。 我創建了一個initWithPList
方法,讀取plist
和的值存儲到在items
@property
一個NSMutableDictionary
在稱爲Settings
class
。NSMutableDictionary is nil
NSMutableDictionary
在initWithStyle
方法中正確填充。但是在viewDidLoad
,Settings
NSObject
的NSMutableDictionary
items
@property
是nil
。
我不知道我在做什麼錯誤的任何想法????
下面是一些代碼:
顯示的ViewController
:
TestViewController *settingsViewController = [[TestViewController alloc] initWithStyle:UITableViewStyleGrouped];
[[self navigationController] pushViewController:settingsViewController animated:YES]
初始化TestViewController
:
- (id)initWithStyle:(UITableViewStyle)style{
if (self = [super initWithStyle:style]) {
// Init settings property here
settings = [[Settings alloc] initWithPList];
// settings.items has a count of 5 here....which is correct
}
return self;
}
viewDidLoad
爲TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Check settings object here to see if it still has items
// settings.items is nil here for somereason??
NSLog(@"%i", [settings.items count]); // now it's nil here?
}
的Settings
NSObject
:
#import <Foundation/Foundation.h>
@interface Settings : NSObject {
NSMutableDictionary *items;
NSString *path;
}
@property(nonatomic, retain) NSMutableDictionary *items;
@property(nonatomic, retain) NSString *path;
-(id)initWithPList;
@end
的@implementation
一個片段:
@synthesize items;
@synthesize path;
-(id) initWithPList {
path = [[self documentsDirectory]stringByAppendingPathComponent:@"Settings.plist"];
if(![[NSFileManager defaultManager]fileExistsAtPath:path])
{
[[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"Settings" ofType:@"plist"] toPath:path error:nil];
}
items = [NSMutableDictionary dictionaryWithContentsOfFile:path];
return self;
}
當你說「//初始設置屬性在這裏」,你在技術上不是。這個'settings'不是一個屬性引用。它是如何聲明的? (也是這個項目使用ARC?) –
@PhillipMills,我猜Gabe在屬性聲明中使用了retain,這個項目是ARC之前的。 –