1

そのため、アプリケーションの終了時にカスタムオブジェクトの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に設定しています。

4

1 に答える 1

1

オブジェクトが初期化される前に、オブジェクトに値を設定しようとしています。そして、初期化により値がリセットされます。

-(Course *)initWithName:(NSString *)courseName {
    name = [courseName retain];              // <- Accessing ivar before self is initialized
    grade = -1.0;                            // <- Accessing ivar before self is initialized
    totalWeight = 0.0;                       // <- Accessing ivar before self is initialized
    list = [[NSMutableArray alloc] init];    // <- Accessing ivar before self is initialized
    [super init];                            // initialization resets your values !!!!
    return self;
}

さらに、super の init 戻り値を無視しています。これはすべてのケースで 98% 正常に機能しますが、常に適切な初期化スキームを使用することをお勧めします。

- (id)init {
    if (self = [super init]) {
        // It's save to access ivars here
    }
    return self
}

Cocoa では、init メソッドは別のオブジェクトを返す場合があり、次に割り当てられたオブジェクトを返します。したがって、スーパーの init に self を割り当てる必要があります。

したがって、init は次のようになります。

-(Course *)initWithName:(NSString *)courseName {
    if (self = [super init]) {
        name = [courseName retain];
        grade = -1.0;
        totalWeight = 0.0;
        list = [[NSMutableArray alloc] init];
    }
    return self;
}

についても同様initWithCoder:(NSCoder *)coderです。

于 2010-12-08T10:12:43.613 に答える