2012-05-01 7 views
0

私はPLISTに保存した配列を読み込み、要素を修正してPLISTに保存し直したいと思います。次のようにNSMutable配列をインデックスで編集する

私の単純なコードは次のとおりです。

NSArray *mathScoreArray; 
NSNumber *score1 = [NSNumber numberWithInteger:score]; 
NSMutableArray *scoreArray1 = [[NSArray arrayWithArray:mathScoreArray] init]; 
[scoreArray1 replaceObjectAtIndex:4 withObject:score1]; 
[dictionary setObject:scoreArray1 forKey :@"mathScoreArray"]; 
[dictionary writeToFile:finalPath atomically:YES]; 

私は配列を変更するための「replaceObjectAtIndex」のエラーを取得しています。 どうすればいいですか?

ただ、参考のために、私のplistは次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>mathQues</key> 
    <integer>1</integer> 
    <key>mathScore</key> 
    <integer>0</integer> 
    <key>mathScoreArray</key> 
    <array> 
     <integer>10</integer> 
     <integer>20</integer> 
     <integer>30</integer> 
     <integer>40</integer> 
     <integer>50</integer> 
    </array> 
</dict> 
</plist> 

だけのスタックのように[最終的に私が右に配列1位をシフトし、最上部に新しい着信値を格納したい、問題の拡張]。

ポインタありがとうございます。

NSMutableArray *scoreArray1 = [[NSArray arrayWithArray:mathScoreArray] init]; 
// you need NSMutableArray, not NSArray^
//       you already got an array, no need to initialize^

: ユーザー

答えて

3

変更するには、この行を

NSMutableArray *scoreArray1 = [NSMutableArray arrayWithArray:mathScoreArray]; 

はまた、あなたのコードでは、あなたがで開始するNSArrayを読まなかったかの兆候はありません。

+0

こんにちはビニャミン、お世話になりました。質問を投稿した後でコードをかなり変更しました。現在、PLISTには3つの要素、2つの数値、5つの要素を持つ1つの配列があります。私が望むのは、既存のスコアの配列をPLISTから読み込み、このスコアの配列の上に新しいスコアをプッシュしてPLISTに書き戻すことだけです。この文脈では、(PLISTからの)mathScoreArrayのNSMutableArrayが正しい選択であるかどうかはわかりません。ありがとう、ユーザー – user1314186

+0

歓迎、私が助けることができてうれしい:) – MByD

関連する問題