0

In my code I'm calling the Apple geocoder:

private func callGoogleReverseGeocodingWebservice(location: CLLocationCoordinate2D) {

    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: location.latitude, longitude: location.longitude)
    self.defaults.synchronize()
    if (!self.alreadyShowedHashtagSuggestionPanel && defaults.object(forKey: "doNotShowHashtags") == nil) {
        self.alreadyShowedHashtagSuggestionPanel = true
    geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
        guard let addressDict = placemarks?[0].addressDictionary else {
            return
        }
        self.suggestedHashtags.removeAllObjects()

        if let street = addressDict["Thoroughfare"] as? String {
            self.suggestedHashtags.add(street)
        }
        if let city = addressDict["City"] as? String {
            self.suggestedHashtags.add(city)
        }
        if let country = addressDict["Country"] as? String {
            self.suggestedHashtags.add(country)
        }

        if(self.suggestedHashtags.count > 0) {
            print(self.suggestedHashtags.count)
            print("here")
            DispatchQueue.main.async {
                self.hashtagsCollectionView.reloadData()
            } 
        } else {
            print("there's nothing here")
        }

    })
    }
}

Every result from the method above is placed in an array, and that array is used in method:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    print("cellForRow")
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "hashtagCell", for: indexPath) as! HashtagCollectionViewCell

    let hashtagName:String =  self.suggestedHashtags[(indexPath as NSIndexPath).item] as! String
    cell.hashtagName.textColor = UIColor.lightGray
    cell.hashtagName.font = font
    cell.hashtagName.text = hashtagName
    print("this is \(hashtagName)")
    return cell 
}

Unfortunately, sometimes this line:

let hashtagName:String =  self.suggestedHashtags[(indexPath as NSIndexPath).item] as! String

throws error that says:

libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

I'm not sure what might be causing this issue, from what I've read it seems like the geocoder avoids allowing too much requests, so I think that after querying it too much - it returns empty data, therefore as! String throws error - but it's just my random thoughts, maybe there's something else that I might be missing?

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • Post the full exception message. If you have an exception breakpoint you may need to remove/step past it so that the full message gets logged in the console. – dan Jan 19 '17 at 18:19

1 Answers1

0

You shouldn't cast IndexPath to NSIndexPath, it may cause this kind of problem. So please try to change this line

let hashtagName:String = self.suggestedHashtags[(indexPath as NSIndexPath).item] as! String

To

let hashtagName = (self.suggestedHashtags[indexPath.item] as? String) ?? ""

Hope this will help to you

Nick Rybalko
  • 202
  • 2
  • 5