简体   繁体   中英

Convert UnsafeMutablePointer<CLLocationCoordinate2D> to [CLLocationCoordinate2D] in Swift

I write an app in Swift and is bridging some Objective-C code. One of these classes has a method that looks like this: + (CLLocationCoordinate2D *)polylineWithEncodedString:(NSString *)encodedString; .

In Swift, it said this method returns a UnsafeMutablePointer<CLLocationCoordinate2D> . What I want is a Swift array of CLLocationCoordinate2D .

What obviously doesn't work, but I tried, is this:

let coordinates: [CLLocationCoordinate2D] = TheClass.polylineWithEncodedString(encodedString)

which will give me the following error:

'UnsafeMutablePointer<CLLocationCoordinate2D>' is not convertible to '[CLLocationCoordinate2D]'

Is it somehow possible to convert this UnsafeMutablePointer<CLLocationCoordinate2D> to a [CLLocationCoordinate2D] ? Or should I take a different approach?

You can just use the memory property of the UnsafeMutablePointer, which is the data behind the pointer. But keep in mind that UnsafeMutablePointer < CLLocationCoordinate2D > will return one CLLocationCoordinate2D, not an array, just as declared in the obj c function.

var coordinate: CLLocationCoordinate2D = TheClass.polylineWithEncodedString(encodedString).memory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM