ファイルとして保存してアプリケーションに再ロードするオブジェクトの配列があります。ファイルを保存していますが(データが入っていますが)、NSMutable配列に読み戻すことができません。だから今、私がしようと...NSMutableArrayにNSDataを取得できません
- (void) saveMyOptions {
// Figure out where we're going to save the app's data files
NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user
// Figure out if that directory exists or not
BOOL isDir;
NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
// If the directory doesn't exist, create it
if (!isDir)
{
[fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:NULL];
}
// Assemble everything into an array of objects with options
NSMutableArray *savedPreferences = [[NSMutableArray alloc] init];
myModel *saveOptions = nil;
for (int i; i < [otherArray count]; i++)
{
saveOptions = [[myModel alloc] init];
[saveOptions setName:@"Some String"];
[saveOptions setNumber:i];
[savedPreferences addObject:saveOptions];
saveOptions = nil;
}
// Actually save those options into a file
NSData* saveData = [NSKeyedArchiver archivedDataWithRootObject:savedPreferences];
NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath];
NSError *error = nil;
BOOL written = [saveData writeToFile:fileName options:0 error:&error];
if (!written)
{
NSLog(@"Error writing file: %@", [error localizedDescription]);
}
}
@implementation myModel
@synthesize name;
@synthesize number;
-(void) encodeWithCoder: (NSCoder *) encoder
{
[encoder encodeObject:name forKey:@"name"];
[encoder encodeInteger:number forKey:@"number"];
}
-(id) initWithCoder: (NSCoder *) decoder
{
name = [decoder decodeObjectForKey:@"name"];
number = [decoder decodeIntegerForKey:@"number"];
return self;
}
@end
は、だから私は、私はそれを保存し、これらのオブジェクトの配列を作成します。
オブジェクトはNSCodingプロトコルに準拠したモデルですそのデータを配列に戻します。これが崩壊していると思う場所です...
- (NSMutableArray *) loadOptions {
// Create file manager object
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSData *saveData = nil;
// Find user directory path
NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user
// Assign file name
NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath];
// Create options array
NSMutableArray *myOptions = nil;
// If the file exists, fill the array with options
if ([fileManager fileExistsAtPath:fileName])
{
saveData = [NSData dataWithContentsOfFile:fileName];
myOptions = [NSKeyedUnarchiver unarchiveObjectWithData:saveData];
}
NSLog(@"%lu", [myOptions count]); // This ALWAYS reports 0!
NSLog(@"%lu", [saveData length]); // This reports a value of 236;
return myOptions;
}
誰かが私が間違っている方向に私を向けることができますか?私はあなたがあなたのencodeWithCoder:
とinitWithCoder:
方法でsuper
のコールが欠落している!
スーパーは間違いなくそれの一部でした。有難うございます! 2番目の部分は、馬鹿のように、保存したいオブジェクトで配列を塗りつぶすときにi = 0を割り当てなかったからです。その結果、私は何もなかったのでループは決して走りませんでした...ニース。あなたの助けをありがとう! –
不足している 'i'アサインも見られませんでした。 –