セッション間で永続的なデータを保存するのに役立つクラスがあります。問題は、Persistance クラスのインスタンス全体で、プロパティ リストまたは「plist」ファイルの実行中のサンプルを NSMutableArray に保存して、必要に応じて値を読み取って編集し、書き戻すことができるようにすることです。
問題は、メソッドが公に定義されているため、宣言された NSMutableDictionary にエラーなしでアクセスできないように見えることです。コンパイル時に発生する特定のエラーは次のとおりです。
warning: 'Persistence' may not respond to '+saveData'
そのため、この問題を解決するまで、プロセス全体が使用できなくなります。
ここに私の完全な永続性クラスがあります(これは未完成なので、この問題を示すためだけにあることに注意してください):
持続性.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
永続性.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]];