-3

以前に NSuserDefaults と NSkeyedArchive を使用したことがありますが、新しいプロジェクトではうまくいかないと思います..

JSON からデータを取得し、配列 (名前、年齢、国) に格納します (すべて NSString)

詳細ビューに保存ボタンを作り、その人のデータを保存したいです。

保存したデータを別のテーブルビューで表示します。(配列の for ループとすべてのオブジェクトを取得)

これを簡単な方法で処理するにはどうすればよいですか..最大40個の保存された名前を除いて、それほど重くありません..

要するに、「家をお気に入り/保存」できる「ホームアプリ」のような機能が欲しい

- アップデート

viewDidLoad

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"Names.plist"];

NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

arrayWithNames = [[NSMutableArray alloc]init];
[arrayWithNames addObjectsFromArray:array];

保存ボタン

NSMutableArray *nameInfo = [[NSMutableArray alloc]initWithObjects:self.name,self.age,self.country, nil];

[arrayWithNames addObjectsFromArray:nameInfo];



NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Names.plist"];


[arrayWithNames writeToFile:path atomically:YES];

これは機能しますが、すべての配列ではなく、すべてのデータを独立したオブジェクトとして取得します

ところで、NULLが存在しないことを確認しました:)

4

2 に答える 2

0

私はあなたの質問をよく理解していません。

しかし、あなたの場合、私がしたことは、私が保存しようとしていた情報の構造を持つモデルを作成し(あなたの場合はPersonに見えました)、オブジェクトPersonを追加する配列を作成することでした

いくつかのケースを使用して保存できますが、私の意見では、最も簡単なのは NSUserDefaults を使用することです (解決策はデータベースに大きく依存します)。

スー、あなたはモデルの人を持っています

import <Foundation/Foundation.h> 
@interface Person : NSObject


@property(nonatomic,strong) NSString *name;
@property(nonatomic,strong) NSString *country;
@property(nonatomic,strong) NSString *age;
...

暗号化の方法:

- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeObject:self.age forKey:@"age"];
    [encoder encodeObject:self.country forKey:@"country"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.name = [decoder decodeObjectForKey:@"name "];
        self.age = [decoder decodeObjectForKey:@"age"];
        self.country = [decoder decodeObjectForKey:@"country"];

    }
    return self;
}

次に、オブジェクトを追加する NSMutableArray を作成します。

[arrayPeople addObject:person];

アプリケーション データに保存する場合は、次の操作を実行できます。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSData *myEncodedObjectShopping = [NSKeyedArchiver archivedDataWithRootObject:arrayPeople];
[defaults setObject:myEncodedObjectShopping forKey:@"people"];

データを取得するには:

NSData *myDecodedObject = [defaults objectForKey:@"people"];
NSMutableArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];
于 2013-06-27T17:38:04.290 に答える