The path you mentioned, /Users/wargadex/Library/Cookies/HTST.plist, looks like a macOS path, suggesting that youâre targeting macOS. If so, you may run into privilege problems here. On modern versions of macOS, the ~/Library/Cookies/ directory is a data vault, which is inaccessible even to programs running as root.
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.5
BuildVersion: 18F132
$
$ ls -lh ~/Library/Cookies/
ls: : Operation not permitted
$
$ sudo ls -lh ~/Library/Cookies/
Password: ********
ls: : Operation not permitted
import Foundation
let url = URL(fileURLWithPath: "~/Desktop/foo.plist")
let data = try Data(contentsOf: url)
let encoder = PropertyListEncoder()
let decoder = PropertyListDecoder()
var dict = try decoder.decode([String:String].self, from: data)
dict["textField"] = "a"
let newData = try encoder.encode(dict)
try newData.write(to: url)
import Foundation
let url = URL(fileURLWithPath: "~/Desktop/foo.plist")
let data = try! Data(contentsOf: url)
let encoder = PropertyListEncoder()
let decoder = PropertyListDecoder()
var dict = try! decoder.decode([String:String].self, from: data)
dict["textField"] = "a"
let newData = try! encoder.encode(dict)
try! newData.write(to: url)
@Daniel_Mullenbornâs answer is the general answer for 1. and 2. (But @eskimoâs caveat regarding the specific path is correct.)
The standard and core libraries are basically the same on all platforms, and they are where all the functionality from @Daniel_Mullenbornâs examples come from. It should work on all platforms that Swift supports, which are macOS, Linux, iOS, watchOS and tvOS. Android and Windows support is currently a work in progress, so while everything should eventually work, sometimes bits and pieces are still missing, and it is far more complicated to get Swift set up on them in the first place.
But the user interface is not part of Swift, so it varies heavily depending on the platform. You need a Swiftâenabled user interface library to do this:
Show some lines of this file, for example, on text field in a window.
Since you are dealing with macOS, the answer for that is to look into AppKit (or any day now SwiftUI will be released), which are both easy to use. But if you were wanting to use user interface elements on the Windows operating system, then I cannot even guarantee that it is even possible yet, let alone explain how to do it.
Your example doesn't work, I have similar question how to write in a plist file.
Someone says that the Bundle.main.path can read data, but it can not write ,so I use file manager and encode to write ,but is doesn't work also, could you help me to look?
Iâm going to recommend that you start a new thread for this; the problem youâre having is very unlikely to be related to the one that tripped up Wargadex.
While youâre doing this, please expand on âdoesn't workâ. Does it not compile? Fail with an error? Crash? Produce the wrong results?