Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSMutableDictionary在无效和iActions中不起作用_Iphone_Xcode_Nsdictionary - Fatal编程技术网

Iphone NSMutableDictionary在无效和iActions中不起作用

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

我在主viewController的
.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];
}