0

私はステッパーを持っています。その値の変更中に、値をplistに保存する必要があります。

したがって、saveAppPlist メソッドに従って、変更されたファイルで NSMutableDictionary を渡す必要があります -> dictData

NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dictData format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
    [plistData writeToFile:plistPath atomically:YES];
}

ステッパーのメソッドは次のとおりです。

-(void)changeItemQuantityAtRow:(int)row
                       toValue:(double)value
{
    NSMutableDictionary *item = [[items objectAtIndex:row] mutableCopy];
    [item setObject:[NSNumber numberWithDouble:value] forKey:@"Quantity"];
    [items setObject:item atIndexedSubscript:row];
    NSLog(@"%@", items);
    NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:items forKeys:[NSArray arrayWithObjects:@"Name", @"Price", @"Quantity", nil]];
    NSLog(@"%@", plistDict);
    [self saveAppPlist:plistDict];
}

plist は次のようになります (これは 4 つの項目のうちの 1 つにすぎません)。

<dict>
    <key>Items</key>
    <array>
        <dict>
            <key>Name</key>
            <string>The zero item</string>
            <key>Price</key>
            <integer>133</integer>
            <key>Quantity</key>
            <integer>17</integer>
        </dict>

ステッパーがアイテムの数量を変更しています。しかし、数量の値を変更しようとすると、次のようなエラーが発生します。

'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (4) differs from count of keys (3)'

エラーの原因はわかりましたが、この問題を解決する方法がわかりません。誰が私を助けることができます?もう 1 つ - ステッパーをタップするたびに値を保存するのはあまり良くないのでしょうか?

4

3 に答える 3

2

配列を格納することを忘れて、オブジェクトをidインスタンスと考えてください。id itemsキーで保存したいので@"Items"、やりたいことは使用することです

//[NSDictionary dictionaryWithObject:<#(id)#> forKey:<#(id<NSCopying>)#>]
NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObject:items forKey:@"Items"];
于 2013-04-01T10:02:04.927 に答える
1

pListファイルにデータを書きたい場合の簡単な方法は次のとおりです

plist ファイル名は「Contacts.plist」です

  1. 名前
  2. 住所
  3. 電話番号。

以下のリンクで、plist ファイルの操作方法に関するコンパイル チュートリアルを見つけることができます。

iPhoneチュートリアルでPListファイルを使用する方法

以下はコード例です

- (void)saveDataInPlist {

    //get the plist document path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"];    

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
    NSMutableArray *contentArray= [[NSMutableArray alloc]init];

    if (![fileManager fileExistsAtPath: plistFilePath])
    {
        NSLog(@"File does not exist");

        // If the file doesn’t exist, create an empty plist file        
        plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"];
        //NSLog(@"path is %@",plistFilePath);

    }
    else{
        NSLog(@"File exists, Get data if anything stored");

        contentArray = [[NSMutableArray alloc] initWithContentsOfFile:plistFilePath];       
    }       


    NSString *name = nameTextField.text;
    NSString *address = addressTextField.text;
    NSString *phone = phoneTextField.text;


    //add values to dictionary
    [data setValue:name forKey:@"Name"];
    [data setValue:address forKey:@"Address"];
    [data setValue:phone forKey:@"PhoneNo"];

    //add dictionary to array
    [contentArray addObject:data];

    //write array to plist file
    if([contentArray writeToFile:plistFilePath atomically:YES]){

       NSLog(@"saved");  
     }
     else {
       NSLog(@"Couldn't saved");
    }
    }

}

これがあなたを助けることを願っています。

于 2013-04-01T10:02:19.787 に答える