将数据从plist添加到NSSet?

时间:2013-07-18 04:28:39

标签: ios objective-c plist nsset

我正在尝试在应用购买中添加产品ID。但是在下面的代码中我将手动添加它。如何从plist添加它?

我的代码:

@implementation RageIAPHelper

+ (RageIAPHelper *)sharedInstance {
    static dispatch_once_t once;
    static RageIAPHelper * sharedInstance;
    dispatch_once(&once, ^{
        NSSet * productIdentifiers = [NSSet setWithObjects:
                                      @"greatminds.assamkarttestingdays",
                                      @"greatminds.newgirlinthecity",
                                      @"greatminds.newlights",
                                      nil];
        sharedInstance = [[self alloc] initWithProductIdentifiers:productIdentifiers];
    });
    return sharedInstance;
}

2 个答案:

答案 0 :(得分:3)

将plist设置为数组。然后从plist文件加载NSArray。然后从NSSet创建NSArray

// Assume you have a plist in the app bundle named "products.plist" with a root of type array
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"products" ofType:@"plist"];
NSArray *products = [NSArray arrayWithContentsOfFile:filePath];
NSSet *productIdentifiers = [NSSet setWithArray:products];

答案 1 :(得分:3)

您实际上可以将plist文件加载到NSSArray中,然后使用setWithArray类方法将NSArray项加载到NSSet中,这将从数组中删除重复项并添加要设置的项。这是代码。

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"plist"];
NSArray * contentArray = [NSArray arrayWithContentsOfFile:plistPath];
NSSet * sampleSet = [NSSet setWithArray:contentArray];