在为僵尸分析应用程序时,我收到以下错误:
堆栈:
代码:
启动应用后,我们会使用类似的代码访问多个NSUserDefaults
属性。有时,应用程序会在尝试访问默认值的不同方法上崩溃。
+(NSString*)getSettingsFileName{
prefs = [NSUserDefaults standardUserDefaults];
return [prefs objectForKey:@"SettingsFileName"]; // EXC_BAD_ACCES (code=1, address=0x8178b6ed)
}
我尝试在访问之前同步NSUserDefaults,但我得到了相同的行为。
非常感谢有关如何解决此问题的任何提示!
修改
+(void)readSettingsDataAndUpdateIncludeDomain:(BOOL)updateDomain MBMHost:(BOOL)updateMBMHost
{
//Read from Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *settingsFileName = [documentsDirectory stringByAppendingPathComponent:SETTINGS_FILE];
NSDictionary *settingsDic = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileExists = [fileManager fileExistsAtPath:settingsFileName];
if(fileExists) {
settingsDic = [NSDictionary dictionaryWithContentsOfFile:settingsFileName];
} else {
NSError *error = nil;
NSString *settingsBundleFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:SETTINGS_FILE];
BOOL success = [fileManager copyItemAtPath:settingsBundleFilePath toPath:settingsFileName error:&error];
if(success)
settingsDic = [NSDictionary dictionaryWithContentsOfFile:settingsFileName];
}
prefs = [NSUserDefaults standardUserDefaults];
if(!fileExists)
{
//subscription
[prefs setBool:YES forKey:@"Resubscribe"];
}
[prefs setObject:[settingsDic objectForKey:@"ADDRNAME"] forKey:@"ADDRNAME"];
if( updateDomain )
{
[prefs setObject:[settingsDic objectForKey:@"Domain"] forKey:@"Domain"];
}
[prefs setBool:[[settingsDic objectForKey:@"Https"] boolValue] forKey:@"Https"];
if( updateMBMHost )
{
[prefs setObject:[settingsDic objectForKey:@"Host"] forKey:@"Host"];
}
[prefs setObject:[settingsDic objectForKey:@"ServerNode"] forKey:@"ServerNode"];
[prefs removeObjectForKey:@"ULVersion"];
[prefs setObject:[settingsDic objectForKey:@"ULVersion"] forKey:@"ULVersion"];
forKey:@"ULDownloadOnly"];
[prefs setObject:[settingsDic objectForKey:@"AgentName"] forKey:@"AgentName"];
if ([[Settings getSettingsFileName] length] < 1) //This is the Line where it usually crashes"
[prefs setObject:[settingsDic objectForKey:@"SettingsFileName"] forKey:@"SettingsFileName"];
[prefs setObject:[settingsDic objectForKey:@"ScannerType"] forKey:@"ScannerType"];
NSString *supportEmailAddress = [settingsDic objectForKey:@"SupportEmailAddress"];
if( supportEmailAddress == nil )
{
supportEmailAddress = SUPPORT_EMAIL_ADDRESS;
}
[prefs setObject:supportEmailAddress forKey:@"SupportEmailAddress"];
//Don't set SyncCalContactsMode from plist since this will overide the user's selection
if([[settingsDic objectForKey:@"Instance"] length] > 1)
[prefs setObject:[settingsDic objectForKey:@"Instance"] forKey:@"Instance"];
else if ([[Settings getInstance] length] < 1)
[prefs setObject:@"Company" forKey:@"Instance"];
NSString *info = [NSString stringWithFormat:@"ULVersion = %@, ULDownloadOnly = %@ ", ([Settings getULVersion]==nil)?@"Version1":[Settings getULVersion], [Settings getULDownloadOnly]?@"YES":@"NO"];
[DebugLog writeDebugData:info];
}