xcode中viewDidLoad之后的Sigbrt错误

时间:2016-12-07 15:11:56

标签: ios swift xcode sigabrt viewdidload

在swift项目中,我试图从一个表视图控制器转到另一个,但在viewDidLoad之后,我收到一个SIGBRT错误。表视图控制器,用于填充被视图的视图以从其他类继承其许多属性。以下是儿童班。

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!)!)
        }
    }
}

这是父类。

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

    }
}

我使用断点查看代码并查看错误将抛出的位置,并且在经过viewDidLoad的{​​{1}}函数并到达{{1}的最后一个括号后抛出}。

1 个答案:

答案 0 :(得分:0)

请注意,! 表示“让我崩溃”。当Swift完全按照你的要求行事时,你几乎不会感到惊讶。

在这种情况下,你说:

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

FlickrFetcher.GetPlace(placeID!)nil。因此,在!之后导致崩溃。

要在不崩溃的情况下执行此操作,只需查看FlickrFetcher.GetPlace(placeID!)是否为nil,如果是if let则不执行任何操作。 “整洁”的Swift方法是使用if let place = FlickrFetcher.GetPlace(placeID!) { self.places.add(place:place) }

!

在我们清理您的代码时,让我们以同样的方式摆脱所有 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) } }

{{1}}

现在你不能在这里崩溃。

说了这么多,即使它停止崩溃,你的代码也不会工作,因为你在这段代码中所做的是完全错误。实质上,您在主线程上建立联网。你不能这样做。您需要重新考虑整个方法。