iPhone sdk:我试图将整数增加 10 但失败

发布于 2024-07-26 22:39:01 字数 1058 浏览 3 评论 0原文

(问题现已解决,请参阅下面的答案)

您好,在我的应用程序中,我正在尝试创建一个积分计数器。 每按一次按钮,数字就会增加 10。

我成功地让我的应用程序在每次按下按钮时将值 (0) 加 1,并记住最新值并在下次应用程序启动时显示它。 但是,当您按下按钮时,应用程序重新启动后积分值会增加 1,数字会从 0 开始增加,而不是记住的值。

在应用程序退出并再次启动后,如何让积分增加 10 并记住最新值并显示(并从中计数)?

这是我当前的代码:

按钮将值增加 1 的代码:

   - (IBAction)startClick:(id)sender{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:count forKey:@"greeting"];

NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
counter.text = numValue;

viewDidLoad 方法:

   - (void)viewDidLoad {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *greetName = [prefs stringForKey:@"greeting"];
NSInteger count = [prefs integerForKey:@"greeting"];
counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];

if(count == 0) {
    counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
    counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}

(Problem solved now, see answers below)

Hello, in my app I am trying to create a points counter. That increases a number by 10 every time a button is pressed.

I successfully managed to get my app to increase a value (0) by 1 every time a button is pressed, and to remember the latest value and display it next time the app starts. But when you press the button that's suppose to increase the points value by 1 after the app restarts the number starts increasing from 0 not the remembered value.

How would I get the points to increase by 10 and remember the latest value and display (and count from it) after the application is exited and started again ?

Here is my current code:

Code for the button to increase the value by 1:

   - (IBAction)startClick:(id)sender{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:count forKey:@"greeting"];

NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
counter.text = numValue;

viewDidLoad method:

   - (void)viewDidLoad {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *greetName = [prefs stringForKey:@"greeting"];
NSInteger count = [prefs integerForKey:@"greeting"];
counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];

if(count == 0) {
    counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
    counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮云壑 2024-08-02 22:39:01

在您的 startClick: 方法中,您似乎在递增之前将 count 整数存储在用户默认值中。 如果您想将其增加 10,则将方法更改为:

- (IBAction)startClick:(id)sender {
    //Increase count by 10
    count+=10;

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:count forKey:@"greeting"];

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count];
    counter.text = numValue;
}

另外,为什么要设置 counter.text 属性两次?:

counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];

if(count == 0) {
        counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
        counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}

使用 stringWithFormat< 也可能对您有用/code> 而不是分配 NSString 并调用 initWithFormat,因为目前它正在泄漏内存,因为您没有释放它; 如果您调用 stringWithFormat,它将返回一个自动释放的对象,该对象将在设置 counter.text 时保留(因为它是 setter 方法的简写),因此您将没有记忆问题。 您可以将该行更改为:

counter.text = [NSString stringWithFormat:@"%d",count];

这也将使您免于从用户默认值中以字符串形式检索计数器值。

In your startClick: method, you appear to store the count integer in the user defaults before it has been incremented. If you want to increment it by 10, then change the method to:

- (IBAction)startClick:(id)sender {
    //Increase count by 10
    count+=10;

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:count forKey:@"greeting"];

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count];
    counter.text = numValue;
}

Also, why do you set the counter.text property twice?:

counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];

if(count == 0) {
        counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
        counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}

It may also be useful for you to use stringWithFormat instead of allocating an NSString and calling initWithFormat, since at the moment it is leaking memory because you don't release it; if you call stringWithFormat, it will return an autoreleased object, which will be retained when counter.text is set (since it is a shorthand for the setter method) so you will have no memory problems. You can change the line to:

counter.text = [NSString stringWithFormat:@"%d",count];

Which will also save you from retrieving the counter value as a string from the user defaults.

负佳期 2024-08-02 22:39:01

这里发生了一些事情。 首先,您在增加计数之前先保存计数。 这意味着即使保存发生,它也会比你预期的要低。

但您看到的实际错误是您忽略了计数。 如果你仔细观察,你会发现 viewDidLoad: 中有以下行,

NSInteger count = [prefs integerForKey:@"greeting"];

尽管事实上它直接引用了 startClick: 中的计数。 我假设您还有一个名为 count 的 ivar,并且您的意思是设置 ivar,但实际上您正在设置局部变量。 分配类时,ivar 默认设置为 0,这就是为什么它似乎总是被清零的原因。 您可以通过将其更改为:

count = [prefs integerForKey:@"greeting"];

来修复它 如果您在应用程序退出期间崩溃,您的值也可能不会同步到磁盘,但除非您在运行日志中看到崩溃,否则这应该不是问题。 如果该值很重要,您可以显式同步该值。

您是否在打开警告的情况下编译此代码? 您是否正在尝试修复这些警告? 如果您打开了警告,您应该会看到类似“'count'的本地声明隐藏实例变量”之类的内容

There are a couple of things going on here. First off, you are saving count before you are incrementing it. That means even if the save was happening it would be one lower than you expect.

The actual bug that you are seeing though is that you are eliding count. If you look you will notice that you have the following line in viewDidLoad:

NSInteger count = [prefs integerForKey:@"greeting"];

despite the fact that directly refer to count in startClick:. I presume you also have an ivar named count and that what is going on is you mean to set the ivar, but you are in fact setting the local variable. The ivar will be set to 0 by default when the class is alloced, which is why it always seems to be zeroed. You can fix it by changing that like to:

count = [prefs integerForKey:@"greeting"];

It is also possible your values are not being synched out to disk if you are crashing during application quit, but unless you are seeing crashes in the run log that should not be an issue. You can explicitly synchronize out the value if it is important.

Are you compiling this code with warnings turned on? Are you trying to fix those warnings? If you have warnings turned on you should be seeing something like "local declaration of 'count' hides instance variable"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文