釣魚術
swift
swiftui
JSON
MySecretLocations.json
扔進 BaoAnGongFisher
專案目錄中。之後就是從這個格式進行讀寫//
// SecretLocationsDataLoader.swift
// BaoAnGongFisher
//
// Created by nipapa on 2022/9/24.
//
import Foundation
import CoreLocation
public class LocationsLoader {
@Published var originLoadData = [FishPinAnnotation]()
@Published var locationData = [PinLocation]()
init() {
loadDataFromFile()
}
func loadDataFromFile() {
if let myLocationFile = Bundle.main.url(forResource: "MySecretLocations", withExtension: "json") {
// 如果上面的 if let statement 不成立則不執行
do {
let data = try Data(contentsOf: myLocationFile)
let jsonDecoder = JSONDecoder()
let dataFromJson = try jsonDecoder.decode([FishPinAnnotation].self, from: data)
self.originLoadData = dataFromJson
} catch {
print(error)
}
}
}
func transferCoordinate () {
for pin in self.originLoadData {
var latitude = pin.latitude
var longitude = pin.longitude
// var coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
print(latitude)
print(longitude)
}
}
}
struct FishPinAnnotation: Codable {
var name: String
var image: String
var latitude: Double
var longitude: Double
var rank: Int
}