Search code examples
swift3icarousel

iCarousel - Thread1 signal SIGABRT


So I created an iCarousel and was able to successfully run it. I did however need to load more than one carousel on the viewcontroller. I found the multiple iCarousels code and tried to adapt it to Swift. Now unfortunately I get a Thread1 signal SIGABRT that shows on the line "class AppDelegate". Really have no idea why it's doing this.

Please help me out! Here's the code I have for the ViewController

class ChangeRoomViewController: UIViewController, iCarouselDataSource, 
iCarouselDelegate {

    var hatsArray : NSMutableArray = NSMutableArray()
    var shirtsArray : NSMutableArray = NSMutableArray()

@IBOutlet weak var carousel1: iCarousel!
@IBOutlet weak var carousel2: iCarousel!

override func viewDidLoad() {

    super.viewDidLoad()

    hatsArray = ["TopHat.jpeg", "WhiteBrimHat.jpeg", "StoutHat.jpg", "BaseballCapBlackw:Red.jpg", "WhiteBaseballCap.jpg"]
    carousel1.type = iCarouselType.cylinder
    carousel1.reloadData()

    shirtsArray = ["StarWarsBlackFittedT.jpg", "CollaredFittedWhiteWithBeigeSplash.jpg", "CollaredWhiteWithZigzagLightBlueStripes.jpg", "ClooaredBlackWithWhiteStripes.jpg", "CollaredFittedNavyBlue.jpg"]
    carousel2.type = iCarouselType.cylinder
    carousel2.reloadData()
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {

    var hatsView : UIImageView!
    var shirtsView : UIImageView!

    if view == nil {
        hatsView = UIImageView(frame: CGRect(x: 16, y: 65, width: 90, height: 60))
        hatsView.contentMode = .scaleAspectFit
        shirtsView = UIImageView(frame: CGRect(x: 16, y: 133, width: 90, height: 60))
        shirtsView.contentMode = .scaleAspectFit
    } else {
        hatsView = view as! UIImageView
        shirtsView = view as! UIImageView
    }

    hatsView.image = UIImage(named: "\(hatsArray.object(at: index))")
    shirtsView.image = UIImage(named: "\(shirtsArray.object(at: index))")

    if (carousel == carousel1) {
        return hatsView
    }else {
        return shirtsView
    }

}

func numberOfItems(in carousel: iCarousel) -> Int {
    if (carousel == carousel1) {
        return hatsArray.count
    }else {
        return shirtsArray.count
    }
}

}


Solution

  • I can recreate your error and it is caused by shirtsArray being empty and so the line

    shirtsView.image = UIImage(named: "\(shirtsArray.object(at: index))")
    

    causes the exception you encountered.

    I am not sure why, but if you move the line

        shirtsArray = ["StarWarsBlackFittedT.jpg", "CollaredFittedWhiteWithBeigeSplash.jpg", "CollaredWhiteWithZigzagLightBlueStripes.jpg", "ClooaredBlackWithWhiteStripes.jpg", "CollaredFittedNavyBlue.jpg"]
    

    to be just under the assignment to hatsArray, then it works.

    Your viewForItemAt function is a bit messy. Why are you creating images/views for both carousels before deciding which one is required?