2011-07-07 26 views
1

这里是遇到问题的代码IM:如何在Objective C中全局访问这个变量?

if(DriveInfoDict) { 
    NSLog(@"%@", DriveInfoDict); 
    //PrevSpeedsDict = [DriveInfoDict objectForKey: @"speed"]; 
    //NSLog(@"Previous Speed Dict:%@", PrevSpeedsDict); 
} 



DriveInfoDict = [NSDictionary dictionaryWithObjectsAndKeys: 
       [NSNumber numberWithDouble: CurrentLatitude], @"Lat", 
       [NSNumber numberWithDouble: CurrentLongitude], @"Long", 
       [NSNumber numberWithDouble:speedMPH], @"speed", 
       nil]; 

在这里,我想设置DriveInfoDict,使功能运行它下一次将有之前的值。我已经剥离了操作员来简化我的问题。

我得到的错误是:EXC-BAD-ACCESS

我是新来的OBJ-C,我不知道如何使这个对象这里访问。一些代码与解释,如果它在H或M文件将是非常有用的。

回答

3

您需要保留的字典或使用alloc/init(返回保留字典因此,要么:

DriveInfoDict = [[NSDictionary dictionaryWithObjectsAndKeys: 
       [NSNumber numberWithDouble: CurrentLatitude], @"Lat", 
       [NSNumber numberWithDouble: CurrentLongitude], @"Long", 
       [NSNumber numberWithDouble:speedMPH], @"speed", 
       nil] retain]; 

或:

DriveInfoDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
       [NSNumber numberWithDouble: CurrentLatitude], @"Lat", 
       [NSNumber numberWithDouble: CurrentLongitude], @"Long", 
       [NSNumber numberWithDouble:speedMPH], @"speed", 
       nil]; 

如果更换的DriveInfoDict的内容(即:分配一本新字典)别忘了先发布它

+0

非常感谢您先生 – TaylorMac

+0

当它允许时 – TaylorMac