检查获取的对象是否是Swift中的图像或视频

maf*_*oso 2 ios swift phasset

我想检查一下latestObject的类型.这是一些代码:

allMedia = PHAsset.fetchAssetsWithOptions(fetchOptions)
let allPhotos = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
let allVideo = PHAsset.fetchAssetsWithMediaType(.Video, options: fetchOptions)
print("Found \(allMedia.count) media")
print("Found \(allPhotos.count) images")
print("Found \(allVideo.count) videos")

let latestObject: AnyObject! = allMedia.lastObject

// How to check what type latestObject is?
// I think something with mediaType but how is it exactly going?
Run Code Online (Sandbox Code Playgroud)

OOP*_*Per 6

你尝试过这样的事情:

if let asset = allMedia.lastObject as? PHAsset {
    switch asset.mediaType {
    case .Image:
        print("Image")
    case .Video:
        print("Video")
    case .Audio:
        print("Audio")
    default:
        print("Unknown")
    }
}
Run Code Online (Sandbox Code Playgroud)

包含在其中的每个元素PHFetchResult是a PHAsset(在这种情况下).因此,通过转换PHAsset,您可以访问该属性mediaType.