Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么将对象追加到数组中时会出现nil错误?_Ios_Swift - Fatal编程技术网

Ios 为什么将对象追加到数组中时会出现nil错误?

Ios 为什么将对象追加到数组中时会出现nil错误?,ios,swift,Ios,Swift,我认为我的应用程序崩溃的原因是因为concert1没有被添加到数组中,而swift收到的结果为零。我该如何解决这个问题?当我设置断点时,数组有0个对象 var arrayOfConcerts: [ConcertsController] = [ConcertsController]() override func viewDidLoad() { super.viewDidLoad() self.setUpConcerts() self.table1.dataSource

我认为我的应用程序崩溃的原因是因为concert1没有被添加到数组中,而swift收到的结果为零。我该如何解决这个问题?当我设置断点时,数组有0个对象

var arrayOfConcerts: [ConcertsController] = [ConcertsController]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.setUpConcerts()
    self.table1.dataSource = self

    self.table1.registerNib(UINib(nibName: "LiveConcertsCell", bundle: nil), forCellReuseIdentifier: "cell")

}

 func setUpConcerts(){
    let concert1 = ConcertsController(imageName: "ACL.png")

    arrayOfConcerts.append(concert1)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfConcerts.count
}
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = table1.dequeueReusableCellWithIdentifier("cell") as? CustomCellConcerts
    let concert = arrayOfConcerts[indexPath.row]
    cell!.setCell(concert.imageName)
    return cell!
}
下面是ConcertsController的代码

import UIKit
import Foundation
class ConcertsController{

var imageName = "blank"

init(imageName: String){
    self.imageName = imageName
   }
}
下面是CustomCellConcerts的代码

import UIKit

class CustomCellConcerts: UITableViewCell {
@IBOutlet weak var concertimage: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func setCell(concertimage: String){
    self.concertimage.image = UIImage(named: concertimage)
}

}

如果我不得不猜测,那是因为在代码中的某个地方,您使用
init(命名为:String)
初始值设定项创建了一个
UIImage
,该初始值设定项是一个失败的初始化(返回一个
UIImage?
)。然后,该图像被显式地用
展开
当初始化器因为找不到图像而实际返回了
nil
时。只是猜测而已。环顾您使用该图像的位置,以及您是否明确地将其展开。

您是否调用setUpConcerts()?是的,在我的视图中,我调用了它。我会更新我的代码。你说“你认为你的应用程序崩溃的原因”——崩溃是什么,发生在哪一行。你这里的代码看起来不错,它说的是
在打开可选值时意外发现nil
,所以,显示该代码,但是你的单元格注册或单元格初始化有问题。正如Scott H在他的回答中所说,如果找不到图像,您就有一个潜在的问题,因为
UIImage(命名:)
将返回nil,但您已强制取消包装itI运行了一个断点,并发现应用程序在
let cell=table1之后崩溃。将ReusableCellWithIdentifier(“cell”)作为一个参数退出队列!CustomCellConcerts
Ok,因此在这种情况下,似乎还有两个选项需要考虑:要么
dequeueReusableCellWithIdentifier
返回
nil
(可能是因为它找不到重用id为“cell”的单元原型),要么标记为“cell”重用id的单元不是CustomCellConcerts类型。无论哪种情况,
as
是一个强制向下转换,如果它不工作,将使代码崩溃。我创建了一个自定义cell xib文件,并将匹配的代码放入标识符中。该单元格已连接到CustomCellConcerts类。我看不到图像的强制展开。所以可能是细胞。您或者需要创建原型单元并在创建UITableView的情节提要中声明其重用ID,或者如果要为单元使用单独的NIB,则必须调用
registerNib(\uUllReuseIdentifier:)
。你打了那个电话吗?我没有我该在哪里打那个电话?它看起来像什么?