【问题标题】:Public class: The best way to store and access NSMutableDictionary?公共类:存储和访问 NSMutableDictionary 的最佳方式?
【发布时间】:2010-05-23 14:33:08
【问题描述】:

我有一个类可以帮助我跨会话存储持久数据。问题是我想在整个 Persistance 类的实例中将属性列表或“plist”文件的运行示例存储在 NSMutableArray 中,以便我可以读取和编辑值并在需要时将它们写回。

问题是,由于这些方法是公开定义的,我似乎无法毫无错误地访问声明的 NSMutableDictionary。我在编译时遇到的特定错误是:

warning: 'Persistence' may not respond to '+saveData'

所以在我解决这个问题之前,它会导致我的整个过程无法使用。

这是我完整的持久性课程(请注意,它尚未完成,因此只是为了说明这个问题):

Persistence.h

#import <UIKit/UIKit.h>

#define kSaveFilename @"saveData.plist"


@interface Persistence : NSObject {

    NSMutableDictionary *saveData;

}

@property (nonatomic, retain) NSMutableDictionary *saveData;

+ (NSString *)dataFilePath;

+ (NSDictionary *)getSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level;
+ (void)writeSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level withData:(NSDictionary *)saveData;
+ (NSString *)makeCampaign:(NSUInteger)campaign andLevelKey:(NSUInteger)level;

@end

Persistence.m

#import "Persistence.h"


@implementation Persistence
@synthesize saveData;

+ (NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    return [documentsDirectory stringByAppendingPathComponent:kSaveFilename];
}

+ (NSDictionary *)getSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level
{
    NSString *filePath = [self dataFilePath];

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSLog(@"File found");

        [[self saveData] setDictionary:[[NSDictionary alloc] initWithContentsOfFile:filePath]]; // This is where the warning "warning: 'Persistence' may not respond to '+saveData'" occurs



        NSString *campaignAndLevelKey = [self makeCampaign:campaign andLevelKey:level];

        NSDictionary *campaignAndLevelData = [[self saveData] objectForKey:campaignAndLevelKey];

        return campaignAndLevelData;
    }
    else
    {
        return nil;
    }

}

+ (void)writeSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level withData:(NSDictionary *)saveData
{
    NSString *campaignAndLevelKey = [self makeCampaign:campaign andLevelKey:level];
    NSDictionary *saveDataWithKey = [[NSDictionary alloc] initWithObjectsAndKeys:saveData, campaignAndLevelKey, nil];

    //[campaignAndLevelKey release];

    [saveDataWithKey writeToFile:[self dataFilePath] atomically:YES];
}

+ (NSString *)makeCampaign:(NSUInteger)campaign andLevelKey:(NSUInteger)level
{
    return [[NSString stringWithFormat:@"%d - ", campaign+1] stringByAppendingString:[NSString stringWithFormat:@"%d", level+1]];
}   

@end

我通过在我想要的位置包含头文件来像任何其他类一样调用这个类:

@import "Persistence.h"

然后我这样调用函数本身:

NSDictionary *tempSaveData = [[NSDictionary alloc] [Persistence getSaveWithCampaign:currentCampaign andLevel:currentLevel]];

【问题讨论】:

    标签: iphone objective-c public nsmutabledictionary


    【解决方案1】:

    您的问题是您正在使用类方法来尝试访问实例变量。

    如果你希望它是一个类变量,那么在.m文件中在@implementation之前声明它,或者为它创建类访问器方法,或者直接访问它。

    或者,这可能更好(虽然我不能轻易解释为什么),将你的类方法变成实例方法并创建一个类的单例。

    请参阅此处:What should my Objective-C singleton look like? 和此处:http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html 以了解关于单身人士的讨论。

    【讨论】:

    • 我无法得到您建议的正常工作。我添加了关于@implementation 的 NSMutableDictionary,它总是返回空白。
    • 你调用它的那一行是无效的——如果你alloc,你应该调用一个init方法。那你不要说你是否改变了你的代码直接访问saveData;当你试图在你的类对象上调用实例方法时,这是行不通的,我想我已经说过了。可能你不明白类和实例的区别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多