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 个项目之一):

<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)'

我了解错误的根源,但不知道如何解决此问题。谁能帮我?还有一个 - 每次点击步进器后存储值可能不太好?

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 回答
1

据我所知,问题出在字典结构上。您提供items作为值(每个项目可能包含Name,PriceQuantity键)并作为键提供item键。您可能需要将结构更改为: [NSDictionary dictionaryWithObjects:@[items] forKeys:@[@"Items"]]

于 2013-04-01T10:11:35.743 回答