如何在 iOS 中保存和检索变量 to/from 持久化?
How to Save and Retrieve a Variable to/from Persistence in iOS?
每当我调用方法 generateTitleString 时,我需要向用户显示一条文本,内容为“Video X”,其中“X”是最后显示的视频编号用户加 1。如果我使用下面的代码,它总是生成相同的数字,即 1。
- (NSString *) generateTitleString
{
NSString *title = [NSString stringWithFormat:@"Video %d", count+1];
return title;
}
如何保存和检索 in/from 持久显示最后一个视频的值,所以每次显示“Video X”文本它显示增加的计数?
谢谢。
看起来你没有增加变量 count 这就是为什么你总是得到 "Video 1".
UPDATE:您可以创建一个 class 来保存所有全局变量(例如“count”) .在此示例中,我将其命名为 class GlobalData。您还需要使用 User Defaults 来永久保存某些变量的值。
试试下一个。
GlobalData.h:
#import <Foundation/Foundation.h>
@interface GlobalData : NSObject
+ (NSInteger) getCount;
+ (void) setCount:(NSInteger)newCount;
@end
GlobalData.m:
#import "GlobalData.h"
#define COUNT_USER_DEFAULT_KEY @"COUNT_USER_DEFAULT_KEY"
@implementation GlobalData
/**
* It returns the number of the last video displayed.
*
* @param
* @return the number of the last video displayed
*/
+ (NSInteger) getCount
{
NSInteger count;
NSString* recoveredValue;
// init variables
count = 0;
recoveredValue = [[NSUserDefaults standardUserDefaults] valueForKey:COUNT_USER_DEFAULT_KEY];
// If the recovered value is not NIL, we convert it to an integer.
if (recoveredValue != nil)
count = [recoveredValue integerValue];
return count;
}
/**
* It sets the number of the last video displayed with the value of input "newCount".
*
* @param newCount Number of the last video displayed.
* @return
*/
+ (void) setCount:(NSInteger)newCount
{
// Save user info in NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%li", (long)newCount] forKey:COUNT_USER_DEFAULT_KEY];
// Writes NSUserDefaults on disk.
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
视图控制器:
#import "GlobalData.h"
@implementation
- (void) viewDidLoad
{
[super viewDidLoad];
}
- (NSString *) generateTitleString
{
NSString *title;
NSInteger count;
// Getting count and increasing it.
count = [GlobalData getCount];
count = count + 1;
// Creating title by using 'count'.
title = [NSString stringWithFormat:@"Video %d", count];
// Updating 'count'
[GlobalData setCount:count];
return title;
}
@end
每当我调用方法 generateTitleString 时,我需要向用户显示一条文本,内容为“Video X”,其中“X”是最后显示的视频编号用户加 1。如果我使用下面的代码,它总是生成相同的数字,即 1。
- (NSString *) generateTitleString
{
NSString *title = [NSString stringWithFormat:@"Video %d", count+1];
return title;
}
如何保存和检索 in/from 持久显示最后一个视频的值,所以每次显示“Video X”文本它显示增加的计数?
谢谢。
看起来你没有增加变量 count 这就是为什么你总是得到 "Video 1".
UPDATE:您可以创建一个 class 来保存所有全局变量(例如“count”) .在此示例中,我将其命名为 class GlobalData。您还需要使用 User Defaults 来永久保存某些变量的值。
试试下一个。
GlobalData.h:
#import <Foundation/Foundation.h>
@interface GlobalData : NSObject
+ (NSInteger) getCount;
+ (void) setCount:(NSInteger)newCount;
@end
GlobalData.m:
#import "GlobalData.h"
#define COUNT_USER_DEFAULT_KEY @"COUNT_USER_DEFAULT_KEY"
@implementation GlobalData
/**
* It returns the number of the last video displayed.
*
* @param
* @return the number of the last video displayed
*/
+ (NSInteger) getCount
{
NSInteger count;
NSString* recoveredValue;
// init variables
count = 0;
recoveredValue = [[NSUserDefaults standardUserDefaults] valueForKey:COUNT_USER_DEFAULT_KEY];
// If the recovered value is not NIL, we convert it to an integer.
if (recoveredValue != nil)
count = [recoveredValue integerValue];
return count;
}
/**
* It sets the number of the last video displayed with the value of input "newCount".
*
* @param newCount Number of the last video displayed.
* @return
*/
+ (void) setCount:(NSInteger)newCount
{
// Save user info in NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%li", (long)newCount] forKey:COUNT_USER_DEFAULT_KEY];
// Writes NSUserDefaults on disk.
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
视图控制器:
#import "GlobalData.h"
@implementation
- (void) viewDidLoad
{
[super viewDidLoad];
}
- (NSString *) generateTitleString
{
NSString *title;
NSInteger count;
// Getting count and increasing it.
count = [GlobalData getCount];
count = count + 1;
// Creating title by using 'count'.
title = [NSString stringWithFormat:@"Video %d", count];
// Updating 'count'
[GlobalData setCount:count];
return title;
}
@end