gpt4 book ai didi

ios - 添加到之前保存的 NSUserDefaults

转载 作者:行者123 更新时间:2023-11-29 04:01:12 26 4
gpt4 key购买 nike

我正在为 iOS 制作一款游戏,其中每个级别都有一个评分系统。这里我有一些使用委托(delegate)获取 NSString (passedValue1) 的代码。

这是委托(delegate)的代码:

LevelCompleteLevel1

.h

#import "ViewController.h"
@interface LevelCompleteLevel1 : UIViewController{
IBOutlet UILabel *label;

ViewController *secondviewData;
}
@property (nonatomic, retain)ViewController*secondviewData;

.m

ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
self.secondviewData = second;
secondviewData.passedValue1 = label.text;
[self presentModalViewController:second animated:YES];

.m中,我将当前 View Controller 中标签中的值发送到另一个 View Controller ,我发送的值被称为passedValue1

一旦我发送了需要在我的 ViewController 中显示和保存的值

ViewController

.h

@interface ViewController : UIViewController{

IBOutlet UILabel *label;
NSString *passedValue1;
}
@property (nonatomic, retain)NSString *passedValue1;

@end

.m

首先我...@synthesize passValue1;

然后我添加代码以接收 viewDidLoad 中的值并在 UILabel 中显示我的值

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
NSString *key=@"labelKey";

// Do any additional setup after loading the view, typically from a nib
if(passedValue1){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSObject * object = [prefs objectForKey:key];
if(object != nil){
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setObject:passedValue1 forKey:key];
[defults synchronize];
}
else{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];

NSInteger readScore=[[[NSUserDefaults standardUserDefaults] objectForKey:key] integerValue];
NSInteger newValue=readScore+[passedValue1 integerValue];
[defults setObject:[NSString stringWithFormat:@"%d",newValue] forKey:key];
[defults synchronize];
}
}
label.text = [[NSUserDefaults standardUserDefaults]objectForKey:key];

显示值后,我会使用 NSUserDefault 将其保存到标签中。但是,一旦我重玩游戏并获得另一个分数值,我想将新的 passedValue1 值添加到当前保存的值...

例如:

假设我玩我的关卡,得到的分数值为 10。然后保存该值,然后我重玩我的关卡。然后我想将保存的值添加到我刚刚得分的值中。也就是说,我得分的第二个值是 20。然后我希望我的代码将它们加在一起并给我一个值 30。

任何人都可以提供帮助,因为我正在使用的代码无法正确执行我想要执行的功能,即添加先前和当前传递的值。

提前致谢。

编辑我正在使用此代码:

 - (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
NSString *key=@"labelKey";

// Do any additional setup after loading the view, typically from a nib
if(passedValue1){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSObject * object = [prefs objectForKey:key];
if(object != nil){
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
[defults setObject:passedValue1 forKey:key];
[defults synchronize];
}
else{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];

NSInteger readScore=[[[NSUserDefaults standardUserDefaults] objectForKey:key] integerValue];
NSInteger newValue=readScore+[passedValue1 integerValue];
[defults setValue:[NSString stringWithFormat:@"%d",newValue] forKey:key];
[defults synchronize];
}
}
label.text = [[NSUserDefaults standardUserDefaults]objectForKey:key];
}

为什么这不起作用,标签显示的值是原来传递的Value1?

最佳答案

尝试一下这个,

  NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:@"10" forKey:@"old value"];
[defaults synchronize];

int k=[[defaults objectForKey:@"old value"] intValue]+newValue;

关于ios - 添加到之前保存的 NSUserDefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15833907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com