gpt4 book ai didi

swift - Swift 中的 UTTypeCreatePreferredIdentifierForTag 和 CFStringRef

转载 作者:IT王子 更新时间:2023-10-29 05:46:48 26 4
gpt4 key购买 nike

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")
}
}

我收到这个错误

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

在线

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

有什么想法吗??谢谢

最佳答案

UTTypeCreatePreferredIdentifierForTag传回一个 Unmanaged<CFStringRef> ,因此您需要从 Unmanaged 中获取值对象之前你可以使用它:

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

请注意,我正在调用 takeRetainedValue()UTTypeCreatePreferredIdentifierForTag正在返回一个我们负责释放的对象。关于 takeRetainedValue() 的评论说:

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()相反。

关于swift - Swift 中的 UTTypeCreatePreferredIdentifierForTag 和 CFStringRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26635643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com