ios - 是否可以从另一个类检索对象列表(从 JSON 文件创建)?

标签 ios json swift class variables

我是 iOS 编程新手,我有一个关于从另一个类构建列表的问题。让我解释 : 我正在尝试构建一个基本的自行车应用程序,它允许用户在 map 或表格 View 中查看所有自行车的位置。打开应用程序时,第一个 View 是 map 。我希望 map 上充满注释。事情是这样的:在 map View Controller 中,我调用在 TableView Controller 类中定义的 fetchStationData() ,它用自行车站对象填充全局变量。我想在 map VC中获取这个全局变量来放置我所有的注释。换句话说:我希望我的 stationList 在打开应用程序时仅构建一次,并且我希望能够在 TableView 和 map 上访问此列表中的数据,我不想从JSON 文件。

我尝试在获取数据后打印对象列表,但什么也没得到。

// In Map VC file
override func viewDidLoad() {
        super.viewDidLoad()
        StationListScreen().fetchStationData()
    }

private func setUpAnnotation() {
        for station in StationListScreen.stationsList {
            let location = station.location
            let annotation = MKPointAnnotation()
            annotation.title = station.stationName
            annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
            mapView.addAnnotation(annotation)
        }
    }

// In StationListScreen class
// Global var declaration

static var stationsList = [Station]()

// Fetching data function

func fetchStationData() {

        let locationManager = CLLocationManager()
        let location = locationManager.location

        var allStations = [Station]()

        let jsonStringUrl = URL(string: "https://opendata.paris.fr/api/records/1.0/search/?dataset=velib-disponibilite-en-temps-reel&rows=2000")

        URLSession.shared.dataTask(with: jsonStringUrl!) { (data, response, error) in
            guard let data = data else { return }

            do {
                guard let jsonObject = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else { return }
                guard let recordsObjects = jsonObject["records"] as? [[String: AnyObject]] else { return }
                for recordObject in recordsObjects {
                    if let fields = recordObject["fields"] as? [String: AnyObject],
                        let nbFreeEDock = fields["nbfreeedock"] as? Int,
                        let nbBikes = fields["nbbike"] as? Int,
                        let nbEBikes = fields["nbebike"] as? Int,
                        let stationName = fields["station_name"] as? String,
                        let geo = fields["geo"] as? [CLLocationDegrees]
                    {
                        allStations.append(Station(stationName: stationName,nbBikes: nbBikes, nbEBikes: nbEBikes, nbFreeDocks: nbFreeEDock, location: Location(latitude: geo[0], longitude: geo[1]), distance: Int(location!.distance(from: CLLocation(latitude: geo[0], longitude: geo[1]))) / 1000))
                    }
                }
                DispatchQueue.main.async {
                    StationListScreen.self.stationsList = allStations
                }
            }
            catch {
                return
            }
        }.resume()
    }

表格 View 加载了我需要的每个站点,因此对该函数的调用完成了它的工作,但是 map 上没有注释(我知道我的注释函数正在工作并且我调用了它,我刚刚在此处删除了调用)

最佳答案

为此目的使用单例类。将站点数据保存在该单例类中,并在 TableViewController 和 MapView 类中使用。

关于ios - 是否可以从另一个类检索对象列表(从 JSON 文件创建)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58531705/

相关文章:

android - 在 android 中将数据传递给 json 时单引号不起作用

javascript - 在迭代 JavaScript 对象的属性时修改它

ios - 在 Interface Builder 中使用泛型类作为自定义 View

swift - 何时以及如何在 Swift 中使用@noreturn 属性?

iphone - PhoneGap.exec() 在 JS 和 Obj-C 之间传递对象

iphone - 将捏合手势从自定义 UIView 传递给父 UIScrollView

javascript - 将网站重定向到未知的内部地址

ios - Objective-C NSString 类别 Getter/Setter 崩溃

ios - 无法将 json 类型的值分配给 NSArray 类型的值

ios - 如何在 Swift 中的 UIView/Utility 类中获取导航栏