0

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.

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

import UIKit
import Foundation
class ConcertsController{

var imageName = "blank"

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

Below is the code for 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)
}

}

blee
  • 297
  • 4
  • 13
  • do you call setUpConcerts() ? – nRewik Oct 06 '15 at 01:22
  • yes in my viewDidLoad I called it. I will update my code. – blee Oct 06 '15 at 01:23
  • You say "you think the reason why your app is crashing" - What is the crash and which line does it occur on. The code you have here looks OK – Paulw11 Oct 06 '15 at 01:43
  • All it says is `unexpectedly found nil while unwrapping an Optional values` https://www.dropbox.com/s/0oc5e1o8prc24gg/Screenshot%202015-10-05%2020.46.29.png?dl=0 – blee Oct 06 '15 at 01:44
  • Look at the link in the previous comment. – blee Oct 06 '15 at 01:47
  • What line in your code does the error occur? – zaph Oct 06 '15 at 01:48
  • You need to look back up the stack to find where in your code it crashed, but it won't be in the code shown since you aren't dealing with any optionals – Paulw11 Oct 06 '15 at 01:49
  • I do have a login page, could the problem possibly be there? – blee Oct 06 '15 at 01:51
  • @Paulw I ran a breakpoint and figured out that the app crashes after `let cell = table1.dequeueReusableCellWithIdentifier("cell") as! CustomCellConcerts` – blee Oct 06 '15 at 02:27
  • 1
    So, show that code, but something is wrong with your cell registration or cell initialisation. As Scott H says in his answer, you have a potential issue if the image can't be found, as `UIImage(named:)` will return nil but you have force unwrapped it – Paulw11 Oct 06 '15 at 02:31
  • Have you associated the reuse identifier `cell` with your cell class? It looks likely that `dequeueReusableCellWithIdentifier` is returning nil – Paulw11 Oct 06 '15 at 02:46
  • Yes I did associate the reuse identifier cell with my cell class – blee Oct 06 '15 at 02:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91437/discussion-between-paulw11-and-blee). – Paulw11 Oct 06 '15 at 02:54
  • possible duplicate of http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu – jtbandes Oct 09 '15 at 03:02

1 Answers1

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.

Scott H
  • 1,509
  • 10
  • 14
  • I ran a breakpoint and figured out that the app crashes after `let cell = table1.dequeueReusableCellWithIdentifier("cell") as! CustomCellConcerts` – blee Oct 06 '15 at 02:27
  • Ok, so in that case it seems like there are two more options to consider: Either `dequeueReusableCellWithIdentifier` is returning `nil` (probably because it can't find a cell prototype with reuse id "cell"), or the cell that is marked with the "cell" reuse id is not of a the type CustomCellConcerts. In either case the `as!` is a forced downcast and will crash your code if it doesn't work. – Scott H Oct 06 '15 at 02:32
  • I created a custom cell xib file and put the matched the identifier. The cell is connected to the CustomCellConcerts class. – blee Oct 06 '15 at 02:34
  • I don't see a forced unwrap of the image. So it's probably the cell. You either need to create the prototype cell and declare it's reuse ID in the storyboard where you created the UITableView or if you are using a separate NIB for the cell then you have to make a call to `registerNib(_:forCellReuseIdentifier:)`. Did you make that call? – Scott H Oct 06 '15 at 02:40
  • I did not where would I make that call? What would it look like? – blee Oct 06 '15 at 02:40
  • You'll want to put it in the same view controller that is controlling the UITableView in which you want to use that cell. `viewDidLoad` is probably not a bad place to put it, but there are probably better options. It's a method you need to call on the UITableView. Beyond that, I'm not going to be able to help you without coding it for you. Have a look at the documentation. Here is a good place to start: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/doc/uid/TP40006943-CH3-SW64 – Scott H Oct 06 '15 at 02:44
  • Ok I added it and I'm getting a new error. https://www.dropbox.com/s/89p9wlu9mxewf5r/Screenshot%202015-10-05%2021.49.03.png?dl=0 – blee Oct 06 '15 at 02:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91436/discussion-between-scott-h-and-blee). – Scott H Oct 06 '15 at 02:50