简体   繁体   English

Swift-数组未填充值

[英]Swift - Array not being populated with values

I am trying to build a list within a table view controller and I have the proper setup, but for some reason my simulator crashes at the line where I set my array. 我正在尝试在表视图控制器中构建列表,并且已经进行了正确的设置,但是由于某种原因,我的模拟器在我设置数组的那一行崩溃了。 It is not an error, but a Thread issue. 这不是错误,而是Thread问题。 I'm still learning the XCode warning system so I'm not sure what that means, but I noticed that in the Thread notifications that cityArray = ([String]) 0 values . 我仍在学习XCode警告系统,所以我不确定这是什么意思,但是我注意到在Thread通知中, cityArray = ([String]) 0 values Can anyone help? 有人可以帮忙吗?

import UIKit

class ListTableViewController: UITableViewController {

    var cityArray: [String] = ["Portland","San Francisco","Cupertino"]


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    // MARK: - Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return cityArray.count

    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = self.cityArray[indexPath.row]

        return cell
    }


}

UPDATE: 更新:

Images of the Thread message: 主题消息的图像:

发行线

侧边栏

安慰

尝试这个

let cityArray: [NSArray] = ["Portland","San Francisco","Cupertino"] as NSArray

Can you also provide the error you're receiving? 您还能提供您收到的错误吗? Is it an issue with the array being mutated while being enumerated or something? 枚举时数组是否发生突变或其他问题?

Also, 也,

var cityArray: [String] = ["Portland","San Francisco","Cupertino"]

can change to 可以更改为

var cityArray = ["Portland", "San Francisco", "Cupertino"]

Since all your objects are of the same type, the Swift's type inference takes care of this for you. 由于所有对象都是同一类型,因此Swift的类型推断将为您解决这一问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM