首先,它是Swift約定來命名您的類和結構以大寫字母開頭。我建議使用結構而不是類。開始聲明所有的屬性常量並添加一個自定義初始值設定項,它將您的任何類型對象(IMO應該使用字典作爲源)的數組作爲參數。添加一個警戒語句以確保數組數等於6並正確初始化每個結構屬性。爲了讓持久性,你可以添加一個計算的屬性創建一個字典有自己的信息和其他財產,以你的JSON對象轉換成數據:
struct TramStop {
let tramDirection: String
let stopCode: String
let nameEnglish: String
let nameChinese: String
let latitude: Double
let longitude: Double
init?(array: [Any]) {
guard array.count == 6 else { return nil }
tramDirection = array[0] as? String ?? ""
stopCode = array[1] as? String ?? ""
nameEnglish = array[2] as? String ?? ""
nameChinese = array[3] as? String ?? ""
latitude = array[4] as? Double ?? 0
longitude = array[5] as? Double ?? 0
}
init(_ dictionary: [String: Any]) {
tramDirection = dictionary["tramDirection"] as? String ?? ""
stopCode = dictionary["stopCode"] as? String ?? ""
nameEnglish = dictionary["nameEnglish"] as? String ?? ""
nameChinese = dictionary["nameChinese"] as? String ?? ""
latitude = dictionary["latitude"] as? Double ?? 0
longitude = dictionary["longitude"] as? Double ?? 0
}
var dictionary: [String: Any] {
return [ "tramDirection" : tramDirection,
"stopCode" : stopCode,
"nameEnglish" : nameEnglish,
"nameChinese" : nameChinese,
"latitude" : latitude,
"longitude" : longitude
]
}
var data: Data { return (try? JSONSerialization.data(withJSONObject: dictionary)) ?? Data() }
var json: String { return String(data: data, encoding: .utf8) ?? String() }
}
,並創建一個結構來保存你的tramStops陣列和正確初始化(array2D和jsonData):
struct Stops {
let tramStops: [TramStop]
init(_ array2D: [[Any]]) {
tramStops = array2D.flatMap(TramStop.init)
}
init?(_ data: Data) {
guard let array = (try? JSONSerialization.jsonObject(with: data)) as? [[String: Any]]
else { return nil }
tramStops = array.map(TramStop.init)
}
var array: [[String: Any]] {
return tramStops.map{ $0.dictionary }
}
var data: Data {
return (try? JSONSerialization.data(withJSONObject: array)) ?? Data()
}
var json: String {
return String(data: data, encoding: .utf8) ?? String()
}
}
遊樂場測試
let array2D = [["a1", "a2", "a3", "a4", 1.0, 2.0],
["b1", "b2", "b3", "b4", 3.0, 4.0]]
let stops = Stops(array2D)
print(stops.json) // "[{"nameEnglish":"a3","latitude":1,"tramDirection":"a1","longitude":2,"stopCode":"a2","nameChinese":"a4"},{"nameEnglish":"b3","latitude":3,"tramDirection":"b1","longitude":4,"stopCode":"b2","nameChinese":"b4"}]"
let jsonURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("TramStop.json")
stops.data // 209 bytes
for tramStop in stops.tramStops {
print(tramStop.dictionary)
print(tramStop.stopCode)
print(tramStop.nameEnglish)
print(tramStop.nameChinese)
print(tramStop.latitude)
print(tramStop.longitude)
}
do {
try stops.data.write(to: jsonURL)
print("json data saved")
} catch {
print(error)
}
從fileURL讀取json數據:
do {
let data = try Data(contentsOf: jsonURL)
print(String(data: data, encoding: .utf8) ?? "")
if let stops = Stops(data) {
for tramStop in stops.tramStops {
print(tramStop.tramDirection)
print(tramStop.stopCode)
print(tramStop.nameEnglish)
print(tramStop.nameChinese)
print(tramStop.latitude)
print(tramStop.longitude)
}
}
} catch {
print(error)
}
是否要將它們存儲在文本文件中?帖子的標題與您提到的不同。請清楚說明 – user1046037
@ user1046037是的,這是詢問,編輯標題的更好方式。 – Kevin
@Kevin你有沒有試過我的建議保存你的結構爲json? –