为什么我的数组在加载后变空了? [英] Why does my array empty out after it's loaded?

查看:117
本文介绍了为什么我的数组在加载后变空了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须缺少一些基本知识,但似乎无法保持阵列加载.它加载成功,但是在另一个函数中显示时变为空.

I must be missing something basic, but I can't seem to keep my array loaded. It loads successfully, but it turns up empty when it appears in another function.

我的目标是从locations数组中随机选择一个城市.地图视图会加载我所有的注释,但是注释出现后,当我调用pickRandomNumber()时,我的locations数组就会清空.

My goal is to randomly pick a city from the locations array. The mapview loads with all my annotations, but then my locations array empties out when I call pickRandomNumber() after the annotation appears.

谢谢

Eli

这是我的代码:

import UIKit
import Mapbox
import GameplayKit

class ViewController: UIViewController, MGLMapViewDelegate {

var playerAnswer = ""
var number = Int()
var locations:[(number: Int, title: String, latitude:Double, Longitude:Double)] = []
var question = ""
var theCount = Int()
var randomNumber = Int()
var mapView = MGLMapView()


override func viewDidLoad() {
    super.viewDidLoad()

    mapView = MGLMapView(frame: view.bounds)

    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Set the map’s center coordinate and zoom level.
    mapView.setCenter(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 1, animated: true)
    view.addSubview(mapView)



    mapView.allowsZooming = true

    let center = CLLocationCoordinate2D(latitude: 38.894368, longitude: -77.036487)
    mapView.setCenter(center, zoomLevel: 7, animated: true)

    // Set the delegate property of our map view to `self` after instantiating it.
    mapView.delegate = self as! MGLMapViewDelegate

    loadArray()

}


func loadArray() {
     var locations = [
        [number:0, "title": "Washington DC, USA",    "latitude": 38.90, "longitude": -77.04],
        [number:1,"title": "Ottawa, Canada", "latitude": 45.41, "longitude": -75.70],
        [number:2,"title": "Mexico City, Mexico",     "latitude": 19.43, "longitude": -99.13],
        [number:3,"title": "Tegucigalpa, Honduras",     "latitude": 14.08, "longitude": -87.21],
        [number:4,"title": "San Salvador, El Salvador",     "latitude": 13.69, "longitude": -89.19],
        [number:5,"title": "Managua, Nicaragua",     "latitude": 12.13, "longitude": -86.25],
        [number:6,"title": "Belmopan, Belize",     "latitude": 14.64, "longitude": -90.51],
        [number:7,"title": "Panama City, Panama",     "latitude": 8.99, "longitude": -79.52],
        [number:8,"title": "Havana, Cuba",     "latitude": 23.13, "longitude": -82.38],
        [number:9,"title": "Caracas, Venezuela",     "latitude": 10.49, "longitude": -66.88],
        [number:10,"title": "Bogotá, Colombia",     "latitude": 4.61, "longitude": -74.08],
        [number:11,"title": "Lima, Peru",     "latitude": -12.04, "longitude": -77.03]

    ]

    for location in locations {
        let annotation = MGLPointAnnotation()
        annotation.title = location["title"] as? String
        annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
        mapView.addAnnotation(annotation)
    }


    print ("Current count for locations is \(locations.count)")  // count is 12

}

func pickRandomNumber() {

    print ("The array count is \(locations.count)")  // count is 0
    randomNumber = GKARC4RandomSource().nextInt(upperBound: locations.count)
    print (randomNumber) // randomNumber is always 0

}


// Use the default marker. See also: our view annotation or custom marker examples.
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {

    return nil
}

// Allow callout view to appear when an annotation is tapped.
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    playerAnswer = annotation.title as! String
    print ("The player pressed \(playerAnswer)")
    pickRandomNumber()
    return true
}

推荐答案

func loadArray() {
 var locations = [

您正在创建局部变量locations,而不是将数据设置为类变量.简而言之:删除var

You're creating a local variable locations, rather than setting the data to the class variable. In short: drop the var

这篇关于为什么我的数组在加载后变空了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆