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

时间:2021-04-10 21:15:33

I think the reason why my app is crashing is because concert1 is not being added to the array, and swift receives it as nil. How would I fix this? When I breakpoint, the array has 0 objects.

我认为我的应用程序崩溃的原因是因为没有将concert1添加到数组中,并且swift将其作为nil接收。我该如何解决这个问题?当我断点时,数组有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!
}

Below is the code for the ConcertsController

下面是ConcertsController的代码

import UIKit
import Foundation
class ConcertsController{

var imageName = "blank"

init(imageName: String){
    self.imageName = imageName
   }
}

Below is the code for CustomCellConcerts

下面是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)
}

}

}

1 个解决方案

#1


0  

If I had to guess, it's because somewhere in the code you create a UIImage using the init(named: String) initializer which is a failable initialize (returns a UIImage?). That image was then explicitly unwrapped with a ! when the initializer actually returned a nil because it couldn't find the image. Just a guess. Look around where you are using that image and whether or not you explicitly unwrap it.

如果我不得不猜测,那是因为代码中的某个地方使用init(named:String)初始化器创建了一个UIImage,它是一个可用的初始化器(返回一个UIImage?)。那个图像然后用一个明确的解开!当初始化程序实际返回nil时,因为它无法找到图像。只是一个猜测。查看您使用该图像的位置以及是否明确打开它。

#1


0  

If I had to guess, it's because somewhere in the code you create a UIImage using the init(named: String) initializer which is a failable initialize (returns a UIImage?). That image was then explicitly unwrapped with a ! when the initializer actually returned a nil because it couldn't find the image. Just a guess. Look around where you are using that image and whether or not you explicitly unwrap it.

如果我不得不猜测,那是因为代码中的某个地方使用init(named:String)初始化器创建了一个UIImage,它是一个可用的初始化器(返回一个UIImage?)。那个图像然后用一个明确的解开!当初始化程序实际返回nil时,因为它无法找到图像。只是一个猜测。查看您使用该图像的位置以及是否明确打开它。