-(void)addPower:(NSString *)newPower{
newPower = [newPower lowercaseString];
for(int i=0; i<[powers count]; i++){
NSString *p = [powers objectAtIndex:i];
NSString* count = p;
NSRange endRange = [p rangeOfString:@" "];
NSRange searchRange = NSMakeRange(0, endRange.location);
p = [p substringWithRange:searchRange];
endRange = [count rangeOfString:@" "];
count = [count substringWithRange:NSMakeRange(endRange.location, count.length-p.length)];
if([newPower isEqualToString:p]){
NSString *addNewPower = [NSString stringWithFormat:@"%@ %i",newPower,count.integerValue+1];
[powers replaceObjectAtIndex:i withObject:addNewPower];
[ud setValue:addNewPower forKey:productName];
NSLog(@"NEW POWER ADDED: %@ ||| %@\n",addNewPower,productName);
NSLog(@"NEW POWER UD: %@\n",[ud stringForKey:productName]);
}
}
[ud synchronize];
}
输出是;
新增功能:snap 4 ||| 折断
新力量 UD:(空)
当我在手机上重新启动应用程序后再次加载 Userdefault 时,它会用“1”替换应该是“snap 4”的内容
我不明白为什么它不保存我的用户默认值。
编辑:忘了提,在初始化程序中设置了 ud 并且我尝试了 setObject forKey 并且仍然得到相同的结果