简体   繁体   English

转换UnsafeMutablePointer <CLLocationCoordinate2D> 到Swift中的[CLLocationCoordinate2D]

[英]Convert UnsafeMutablePointer<CLLocationCoordinate2D> to [CLLocationCoordinate2D] in Swift

I write an app in Swift and is bridging some Objective-C code. 我在Swift中编写了一个应用程序,并桥接了一些Objective-C代码。 One of these classes has a method that looks like this: + (CLLocationCoordinate2D *)polylineWithEncodedString:(NSString *)encodedString; 这些类之一具有如下所示的方法: + (CLLocationCoordinate2D *)polylineWithEncodedString:(NSString *)encodedString; .

In Swift, it said this method returns a UnsafeMutablePointer<CLLocationCoordinate2D> . 在Swift中,它表示此方法返回UnsafeMutablePointer<CLLocationCoordinate2D> What I want is a Swift array of CLLocationCoordinate2D . 我想要的是一个CLLocationCoordinate2D的Swift数组。

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] ? 是否可以将UnsafeMutablePointer<CLLocationCoordinate2D>转换为[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. 您可以只使用UnsafeMutablePointer的memory属性,该属性是指针后面的数据。 But keep in mind that UnsafeMutablePointer < CLLocationCoordinate2D > will return one CLLocationCoordinate2D, not an array, just as declared in the obj c function. 但是请记住,就像obj c函数中声明的那样, UnsafeMutablePointer <CLLocationCoordinate2D>将返回一个CLLocationCoordinate2D,而不是数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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