import SwiftUI
struct ContentView: View {
@State var pliststr:String = "a"
var body: some View {
VStack {
Text("Hello, World!")
Text(pliststr)
.onTapGesture {
self.pliststr = self.getPlist(name: "Property List")
}
}
}
func getPlist(name: String) -> String{
let unit_path = Bundle.main.path(forResource: name, ofType: "plist")
let unit_data:NSMutableDictionary = NSMutableDictionary.init(contentsOfFile: unit_path!)!
let unit_String = unit_data["a"]! as! String
unit_data.setValue("CCC", forKey: "a")
unit_data.write(toFile: unit_path!, atomically: true)
print(unit_String)
return unit_String
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Вот демоверсия r / w plist. Код выше может только сохранить "CCC" в оперативную память, но файл plist остается неизменным. Как сохранить данные в файл plist.