gpt4 book ai didi

ios - 单例创建,在 ViewController 中使用时出错?

转载 作者:行者123 更新时间:2023-11-29 01:27:06 28 4
gpt4 key购买 nike

我在一个 ViewController (PointsViewController.m) 中将 NSUbiquitousKeyValueStore 定义为“CloudStore”,并创建了一个 CloudStore 单例,以便能够在另一个 ViewController (PayViewController.m) 中使用它。

但是,当我在 PayViewController.m 中调用 Singleton 时,它会在其下的每个 NSString 引用上引发错误。错误内容为:“没有已知的 StringForKey 类方法”和“没有已知的 setString:forKey 方法”。我在这里错过了什么吗?我是否错误地实现了单例?请参阅下面的 ViewController 和 Singleton 代码。

PointsViewController.m

 NSUbiquitousKeyValueStore *CloudStore = [NSUbiquitousKeyValueStore defaultStore];

NSString *pointsStr = [CloudStore stringForKey:@"UserWantsToBuyPoints"];
[CloudStore setString:pointsStr forKey:@"UserWantsToBuyPoints"];
[CloudStore synchronize];


NSString *TotalUserPoints = [CloudStore stringForKey:@"TotalUserPoints"];


int pointsInt = [pointsStr intValue];
int TotalUserPointsInt = [TotalUserPoints intValue];


TotalUserPointsInt = TotalUserPointsInt + pointsInt;

NSString *totalStr = [NSString stringWithFormat:@"%d", TotalUserPointsInt];

[CloudStore setObject:totalStr forKey:@"TotalUserPoints"];
[CloudStore synchronize];

PayViewController.m

[CloudStore singleton];

NSString *pointsStr = [CloudStore stringForKey:@"UserWantsToBuyPoints"];
[CloudStore setString:pointsStr forKey:@"UserWantsToBuyPoints"];
[cloudStore synchronize];


NSString *TotalUserPoints = [CloudStore stringForKey:@"TotalUserPoints"];


int pointsInt = [pointsStr intValue];
int TotalUserPointsInt = [TotalUserPoints intValue];


TotalUserPointsInt = TotalUserPointsInt + pointsInt;

NSString *totalStr = [NSString stringWithFormat:@"%d", TotalUserPointsInt];

[CloudStore setObject:totalStr forKey:@"TotalUserPoints"];
[CloudStore synchronize];
NSString *pointsStr = [CloudStore stringForKey:@"UserWantsToBuyPoints"];
[CloudStore setString:pointsStr forKey:@"UserWantsToBuyPoints"];
[CloudStore synchronize];


NSString *TotalUserPoints = [CloudStore stringForKey:@"TotalUserPoints"];


int pointsInt = [pointsStr intValue];
int TotalUserPointsInt = [TotalUserPoints intValue];


TotalUserPointsInt = TotalUserPointsInt + pointsInt;

NSString *totalStr = [NSString stringWithFormat:@"%d", TotalUserPointsInt];

[CloudStore setObject:totalStr forKey:@"TotalUserPoints"];
[CloudStore synchronize];

CloudStore.h(单例)

#import <Foundation/Foundation.h>

@interface CloudStore : NSObject

+(CloudStore *)singleton;

@end

CloudStore.m(单例)

#import "CloudStore.h"

@implementation CloudStore

+(CloudStore *)singleton {
static dispatch_once_t pred;
static CloudStore *shared = nil;
dispatch_once(&pred, ^{
shared = [[CloudStore alloc] init];
});
return shared;
}

@end

最佳答案

您实际上是在搞乱代码中的单例和 iCloud 存储对象。您需要在单例中声明并初始化 iCloud 对象并使用它:

CloudStore.h

#import <Foundation/Foundation.h>

@interface CloudStore : NSObject

// Storage property
@property (nonatomic, strong) NSUbiquitousKeyValueStore *cloud;

+ (CloudStore *)sharedStorage;

@end

CloudStore.m

#import "CloudStore.h"

@implementation CloudStore

+ (CloudStore *)sharedStorage
{
static dispatch_once_t pred;
static CloudStore *shared = nil;
dispatch_once(&pred, ^{
shared = [[CloudStore alloc] init];
[shared initStorage];
});
return shared;
}

- (void)initStorage
{
cloud = [NSUbiquitousKeyValueStore defaultStore];
}
@end

无论您想在何处使用 CloudStore 变量,都可以像这样使用它:

设置数据:

[[[CloudStore sharedStorage] cloud] setObject:totalStr forKey:@"TotalUserPoints"];

获取数据:

 NSString *pointsStr = [[[CloudStore sharedStorage] cloud] stringForKey:@"UserWantsToBuyPoints"];

同步:

[[[CloudStore sharedStorage] cloud] synchronize];

您应该阅读这个不错的教程:iOS Design Patterns ,它将帮助您更好地理解单例的用法。

关于ios - 单例创建,在 ViewController 中使用时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880133/

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