Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将字典添加到plist文件中的数组_Ios_Arrays_Dictionary_Swift3 - Fatal编程技术网

Ios 将字典添加到plist文件中的数组

Ios 将字典添加到plist文件中的数组,ios,arrays,dictionary,swift3,Ios,Arrays,Dictionary,Swift3,我一直在尝试将字典添加到我的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> <dict> <key

我一直在尝试将字典添加到我的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>
  <dict>
    <key>id</key>
    <string>4</string>
    <key>inlat</key>
    <real>49.7930491922698</real>
    <key>inlong</key>
    <real>9.92656307493522</real>
    <key>titel</key>
    <string>Würzburg</string>
    <key>flussname</key>
    <string>Main</string>
    <key>ort</key>
    <string>Würzburg</string>
    <key>land</key>
    <string>Deutschland</string>
    <key>poiart</key>
    <string>Playspot</string>
    <key>charakter</key>
    <string>Welle</string>
    <key>mindestpegel</key>
    <string>-</string>
    <key>besonderheiten</key>
    <string>Hochwasserspot</string>
    <key>pegellink</key>
    <string>www.xxx.html</string>
    <key>beschreibungslink</key>
    <string>www.xxx.html</string>
    <key>fotolink</key>
    <string>www.xxx.html</string>
    <key>outlat</key>
    <real>48.7930491922698</real>
    <key>outlong</key>
    <real>8.92656307493522</real>
   </dict>
  </dict>
</plist>
请你给我一个例子或提示,我可能会找到一个教程,因为我真的迷路了


提前感谢您的帮助

尝试以下方法,例如:

// Create the new dictionary that will be inserted into the plist.
NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:@"FirstName LastName" forKey:@"fullName"];
[nameDictionary setValue:@"123 North Rd" forKey:@"address"];

// Open the plist from the filesystem.
NSMutableArray *plist = [NSMutableArray      arrayWithContentsOfFile:@"/path/to/file.plist"];
if (plist == nil) plist = [NSMutableArray array];
[plist addObject:nameDictionary];
[plist writeToFile:@"/path/to/file.plist" atomically:YES];

plists只保存非常少量的数据类型。尝试将密钥和值强制转换为NSString。此外,还可以使用guard语句保护您对词典的阅读,并在失败时打印错误:

guard let data = NSMutableDictionary(contentsOfFile: documentsDirectory) else {
  print(Error reading data from file \(documentsDirectory)")
  return
}

data[TitelKey as NSString] = Titel as NSString

OP的问题使用Swift。您的答案是Objective-C。无论如何,您不应该使用setValue:forKey:这是一种KVO方法。您应该使用setObject:forKey:
guard let data = NSMutableDictionary(contentsOfFile: documentsDirectory) else {
  print(Error reading data from file \(documentsDirectory)")
  return
}

data[TitelKey as NSString] = Titel as NSString