我已经在一个ViewController(PointsViewController.m)中将NSUbiquitousKeyValueStore定义为“ CloudStore”,并创建了一个CloudStore单例以便能够在另一个ViewController(PayViewController.m)中使用它。
但是,当我在PayViewController.m中调用Singleton时,它将对它下面的每个NSString引用引发错误。该错误显示为:“未知的类方法forStringForKey”和“未知的方法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
最佳答案
您实际上是在代码中弄乱了singleton和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,它将帮助您更好地理解单例的用法。