Swift中的UTTypeCreatePreferredIdentifierForTag和CFStringRef [英] UTTypeCreatePreferredIdentifierForTag and CFStringRef in Swift

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

问题描述

import Foundation
import MobileCoreServices

func checkFileExtension(fileName: NSString){
    println(fileName)

    var fileExtension:CFStringRef = fileName.pathExtension

    println(fileExtension)

    var fileUTI:CFStringRef = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, nil)

    println(fileUTI)

    let testBool = UTTypeConformsTo(fileUTI, kUTTypeImage) != 0

    if  testBool{
        println("image")
    }
}

我收到此错误

错误:非托管"无法转换为"CFStringRef"

error : 'Unmanaged' is not convertible to 'CFStringRef'

在线

var fileUTI:CFStringRef = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,fileExtension,nil)

var fileUTI:CFStringRef = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, nil)

有什么想法吗?谢谢

推荐答案

UTTypeCreatePreferredIdentifierForTag传回 Unmanaged<CFStringRef> ,因此您需要先从Unmanaged对象中获取值,然后才能使用它:

UTTypeCreatePreferredIdentifierForTag passes back an Unmanaged<CFStringRef>, so you need to get the value out of the Unmanaged object before you can use it:

var unmanagedFileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, nil)
var fileUTI = unmanagedFileUTI.takeRetainedValue()

请注意,由于UTTypeCreatePreferredIdentifierForTag返回的是我们负责释放的对象,因此我正在调用takeRetainedValue(). takeRetainedValue()上的评论说:

Note that I'm calling takeRetainedValue() since UTTypeCreatePreferredIdentifierForTag is returning an object that we are responsible for releasing. The comments on takeRetainedValue() say:

获取此非托管引用的值作为托管引用,然后 消耗了不平衡的保留.

Get the value of this unmanaged reference as a managed reference and consume an unbalanced retain of it.

当函数返回非托管引用并且您 知道您有责任发布结果.

This is useful when a function returns an unmanaged reference and you know that you're responsible for releasing the result.

如果从确定您没有负责释放该对象的函数中获取了Unmanaged对象,请调用takeUnretainedValue().

If you get an Unmanaged object back from a function where you are sure you aren't responsible for releasing that object, call takeUnretainedValue() instead.

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

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