swift 如何将Unmanaged!转换< AnyObject>为字符串

kcugc4gi  于 2023-06-28  发布在  Swift
关注(0)|答案(4)|浏览(130)

我在我的新应用程序中使用Swift。我正在尝试从AddressBook访问email-id。一切看起来都很好,但最终我变得无法管理!从ABMultiValueCopyValueAtIndex(multi,0)。
那么我如何转换或提取字符串呢?
提前感谢!

ny6fqffe

ny6fqffe1#

试试这个:

let str = yourVar.takeRetainedValue() as String
u7up0aaq

u7up0aaq2#

let value : String? = ABMultiValueCopyValueAtIndex(multivalue, 0).takeRetainedValue() as? String
pengsaosao

pengsaosao3#

尝试使用此函数将Unmanaged转换为Stirng

func convertCfTypeToString(cfValue: Unmanaged<AnyObject>!) -> String?{

    let value = Unmanaged.fromOpaque(
        cfValue.toOpaque()).takeUnretainedValue() as CFString
    if CFGetTypeID(value) == CFStringGetTypeID(){
        return value as String
    } else {
        return nil
    }
}

用途:

let value = convertCfTypeToString(cfValue: ABMultiValueCopyValueAtIndex(multivalue, 0))
vh0rcniy

vh0rcniy4#

布尔

let obj = classType.perform(selector)
    if let obj = obj as? AnyObject {
        let pointer: UnsafeMutableRawPointer = Unmanaged<AnyObject>.passUnretained(obj).toOpaque()
        let valueBool: Bool = pointer.load(as: Bool.self)
        return valueBool
    }
    return false

相关问题