-1

In swift project, I am trying to segue from a one table view controller to another, but after the viewDidLoad, I get a SIGBRT error. The table view controller that is used to populate the view being segued to inherits many of its properties from another class. Below is the child class.

public class PlacePhotoTVC : PhotoFetcherTVC {
    var placeID : String? = nil
    public override func viewDidLoad() {
        super.viewDidLoad()
        if placeID != nil {
            self.photos = FlickrFetcher.PhotosInPlace(flickrPlaceId: placeID!, maxResults: 50)
            self.places = FlickrPlaces()
            self.places.add(place: FlickrFetcher.GetPlace(placeID!)!)
        }
    }
}

This is the parent class.

public class PhotoFetcherTVC : UITableViewController {
    // public API
    public var photos : [FlickrPhoto]! = nil {
        didSet {
            tableView.reloadData()

        }
    }
    public var recentlyViewedPhotos = [FlickrPhoto]()
    public var places : FlickrPlaces! = nil
    public override func viewDidLoad() {
        super.viewDidLoad()

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

        return (places.placesSorted(in: (places.countriesSorted()?[section])!)?.count)!

    }
    public override func numberOfSections(in tableView: UITableView) -> Int {
        var numberOfSections = 0
        if places != nil {
            numberOfSections = (places.countriesSorted()?.count)!
        }

        return numberOfSections
    }

    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Photo Cell", for: indexPath)
        var title = photos[indexPath.row].title
        var description = photos[indexPath.row].description
        if title == nil || title!.isEmpty {
            title = "Unknown"
        }

        if description == nil || description!.isEmpty {
            description = "Unknown"
        }

        if title == "Unkown" {

        }
        cell.textLabel?.text = title
        if let locationID = photos[indexPath.row].photoInformation["place_id"] as? String {
            let subtitle = FlickrFetcher.GetPlace(locationID)?.region
            cell.detailTextLabel?.text = subtitle
        }
        //tableView.tableHeaderView.

        return cell
    }
    public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        var sectionHeader : String
        if places.countriesSorted()?.count == 0 {
            sectionHeader = ""
        } else {
            sectionHeader = (places.countriesSorted()?[section])!
        }
        return sectionHeader
    }
    public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ViewFlickrPhoto" {
            if let cell = sender as? UITableViewCell {
                let path = tableView.indexPath(for: cell)
                if let destVC = segue.destination as? ImageViewController {
                    destVC.imageUrl = photos[path!.row].URLforPhoto(format: .flickrPhotoFormatOriginal)

                    // add the photo the recently viewed photos
                    var index = 0
                    for photo in recentlyViewedPhotos {
                        if photo.id == photos[path!.row].id {
                            recentlyViewedPhotos.remove(at: index)
                            break
                        }
                        index += 1
                    }
                    recentlyViewedPhotos.insert(photos[path!.row], at: 0)


                }
            }
        } //else if segue.i

    }
}

I used break points to look at the code and see where the error would throw, and it would throw after going through the viewDidLoad function from PlacePhotoTVC and reaching the last bracket of PlacePhototTVC.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • Your description in the last paragraph doesn't really help to pinpoint the error. Please be more specific and tell us the exact line that is causing the error. That said, I'm willing to bet its a forced unwrapped optional that is giving you issues. – Andy Ibanez Dec 07 '16 at 15:14

1 Answers1

0

Keep in mind, please, that ! means "crash me". You can hardly be surprised when Swift does exactly what you asked it to do.

In this case, you are saying:

self.places.add(place: FlickrFetcher.GetPlace(placeID!)!)

But FlickrFetcher.GetPlace(placeID!) is nil. Therefore, saying ! after it causes a crash.

To do this without crashing, simply look to see whether FlickrFetcher.GetPlace(placeID!) is nil and do nothing if it is. The "neat" Swift way to do this is with if let:

if let place = FlickrFetcher.GetPlace(placeID!) {
    self.places.add(place:place)
}

While we are cleaning up your code, let's get rid of all the !, in the same way:

if let id = self.placeID {
    self.photos = FlickrFetcher.PhotosInPlace(flickrPlaceId: id, maxResults: 50)
    self.places = FlickrPlaces()
    if let place = FlickrFetcher.GetPlace(id) {
        self.places.add(place:place)
    }
}

Now you cannot crash here.

Having said all that, your code isn't going to work even after it stops crashing, because what you are doing in this code is totally wrong. In essence, you are networking on the main thread. You must not do that. You need to rethink your entire approach.

matt
  • 515,959
  • 87
  • 875
  • 1,141