Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active January 11, 2021 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/3135b388836fc050e86fec710b0f1187 to your computer and use it in GitHub Desktop.
Save sturdysturge/3135b388836fc050e86fec710b0f1187 to your computer and use it in GitHub Desktop.
struct ContentView: View {
static func getPlist(withName name: String) -> NSDictionary?
{
guard let path = Bundle.main.path(forResource: name, ofType: "plist"),
let xml = FileManager.default.contents(atPath: path),
let dictionary = try? PropertyListSerialization.propertyList(
from: xml, options: .mutableContainersAndLeaves, format: nil) as? NSDictionary
else { return nil }
return dictionary
}
static let plist = getPlist(withName: "Property List")
static let person0 = Self.plist?["Person 0"] as? NSDictionary
let name = person0?["Name"] as? String
let birthday = person0?["Birthday"] as? Date
let lovesSwift = person0?["Loves Swift"] as? Bool
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .full
formatter.timeStyle = .short
return formatter
}
var body: some View {
VStack {
Text("Name: \(name ?? "No name")")
Text("Birthday: \(dateFormatter.string(from: birthday ?? Date()))")
Text("Loves Swift: \(lovesSwift ?? false ? "YES!" : "No")")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment