ios - Cannot subscript a value of type [CLPlacemark] with an index type int -
i retrieve current location. work on swift xcode 7. looked plusieur tutorials, every time use same method. here code , error: !
error : cannot subscript value of type [clplacemark] index type int
import uikit import corelocation class viewcontroller: uiviewcontroller, cllocationmanagerdelegate { let locationmanager = cllocationmanager() override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. self.locationmanager.delegate = self self.locationmanager.desiredaccuracy = kcllocationaccuracybest self.locationmanager.requestwheninuseauthorization() self.locationmanager.startupdatinglocation() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func locationmanager(manager: cllocationmanager, didupdatelocations locations: [anyobject]) { clgeocoder().reversegeocodelocation(manager.location!, completionhandler: { (placemarks, error) -> void in if (error != nil) { print("error") return } if placemarks!.count > 0 { let pm = placemarks[0] clplacemark self.displaylocationinfo(pm) } else { print("errordata") } }) } func displaylocationinfo(placemark: clplacemark){ self.locationmanager.stopupdatinglocation() print(placemark.locality) print(placemark.postalcode) print(placemark.administrativearea) print(placemark.country) } func locationmanager(manager: cllocationmanager, didfailwitherror error: nserror) { print("error:" + error.localizeddescription) } }
no, in xcode 7 error is:
error: cannot subscript value of type '[clplacemark]?' index of type 'int'
note of ?
. have unwrap optional. can replace:
if placemarks!.count > 0 { let placemark = placemarks[0] clplacemark self.displaylocationinfo(placemark) }
with:
if let placemark = placemarks?.first { self.displaylocationinfo(placemark) }
Comments
Post a Comment