Swift 2 / Xcode 7.2 和 Swift 1.2 / Xcode 6.3 中 MapKit 之间的冲突
Conflict between MapKit in Swift 2 / Xcode 7.2 and Swift 1.2 / Xcode 6.3
我正在使用 MapKit 教程 http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial
使用Swift 1.2 和Xcode 6.3 进行教学。然而,在我的项目中,我遇到了一些错误。下面的代码直接来自
的教程
我已经分解了下面标记为 (A) 和 (B) 的代码中的相关行。各自报错如下:
(A) `调用中缺少参数标签 'rawValue.' - 添加 rawValue 参数标签没有帮助
(B) 调用中的额外参数 'error' -
删除 'error' 参数没有帮助
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
var artworks = [Artwork]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
centerLocationOnMap(initialLocation)
mapView.delegate = self
// show artwork on map
let artwork = Artwork(title: "King David Kalakaua",
locationName: "Waikiki Gateway Park",
discipline: "Sculpture",
coordinate: CLLocationCoordinate2D(latitude: 21.283921, longitude: -157.831661)
)
mapView.addAnnotation(artwork)
}
let regionRadius: CLLocationDistance = 1000;
func centerLocationOnMap(location: CLLocation) {
let regionCoordinates = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(regionCoordinates, animated: true)
}
func loadInitialData() {
let fileName = NSBundle.mainBundle().pathForResource("PublicArt", ofType: "json");
var readError : NSError?
(A)
var data: NSData = NSData(contentsOfFile: fileName!, options: NSDataReadingOptions(0), error: &readError)!
var error: NSError?
(B)
let jsonObject: AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions(0), error: &error)
.
if let jsonObject = jsonObject as? [String: AnyObject] where error == nil,
let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
for artworkJSON in jsonData {
if let artworkJSON = artworkJSON.array,
// 5
artwork = Artwork.fromJSON(artworkJSON) {
artworks.append(artwork)
}
}
}
}
swift2 中的方法签名更改
你可以看看 NSData 和 NSJSONSerialization 苹果文档
您必须在调用函数之前删除错误参数并使用 try
对于选项,您可以输入 [] -> 无选项
try NSData(contentsOfFile: fileName!, options: [])
try NSJSONSerialization.
见
并且您必须使用 do { code } catch { } 捕获错误,或者仅出于测试目的使用 try!
我正在使用 MapKit 教程 http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial
使用Swift 1.2 和Xcode 6.3 进行教学。然而,在我的项目中,我遇到了一些错误。下面的代码直接来自
的教程我已经分解了下面标记为 (A) 和 (B) 的代码中的相关行。各自报错如下:
(A) `调用中缺少参数标签 'rawValue.' - 添加 rawValue 参数标签没有帮助
(B) 调用中的额外参数 'error' - 删除 'error' 参数没有帮助
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
var artworks = [Artwork]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
centerLocationOnMap(initialLocation)
mapView.delegate = self
// show artwork on map
let artwork = Artwork(title: "King David Kalakaua",
locationName: "Waikiki Gateway Park",
discipline: "Sculpture",
coordinate: CLLocationCoordinate2D(latitude: 21.283921, longitude: -157.831661)
)
mapView.addAnnotation(artwork)
}
let regionRadius: CLLocationDistance = 1000;
func centerLocationOnMap(location: CLLocation) {
let regionCoordinates = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(regionCoordinates, animated: true)
}
func loadInitialData() {
let fileName = NSBundle.mainBundle().pathForResource("PublicArt", ofType: "json");
var readError : NSError?
(A)
var data: NSData = NSData(contentsOfFile: fileName!, options: NSDataReadingOptions(0), error: &readError)!
var error: NSError?
(B)
let jsonObject: AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions(0), error: &error)
.
if let jsonObject = jsonObject as? [String: AnyObject] where error == nil,
let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
for artworkJSON in jsonData {
if let artworkJSON = artworkJSON.array,
// 5
artwork = Artwork.fromJSON(artworkJSON) {
artworks.append(artwork)
}
}
}
}
swift2 中的方法签名更改 你可以看看 NSData 和 NSJSONSerialization 苹果文档
您必须在调用函数之前删除错误参数并使用 try
对于选项,您可以输入 [] -> 无选项
try NSData(contentsOfFile: fileName!, options: [])
try NSJSONSerialization.
见
并且您必须使用 do { code } catch { } 捕获错误,或者仅出于测试目的使用 try!