Assigned Initializers
, , , . Swift , Swift .
, . . , init , , , .
, MKPolyline init, . , .
:
Swift , , , .
, , , MKPolyline , . , Swift , init init . - init . , Swift .
inits - , , inits . , , . init, init, init, .
, MKPolyline, , . , , , .
, init, Location, init MKPolyline .
- , , . , locations , getCoordinates.
:
extension MKPolyline {
var locations: [Location] {
guard pointCount > 0 else { return [] }
let defaultCoordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
var coordinates = [CLLocationCoordinate2D](repeating: defaultCoordinate, count: pointCount)
getCoordinates(&coordinates, range: NSRange(location: 0, length: pointCount))
return coordinates.map({ Location(coordinate: $0) })
}
convenience init(locations: [Location]) {
let coordinates = locations.map({ $0.coordinate })
self.init(coordinates: coordinates, count: coordinates.count)
}
}
. init, , , , init self, . map Location.
, locations, getCoordinates . , , , , getCoordinates Objective-C UnsafeMutablePointer Swift. CLLocationCoordinate2D , getCoordinates, , range. & coordinates Swift, inout- .
locations , Location, , , , , .
Wrapper
"Swifty" , , , . , , MKPolyline:
class MyPolyline {
let underlyingPolyline: MKPolyline
let locations: [Location]
init(locations: [Location]) {
self.locations = locations
let coordinates = locations.map { $0.coordinate }
self.underlyingPolyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
}
}
, , MyPolyline MKPolyline, myPolyline.underlyingPolyline . , , - , , MKPolyline, , , , Apple.