简体   繁体   English

为什么将此NSMutableArray写入plist无效?

[英]Why doesen't it work to write this NSMutableArray to a plist?

edited. 编辑。

Hey, I am trying to write an NSMutableArray to a plist. 嗨,我正在尝试将NSMutableArray写入plist。 The compiler does not show any errors, but it does not write to the plist anyway. 编译器不会显示任何错误,但是无论如何它不会写入plist。 I have tried this on a real device too, not just the Simulator. 我也曾在真实设备上尝试过此操作,而不仅仅是模拟器。

Basically, what this code does, is that when you click the accessoryView of a UITableViewCell , it gets the indexPath pressed, edits an NSMutableArray and tries to write that NSMutableArray to a plist. 基本上,这段代码所做的,就是当你点击accessoryView一个的UITableViewCell ,它得到了indexPath压,编辑一个NSMutableArray ,并尝试写NSMutableArray到plist中。 It then reloads the arrays mentioned (from multiple plists) and reloads the data in a UITableView from the arrays. 然后,它从多个插件重新加载提到的数组,并从数组中重新加载UITableView的数据。

Code: 码:

NSIndexPath *indexPath = [table indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:table]];

[arrayFav removeObjectAtIndex:[arrayFav indexOfObject:[NSNumber numberWithInt:[[arraySub objectAtIndex:indexPath.row] intValue]]]];

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"arrayFav.plist"];
NSLog(@"%@ - %@", rootPath, plistPath); 


[arrayFav writeToFile:plistPath atomically:YES];    

// Reloads data into the arrays
[self loadDataFromPlists];

// Reloads data in tableView from arrays
[tableFarts reloadData];

CFShow() on the array after removing one of them shows this: 删除其中之一 ,数组上的CFShow()显示如下:

<CFArray 0x6262110 [0x2c810a0]>{type = mutable-small, count = 4, values = (
    0 : <CFNumber 0x6502e10 [0x2c810a0]>{value = +3, type = kCFNumberSInt32Type}
    1 : <CFNumber 0x6239de0 [0x2c810a0]>{value = +8, type = kCFNumberSInt32Type}
    2 : <CFNumber 0x6239dc0 [0x2c810a0]>{value = +10, type = kCFNumberSInt32Type}
    3 : <CFNumber 0x6261420 [0x2c810a0]>{value = +40, type = kCFNumberSInt64Type}

DEBUG-INFO: writeToPlist shows YES, I have tried to release all the arrays before filling them up again, setting them to nil , set atomically to NO. 调试信息: writeToPlist显示为YES,我尝试释放所有数组,然后再次填充它们,将它们设置为nilatomically设置为NO。

[yourMutableArray writeToFile:fileName atomically:YES];

This should work. 这应该工作。 NSMutableArray inherits from NSArray which has a method to write to a plist. 的NSMutableArray从NSArray的具有继承方法写入一个plist中。

As discussed in the comments below, the actual problem here is that the plist is being read from and written to two different locations. 如下面的评论中所述,这里的实际问题是正在从两个不同的位置读取plist并将其写入两个不同的位置。 Somewhere in the app, there is code that reads the file into the array similar to this: 在应用程序中的某个位置,有类似以下代码的代码将文件读取到数组中:

NSString *plistFavPath = [[NSBundle mainBundle] pathForResource:@"arrayFav" 
                                                         ofType:@"plist"]; 
arrayFav = [[NSMutableArray alloc] initWithContentsOfFile:plistFavPath];

This logic reads the array from the application's bundle, which is a read-only location and part of the distributed app. 此逻辑从应用程序包中读取数组,该包是只读位置,是分布式应用程序的一部分。 Later when the edited array is persisted, code similar to this is used: 稍后,当持久保存已编辑的数组时,将使用与此类似的代码:

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask,
                                                          YES) objectAtIndex:0];
NSString *plistPath = [rootPath 
                          stringByAppendingPathComponent:@"arrayFav.plist"];
NSLog(@"%@ - %@", rootPath, plistPath); 
[arrayFav writeToFile:plistPath atomically:YES];   

The result here is that the updated file gets written to the app's documents directory, but it is never read from there, giving the appearance that the file is not being saved correctly. 结果是更新后的文件被写入应用程序的documents目录,但从那里从未读取过文件,看上去文件没有正确保存。 To correct this, you should change the code that reads the file to use the same path that you are writing to. 若要更正此问题,您应该更改读取文件的代码,以使用与写入相同的路径。

If you need to distribute a default version of the plist for use on the initial launch before the array has been edited, you could continue to include a version of the file in your bundle and then add code to your app delegate that check if the file exists in the documents directory and if it is not present, copies the bundle's default version of the file to the proper place. 如果您需要分发plist的默认版本以在编辑阵列之前在首次启动时使用,则可以继续在包中包含文件的版本,然后将代码添加到应用程序委托中,以检查文件是否存在于documents目录中,如果不存在,则将捆绑软件的默认版本复制到正确的位置。

writeToFile:atomically: won't work if your array contains custom objects. writeToFile:atomically:如果您的数组包含自定义对象,则无法使用。

If your array contains custom objects that are not Plist objects (NSArray, NSDictionary, NSString, NSNumber, etc), then you will not be able to use this method. 如果您的数组包含不是Plist对象的自定义对象(NSArray,NSDictionary,NSString,NSNumber等),则将无法使用此方法。 This method only works on Plist objects. 此方法仅适用于Plist对象。

Another option would be to use the NSCoding protocol, and write your objects to disk that way. 另一种选择是使用NSCoding协议,然后以这种方式将对象写入磁盘。

Yes

Look at the Property List Programming Guide . 请参阅《 属性列表编程指南》

phoneNumbers is a NSMutableArray phoneNumbers是一个NSMutableArray

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
    NSString *error;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
            [NSArray arrayWithObjects: personName, phoneNumbers, nil]
            forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                            format:NSPropertyListXMLFormat_v1_0
                            errorDescription:&error];
    if(plistData) {
        [plistData writeToFile:plistPath atomically:YES];
    }
    else {
        NSLog(error);
        [error release];
    }
    return NSTerminateNow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM