\从plist读取时转换为\\

时间:2014-06-16 10:46:40

标签: ios objective-c macos cocoa-touch cocoa

我正在使用arrayWithContentsOfFile方法创建一个数组,如下所示:

NSArray *temp = [NSArray arrayWithContentsOfFile:mBackupSetFilePath];

上述语法适用于正常情况,但如果.plist包含C \ D,C \\ D等数据,则会转换为C \\ D,C \\\\ D.

enter image description here

将此数组传递给函数,在该函数中,这些值显示为C \\ D,C \\\\ D.而不是C \ D,C \\ D.

1 个答案:

答案 0 :(得分:0)

控制台日志正在消除转义字符。刚写完简单的测试来验证。

AppDelegate的内容:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSArray *plist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"]];
    [self takeArray:plist];
}

- (void)takeArray:(NSArray *)arr {
    NSLog(@"%@", arr);
}

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">
<array>
        <string>/Users/Test/C\ D</string>
        <string>/Users/Test/C\ E</string>
        <string>/Users/Test/C\ F</string>
</array>
</plist>

在控制台日志中NSLog将打印:

(
    "/Users/Test/C\\ D",
    "/Users/Test/C\\ E",
    "/Users/Test/C\\ F"
)

但是调试器显示并由函数使用的实际值将是:

(
/Users/Test/C\ D,
/Users/Test/C\ E,
/Users/Test/C\ F
)