将字典添加到 plist 文件中的数组
Posted
技术标签:
【中文标题】将字典添加到 plist 文件中的数组【英文标题】:Adding dictionary to array in plist file 【发布时间】:2017-02-07 22:10:07 【问题描述】:我一直在尝试将字典添加到我的 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>
我已经在谷歌搜索了两个月,并试图将这些数据保存在我的 plist 文件中的一个函数中,如下所示。
import UIKit
class SecondViewController: UIViewController
var Titel = "Titel"
var Flussname = "Flussname"
var Ort = "Ort"
var Land = "Land"
var POIart = "POIart"
var Charakter = "Charakter"
var Mindestpegel = "Mindestpegel"
var Besonderheiten = "Besonderheiten"
var Pegellink = "Pegellink"
var Beschreibungslink = "Beschreibungslink"
var Fotolink = "Fotolink"
var Latitude = 1.123456
var Longitude = 1.123456
let TitelKey = "titel"
let FlussnameKey = "flussname"
let OrtKey = "ort"
let LandKey = "land"
let POIartKey = "poiart"
let CharakterKey = "charakter"
let MindestpegelKey = "mindestpegel"
let BesonderheitenKey = "besonderheiten"
let PegellinkKey = "pegellink"
let BeschreibungslinkKey = "beschreibungslink"
let FotolinkKey = "fotolink"
let LatitudeKey = "lat"
let LongitudeKey = "long"
@IBOutlet weak var I: UILabel!
@IBOutlet weak var II: UILabel!
@IBOutlet weak var III: UILabel!
@IBOutlet weak var IV: UILabel!
@IBOutlet weak var V: UILabel!
@IBOutlet weak var VI: UILabel!
@IBOutlet weak var VII: UILabel!
@IBOutlet weak var VIII: UILabel!
@IBOutlet weak var IX: UILabel!
@IBOutlet weak var X: UILabel!
@IBOutlet weak var XI: UILabel!
@IBOutlet weak var XII: UILabel!
@IBOutlet weak var XIII: UILabel!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
I.text = Titel
II.text = Flussname
III.text = Ort
IV.text = Land
V.text = POIart
VI.text = Charakter
VII.text = Mindestpegel
VIII.text = Besonderheiten
IX.text = Pegellink
X.text = Beschreibungslink
XI.text = Fotolink
@IBAction func Save(_ sender: Any)
self.saveData()
func saveData ()
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) [0] as String
let documentsDirectory = paths.appending("/Favoriten.plist")
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: documentsDirectory)
// Default plist name is Info. Just using ClassA
let bundle = Bundle.main.path(forResource: "Favoriten", ofType: "plist")
try! fileManager.copyItem(atPath: bundle!, toPath: documentsDirectory)
let data = NSMutableDictionary(contentsOfFile: documentsDirectory)
//saving values
data?.setObject(Titel, forKey: TitelKey as NSCopying)
data?.setObject(Flussname, forKey: FlussnameKey as NSCopying)
data?.setObject(Ort, forKey: OrtKey as NSCopying)
data?.setObject(Land, forKey: LandKey as NSCopying)
data?.setObject(POIart, forKey: POIartKey as NSCopying)
data?.setObject(Charakter, forKey: CharakterKey as NSCopying)
data?.setObject(Mindestpegel, forKey: MindestpegelKey as NSCopying)
data?.setObject(Besonderheiten, forKey: BesonderheitenKey as NSCopying)
data?.setObject(Pegellink, forKey: PegellinkKey as NSCopying)
data?.setObject(Beschreibungslink, forKey: BeschreibungslinkKey as NSCopying)
data?.setObject(Fotolink, forKey: FotolinkKey as NSCopying)
data?.setObject(Latitude, forKey: LatitudeKey as NSCopying)
data?.setObject(Longitude, forKey: LongitudeKey as NSCopying)
//writing to .plist
data?.write(toFile: documentsDirectory, atomically: true)
print(data)
请你给我一个例子或提示,我可能会找到一个教程,因为我真的迷路了。
提前感谢您的帮助
【问题讨论】:
【参考方案1】:plists 只保存极少数的数据类型。尝试将您的键和值都转换为NSString
s。还可以使用保护语句保护您对字典的阅读,并在失败时打印错误:
guard let data = NSMutableDictionary(contentsOfFile: documentsDirectory) else
print(Error reading data from file \(documentsDirectory)")
return
data[TitelKey as NSString] = Titel as NSString
【讨论】:
【参考方案2】:试试这样的例子:
// 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];
【讨论】:
OP 的问题使用了 Swift。你的答案是 Objective-C。无论如何,你不应该使用setValue:forKey:
那是一种KVO 方法。你应该使用setObject:forKey:
以上是关于将字典添加到 plist 文件中的数组的主要内容,如果未能解决你的问题,请参考以下文章
想要从 iPhone 的 plist 中的字典数组中获取数据? [复制]
如何使用 PlistBuddy 将多个条目添加到 plist 字典