Iphone NSMutableDictionary在无效和iActions中不起作用
我在主viewController的Iphone NSMutableDictionary在无效和iActions中不起作用,iphone,xcode,nsdictionary,Iphone,Xcode,Nsdictionary,我在主viewController的.h文件中创建了一个名为*temp的NSMutableDictionary,并添加此代码以从我的.plist文件中引入信息 - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; temp=[NSMutableDictionary diction
.h
文件中创建了一个名为*temp
的NSMutableDictionary
,并添加此代码以从我的.plist
文件中引入信息
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
temp=[NSMutableDictionary dictionaryWithContentsOfFile:path];
}
在同一视图控制器中,我添加了一个按钮操作,并添加了以下代码:
-(IBAction)mathButton:(UIButton *)_sender
{
label1.text = [temp objectForKey:@"m1name"];
}
其中“label1是.xib
中的文本字段,m1name
是.plist
中的一个键
但当我运行它时,它不起作用,突出显示label1.text=[temp objectForKey:@“m1name”];
并称之为坏访问
这件事我已经坚持了好几天了,我尝试了很多东西。一个答案真的很有帮助
谢谢
您没有保留通过dictionaryWithContentsOfFile:path创建的字典。您应该将该行更改为:
temp = [[NSMutableDictionary dictionaryWithContentsOfFile:path] retain];
(并确保它是在dealoc
中发布的),或者,如果temp
是一个属性,则通过
self.temp = [NSMutableDictionary dictionaryWithContentsOfFile:path];
在h中:
In.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource: @"Data"
ofType: @"plist"];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: path];
if (exists)
{
temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSLog(@"%@", [temp description]);
}
}
- (IBAction) mathButton: (UIButton *)_sender
{
label1.text = [temp objectForKey: @"m1name"];
}
如果是MRC:
- (void) dealloc
{
[temp release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource: @"Data"
ofType: @"plist"];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: path];
if (exists)
{
temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSLog(@"%@", [temp description]);
}
}
- (IBAction) mathButton: (UIButton *)_sender
{
label1.text = [temp objectForKey: @"m1name"];
}
- (void) dealloc
{
[temp release];
[super dealloc];
}