如何递增整数并保存它以供程序重新运行时使用?
我正在尝试以一种简单的方式增加一个数字,不会导致重复或丢失数字。该数字最终将作为订单号存储在我的 coreData 实体中的字段中,这就是为什么我不能有重复或丢失的数字。
我有一个用于输入订单的视图,当它初始化时,它必须提供正确的订单号,这就是我编码的内容:
-(void) viewDidLoad {
// If no orders have previously been entered the order number will be changed from 0 to 1
if (orderNumber == 0) {
orderNumber = orderNumber + 1;
}
// If orders have previously been entered the order number will be given the value that has previously been stored in orderNumber
else orderNumber = orderNumber;
NSLog(@"The Order Number is %d",orderNumber);
}
当视图上的表单完成时,按下完成按钮,这将最终确定订单。视图上的所有文本字段都会被清除,并且订单号会加 1,以便可以输入下一个订单。
-(void) doneButtonTapped:(id)sender{
orderNumber = orderNumber + 1;
NSLog(@"orderNumber is increased to %d",orderNumber);
}
我的头文件包括以下内容:
int orderNumber;
@property (nonatomic,assign) int orderNumber;
这在应用程序运行时有效,但如果我退出应用程序并重新运行它,我的 orderNumber 将从 1 开始。
当应用程序时,如何存储/保存与 orderNumber 关联的值以便在 viewDidLoad 中使用又跑了?
我的增量方法可以吗还是有更好的方法?我用过int,但我看到其他人使用NSNumber,我不确定是否有优点&使用其中一种或另一种的缺点。
任何帮助/建议将不胜感激。
I am trying to increment a number in a simple way that won't cause duplicates or missed numbers. This number will eventually be stored in a field in my coreData entity as an order number which is why I can't have duplicates or missed numbers.
I have a view for entering orders and when it initialises it must provide the correct order number, this is what I have coded:
-(void) viewDidLoad {
// If no orders have previously been entered the order number will be changed from 0 to 1
if (orderNumber == 0) {
orderNumber = orderNumber + 1;
}
// If orders have previously been entered the order number will be given the value that has previously been stored in orderNumber
else orderNumber = orderNumber;
NSLog(@"The Order Number is %d",orderNumber);
}
When the form on the view has been completed a done button is pressed, this finalises the order. All textfields on the view are cleared and the order number is increased by 1 so that the next order can be entered.
-(void) doneButtonTapped:(id)sender{
orderNumber = orderNumber + 1;
NSLog(@"orderNumber is increased to %d",orderNumber);
}
My header file includes the following:
int orderNumber;
@property (nonatomic,assign) int orderNumber;
This works while the application is running but if I quit the application and re-run it my orderNumber starts back at 1.
How can I store/save the value associated to orderNumber for use in viewDidLoad when the application runs again?
Is my method for incrementing ok or is there a better way? I have used int but I see others use NSNumber, I am not sure if there are advantages & disadvantages of using one or the other.
Any help/suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
NSUserDefaults
来存储int
和其他基本类型(BOOL
、float
、NSString< /code>,
NSArray
)。例如,您可以使用 检索
int
,然后使用 使用 保存
int
。必要时,您还可以调用
以确保您访问的是最新信息。如果您在应用程序的多个位置访问此
int
,这尤其有用。You can use
NSUserDefaults
to storeint
s and other basic types (BOOL
,float
,NSString
,NSArray
).For example, you can retrieve the
int
usingand then save the
int
usingWhen necessary, you can also call
to ensure that you are accessing the most up to date information. This is especially useful if you are accessing this
int
in multiple places in your app.您可以像这样使用
NSUserDefaults
:You would use
NSUserDefaults
like so: