我一直在尝试将字典添加到我的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)
}
请你给我一个例子或小费,我可能会找到一个教程,因为我真的迷路了。
提前感谢您的帮助
答案 0 :(得分:0)
plists只保存极少数数据类型。尝试将密钥和值都投射到NSString
s。还使用guard语句保护您对字典的读取,如果失败则打印错误:
guard let data = NSMutableDictionary(contentsOfFile: documentsDirectory) else {
print(Error reading data from file \(documentsDirectory)")
return
}
data[TitelKey as NSString] = Titel as NSString
答案 1 :(得分:-1)
尝试类似这样的事情:
<div class="homepage-wrapper">
<div class="homepage-top-category-container">
<div class="homepage-top-category-container-title">
<span id="homepage-top-category-container-title">Most popular aisles</span>
</div>
<div class="homepage-top-category-container-list">
<div class="homepage-top-category-container-item" id="homepage-top-category-container-item-a">
block A
<div class="test">
button
</div>
</div>
<div class="homepage-top-category-container-item" id="homepage-top-category-container-item-b">
block B
</div>
<div class="homepage-top-category-container-item" id="homepage-top-category-container-item-c">
block C
</div>
<div class="homepage-top-category-container-item" id="homepage-top-category-container-item-d">
block D
</div>
</div>
</div>
</div>