2011-08-02 6 views
1

NSMutableArrayをpListに格納しようとしていますが、data.pListをチェックすると、内部に値はありません。どんな考え?NSMutableArraysをpListに保存する

-(void)run 
{ 
Global *myGlobal = [Global sharedGlobal]; 

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

NSMutableDictionary *category = [[NSMutableDictionary alloc]init]; 
NSMutableDictionary *totalCategory = [[NSMutableDictionary alloc]init]; 
for (int i = 0 ; i < [myGlobal.categoryArray count];i++){ 
    Category * c = [categoryArray objectAtIndex:i]; 

    [category setObject:c.name forKey:@"category"]; 

    [totalCategory setObject:category forKey:@"allCategory"]; 

    NSMutableDictionary *stock = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 
    [stock writeToFile:path atomically:YES]; 
} 
if(totalCategory==nil){ 
    NSLog(@"failed to retrieve dictionary from disk"); 
} 
} 

答えて

0

これらの2つの行は意味をなさない。最初の行はpathからstockに辞書を読み取り、2行目はそれを書き戻します。 stockには何も追加されていません。

NSMutableDictionary *stock = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 
[stock writeToFile:path atomically:YES]; 
+0

罰金どのように機能するかの例では、それがされていないのですか? –

+0

trueですが、 'stock'またはwriteToFileに何かを置く必要はありません。 – progrmr

0

pListに保存するときは、このようにしてください。

 
[totalCategory writeToFile:path atomically:YES]; 

これは私がライン[WRITETOFILE]それは文句を言わないのplistに私の辞書/配列を格納が含まれていない場合、それは

 

+ (void) saveParams:(int)kotuc one:(NSNumber*)one two:(NSNumber*)two three:(NSNumber*)three four:(NSNumber*)four five:(NSNumber*)five six:(NSNumber*)six seven:(NSNumber*)seven eight:(NSArray*)eight{ 

    NSString *path = [self getNSPath]; 
    NSDictionary* dw = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; 

    NSArray *keys = [[NSArray arrayWithObjects:@"one", @"two", @"three", @"four", @"five", @"six",@"seven", @"eight",nil] retain]; 
    NSArray *objects = [[NSArray arrayWithObjects:one, two, three, four, five, six, seven, eight, nil] retain]; 


    NSMutableDictionary *subDict = [[NSMutableDictionary alloc] initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys]; 


    ////save old 
    [dict setDictionary:dw]; 
    ////save new 
    [dict setObject:subDict forKey:[NSString stringWithFormat:@"%i", kotuc]]; // 
    //write to file 
    [dict writeToFile:path atomically:YES]; 


    [dw release]; 
    [dict release]; 
    [subDict release]; 
    [keys release]; 
    [objects release]; 
} 
+0

問題は、私は多くの問題をw/oこの行を実行することができますが、私は私のdata.plistをチェックするときに、そこに任意の値はありません。 –

+0

totalCategoryはデータで満たされていますか?あなたのコードに関する奇妙なことは、ループでデータを選択したいが、静的キー(categoryまたはallCategory)を設定し、任意の辞書のキーが一意であるとします。この方法では、辞書ごとにレコードにしかアクセスできないので、ループは必要ありません。 – Vanya

+0

yeah totalCategoryには7つの異なるカテゴリがあり、そのうちの7つをpListに保存したいと考えています。 pListについて優れた説明があるチュートリアルはありますか?ほとんどの人は、あなたのplistへのパスをどのように開始するかに関するコードを持っています。たとえば、documentディレクトリとstuff –

関連する問題