我的应用程序出现问题 - 我正在使用NSUserDefaults为当前正在使用该应用的用户保存“个人资料”。
它需要一些信息“姓名,员工ID,地址和基础学校” - 非常标准的东西。它保存得很好,并保存信息。
使用具有学校列表的UIPickerView选择baseSchool - 它使用我的MetaMiles类中的一个名为schoolNameList的方法,用一系列学校填充选择器视图。
当用户尝试向应用添加“里程数”时,我会在另一个视图中使用相同的方法,以便使用相同的学校列表填充这些UIPickerView。
所以这个过程看起来像这样:
用户启动应用并设置其个人资料(点击保存 - 所有内容保存到NSUserDefaults) - 您可以反复打开并保存配置文件。
用户点击“添加里程” - 添加里程页面显示以及学校的选择视图,并添加里程。
用户点击“更改个人资料” - 应用崩溃。
应用程序崩溃是因为在该行的某个时刻 - NSUserDefaults忘记了在步骤1中设置的baseSchool(当用户第一次设置配置文件时)。它似乎只有在用户点击“添加里程”并且加载了该视图的选择视图时才会发生。
所以我想知道它是否与该方法有关 - 或者如果我可能没有正确保存我的默认值? 也许我应该使用coredata并将用户个人资料信息保存到实体?
任何建议/反馈表示赞赏。
这是我的个人资料视图:
//Call method to get array of school names (reusable)
MetaMiles *model = [MetaMiles schoolNameList];
_schoolList = (NSArray *)model;
//****LOAD USER DATA IF AVAILABLE****//
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"profileSetup"]==0){
} else {
_fullName.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"fullName"];
_employeeID.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"employeeID"];
_homeAddress.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"homeAddress"];
//****//****THIS IS WHERE THE CRASH OCCURS BECAUSE IT DOES NOT REMEMBER DEFAULT AFTER THE ADD MILES VIEW HAS BEEN VIEWED****//****//
NSString *lastSelectedSchool =[[NSUserDefaults standardUserDefaults] objectForKey:@"baseSchool"];
//If the add miles view hasn't been loaded this works fine - otherwise it is null
NSLog(@"Last Selected School from defaults: %@",lastSelectedSchool);
NSUInteger currentIndex = [_schoolList indexOfObject:lastSelectedSchool];
//This doesn't work if it's null!
NSLog(@"Integer of school: %lu", (unsigned long)currentIndex);
[schoolPicker selectRow:currentIndex inComponent:0 animated:YES];
}
//****END LOAD USER DATA****//
这是我的MetaMiles类方法
+ (NSArray*)schoolNameList {
NSArray *schoolList;
MetaMiles *school;
//Create fetched results controller of data
NSFetchedResultsController *fetchedSchoolController = [MetaMiles MR_fetchAllSortedBy:@"beg_school" ascending:YES withPredicate:nil groupBy:@"beg_school" delegate:nil];
[fetchedSchoolController.fetchRequest setReturnsDistinctResults:YES];
//Turn the controller into an array called - fetchedSchools
NSArray *fetchedSchools = [fetchedSchoolController fetchedObjects];
//Create a set of school names - mutable set (only 1 listing per school)
NSMutableSet *schoolNames = [[NSMutableSet alloc]init];
//place each school name into the Set
for (school in fetchedSchools) {
[schoolNames addObject:school.beg_school];
}
//Take the set, turn it into an array (schoolList) and order it by school name
schoolList = [[schoolNames allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"" ascending:YES]]];
return schoolList;
}
@end
答案 0 :(得分:1)
你放置了:
selectedSchool = [NSString stringWithFormat:@"%@",
[_schoolList objectAtIndex:row]];
<{1>}中的,但您可以点击按钮(-didSelectRow:
)来保存个人资料。
这就是(IBAction)saveProfileButton:(id)sender
为空的原因。
baseSchool
以空值崩溃。