-1

plist がdocuments directory存在しない場合はコピーされます。

既に存在する場合は、 の「名前」キーを使用して で一致するものを見つけたいNSDictionarybundleArray思いNSDictionaryますdocumentsArray

一致が見つかったら、文字列の変更を確認し、変更があれば置き換えたいと考えています。

一致するものが見つからない場合は、この辞書をドキュメントの plist に追加する必要があることを意味します。

これは私のコードです:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self managePlist];
return YES;
}

- (void)managePlist {

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Objects.plist"];
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Objects" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {
    [fileManager copyItemAtPath:bundle toPath:path error:&error];

} else {

    NSArray *bundleArray = [[NSArray alloc] initWithContentsOfFile:bundle];
    NSMutableArray *documentArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

    BOOL updateDictionary = NO;
    for(int i=0;i<bundleArray.count;i++) {

        NSDictionary *bundleDict=[bundleArray objectAtIndex:i];

        BOOL matchInDocuments = NO;
        for(int ii=0;ii<documentArray.count;ii++)
        {
            NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii]; 
            NSString *bundleObjectName = [bundleDict valueForKey:@"Name"];
            NSString *documentsObjectName = [documentDict valueForKey:@"Name"];
            NSRange range = [documentsObjectName rangeOfString:bundleObjectName options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound) {
                matchInDocuments = YES;
            }
            if (matchInDocuments) {

                if ([bundleDict objectForKey:@"District"] != [documentDict objectForKey:@"District"]) {
                    [documentDict setObject:[bundleDict objectForKey:@"District"] forKey:@"District"];
                    updateDictionary=YES;
                }
            }

            else {
                [documentArray addObject:bundleDict];
                updateDictionary=YES;
            }
        }
    }
    if(updateDictionary){
        [documentArray writeToFile:path atomically:YES];
    }

}

}

今アプリを実行すると、次のメッセージが表示されます。'-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

どうすればこれを修正できますか?

これが修正されたら、私のコードは機能すると思いますか?

If not, I would be happy for some suggestions on how to do this. I have struggled for a while and really need to publish the update with the corrections! Thanks a lot for your help.

4

3 に答える 3

3

おそらく、documentDictは実際には不変です。に割り当ててもNSMutableDictionary、基になるデータを正確に記述していません。

NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii];

する必要があります:

NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithDictionary:[documentArray objectAtIndex:ii]];

そして、あなたが編集するところはどこでもdocumentDict、追加します:

 [documentArray replaceObjectAtIndex:ii withObject:documentDict];
于 2012-09-16T21:07:44.760 に答える
0

ファイルからplistを読み取ると、不変の子を持つ不変の辞書/配列が作成されるため、

  NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii]; 

行は完全に嘘です-あなたは変更可能な辞書を持っていません。plistから可変オブジェクトを作成するには、CFPropertyListCreateWithDataを確認し、可変オプションとして指定kCFPropertyListMutableContainersAndLeavesします。

于 2012-09-16T21:11:01.277 に答える
0

これは、James Webster からの回答を使用した最終的な作業コードです。貢献してくれた H2CO3 にも感謝します。

- (void)managePlist {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Object.plist"];
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Object" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
    [fileManager copyItemAtPath:bundle toPath:path error:&error];
} 
else 
{
    NSArray *bundleArray = [[NSArray alloc] initWithContentsOfFile:bundle];
    NSMutableArray *documentArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

    BOOL updateDictionary = NO;
    for(int i=0;i<bundleArray.count;i++) {
        NSDictionary *bundleDict=[bundleArray objectAtIndex:i];            

        for(int ii=0;ii<documentArray.count;ii++)
        {
            NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithDictionary:[documentArray objectAtIndex:ii]];
            NSString *bundleObjectName = [bundleDict valueForKey:@"Name"];
            NSString *documentsObjectName = [documentDict valueForKey:@"Name"];
            NSRange range = [documentsObjectName rangeOfString:bundleObjectName options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound) {
                 NSString *districtString = [bundleDict objectForKey:@"District"];
                if ([documentDict objectForKey:@"District"] != districtString) {
                    [documentDict setObject:districtString forKey:@"District"];
                    [documentArray replaceObjectAtIndex:ii withObject:documentDict];
                    updateDictionary=YES;
                }
            }
        }
    }
    if(updateDictionary){
        [documentArray writeToFile:path atomically:YES];
    }
}
}
于 2012-09-17T18:39:12.783 に答える