在Swift中获取PHAsset的位置 [英] Getting the location of a PHAsset in Swift

查看:537
本文介绍了在Swift中获取PHAsset的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因, PHAsset 上的 location 属性仅在Objective-c中公开,而不在Swift中公开。

For some reason the location property on a PHAsset is only exposed in Objective-c and not in Swift.

文档:PHAsset.location

为了解决这个问题,我想我可以创建一个Objective-C类其唯一目的是提取位置并将其导入Swift。

To work around it, I figured I could create a Objective-C class whose sole purpose is to extract the location and import it into Swift.

LocationGetter.h

LocationGetter.h

@interface LocationGetter : NSObject
+ (CLLocation *)locationForAsset:(PHAsset *) asset;
@end

LocationGetter.m

LocationGetter.m

@implementation LocationGetter
+ (CLLocation *)locationForAsset:(PHAsset *) asset {
    return [asset location];
}
@end

到目前为止这么好,但是当我尝试在Swift中使用它:

So far so good, but when I try to use it in Swift:

LocationGetter.locationForAsset(ass)




'LocationGetter.Type'没有名为'locationForAsset'的成员

'LocationGetter.Type' does not have a member named 'locationForAsset'

奖金问题:为什么Apple没有在swift中公开位置

推荐答案

事实证明答案非常简单。问题是Swift文件不知道 CLLocation 是什么,因此拒绝导入该函数。导入 CoreLocation 解决了这个问题。

It turns out that the answer is really simple. The problem is that the Swift file don't know what a CLLocation is, and thus refuses to import that function. Importing CoreLocation solves the issue.

import CoreLocation

LocationGetter.locationForAsset(ass)

编辑: Apple已经从那时起包含 .location 作为 PHAsset 的getter。获取位置现在就像 asset.location 一样简单。

Apple has since included .location as a getter on the PHAsset. Getting the location is now as easy as asset.location.

这篇关于在Swift中获取PHAsset的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆