そのため、アプリケーションの終了時にカスタムオブジェクトのNSMutableArray(Course Plannerアプリの大学のコースを表す「Course」)をファイルに書き込んでから、その配列をファイルから関連するViewControllerに読み込んで使用しようとしています。アプリケーションの起動時のデータの。
関連するコードは次のとおりです。
CoursesAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
coursesViewController = [[SampleHomeScreen alloc] initWithNibName:@"SampleHomeScreen" bundle:nil];
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[coursesViewController setCourses:[NSKeyedUnarchiver unarchiveObjectWithFile: filePath]];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)name:UIApplicationWillTerminateNotification object:app];
[window addSubview:coursesViewController.view];
[window makeKeyAndVisible];
return YES;
}
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSLog(@"%@", path);
return path;
}
/**
applicationWillTerminate: saves changes in the application's managed object context before the application terminates.
*/
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"%@", [coursesViewController courses]);
[NSKeyedArchiver archiveRootObject:[coursesViewController courses] toFile:[self dataFilePath]];
}
Course.h:
@interface Course : NSObject <NSCoding> {
NSString *name; //e.g. ECS 189H
double grade, totalWeight; //course grade in %
NSMutableArray *list;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic) double grade, totalWeight;
@property (nonatomic, retain) NSMutableArray *list;
-(Course *)initWithName:(NSString *)courseName;
@end
Course.m:
@implementation Course
@synthesize name, grade, totalWeight, list;
-(Course *)initWithName:(NSString *)courseName {
name = [courseName retain];
grade = -1.0;
totalWeight = 0.0;
list = [[NSMutableArray alloc] init];
[super init];
return self;
}
-(Course *)initWithCoder:(NSCoder *)aDecoder {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.grade = [aDecoder decodeDoubleForKey:@"grade"];
self.totalWeight = [aDecoder decodeDoubleForKey:@"totalWeight"];
self.list = [aDecoder decodeObjectForKey:@"list"];
[super init];
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:name forKey:@"name"];
[coder encodeDouble:grade forKey:@"grade"];
[coder encodeDouble:totalWeight forKey:@"totalWeight"];
[coder encodeObject:list forKey:@"list"];
}
-(void)dealloc {
[name release];
[list release];
[super dealloc];
}
@end
[coursesViewControllercourses]は、コースオブジェクトを保持するNSMutableArrayです。私はそれが有効なデータを保持しているという事実を知っています。
したがって、問題は次のとおりです。1:アプリケーションはxcodeから実行した場合にのみdata.plistに保存されます(つまり、xcodeで[コンパイルして実行]をクリックします)。2:plistからデータをロードしますが、保存されるのはコース名と、gradeとtotalWeightのデフォルト値(それぞれ-1と0)だけです。したがって、実際には、initWithNameが最初に呼び出されたかのように保存されます。
これは、かなり高度なiOSアプリケーションについての私の最初の本当の掘り下げです。したがって、私はこれの初心者なので、いくつかの重要な情報を省略している可能性があります。その場合はお知らせください。質問を更新します。
ありがとう!-HT
ps関連する場合は、info.plistのdoNotRunInBackgroundをtrueに設定しています。