gpt4 book ai didi

ios - "Cannot find an initializer for type ' MKPlaceMark' 接受类型的参数列表

转载 作者:搜寻专家 更新时间:2023-11-01 07:33:40 24 4
gpt4 key购买 nike

这段代码有两个错误:

"Cannot find an initializer for type 'MKPlacemark' that accepts an argument list of type '(coordinate: CLLocationCoordinate2D, addressDictionary: [NSString : String])'

"Cannot invoke 'geocodeAddressstring' with an argument list of type '(String, completionHandler: ([AnyObject?]!, NSError?) -> _)'

我曾尝试在这里和那里放置可选值,因为读到它可能会解决问题,但没有奏效。

我应该如何更改我的代码以修复这些错误并使我的 ViewController 正常工作?

import UIKit
import CoreLocation
import AddressBook
import Contacts
import MapKit

class ViewController: UIViewController {

@IBOutlet weak var address: UITextField!
@IBOutlet weak var city: UITextField!
@IBOutlet weak var province: UITextField!
@IBOutlet weak var zip: UITextField!

var coords: CLLocationCoordinate2D?

@IBAction func getDirections(sender: UIButton) {

let geoCoder = CLGeocoder()

let addressString = "\(address.text) \(city.text) \(province.text) \(zip.text)"

geoCoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [AnyObject?]!, error: NSError?) in

if error != nil {

print("Geocode failed with error: \(error?.localizedDescription)")

} else if placemarks?.count > 0 {

let placemark = placemarks[0] as! CLPlacemark
let location = placemark.location
self.coords = location.coordinate

self.showMap()
}
})
}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func showMap() {
let addressDict =[kABPersonAddressStreetKey as NSString: address.text!,
kABPersonAddressCityKey: city.text!,
kABPersonAddressStateKey: province.text!,
kABPersonAddressZIPKey: zip.text! ]

let place = MKPlacemark(coordinate: coords!, addressDictionary: addressDict)
let mapItem = MKMapItem(placemark: place)
let options = [MKLaunchOptionsDirectionsModeDriving]

mapItem.openInMapsWithLaunchOptions(options)
}
}

最佳答案

你升级到swift 2.0了吗?

Swift 1.2 及之前

geoCoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [AnyObject?]!, error: NSError?) in

swift 2.0

geocoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

还有变化

let placemark = placemarks[0] as! CLPlacemark

let placemark = placemarks[0]

关于ios - "Cannot find an initializer for type ' MKPlaceMark' 接受类型的参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31167386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com