gpt4 book ai didi

ios - Swift 中的 SecAccessControlCreateWithFlags()

转载 作者:搜寻专家 更新时间:2023-10-30 23:00:04 26 4
gpt4 key购买 nike

我正在尝试从 swift 代码调用 Security.framework 中的函数。暂时忘记“错误输出”(最后一个)参数,如果我这样调用函数:

let accessControlRef = SecAccessControlCreateFlags(
kCFAllocatorDefault,
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
SecAccessControlCreateFlags.UserPresence,
nil
)

我收到以下错误:

Cannot find an initializer for type 'SecAccessControlCreateFlags' that accepts an argument list of type '(CFAllocator!, CFStringRef, SecAccessControlCreateFlags, nil)'

...但是,如果我将代码重新格式化为以下内容:

let allocator:CFAllocatorRef! = kCFAllocatorDefault

let protection:AnyObject! = kSecAttrAccessibleWhenUnlockedThisDeviceOnly

let flags:SecAccessControlCreateFlags = SecAccessControlCreateFlags.UserPresence


let accessControlRef = SecAccessControlCreateWithFlags(
allocator,
protection,
flags,
nil
)

(特定类型 - 例如,CFAllocatorRef - 取自 Xcode 自动完成的函数原型(prototype))...它编译没有问题。 这是怎么回事?

接下来,错误参数。我应该通过什么?迁移我的 Objective-C 代码时,我很想传递以下变量(当然,前缀是 &):

var accessControlError:CFErrorRef! = nil

...这给出了错误:

Cannot invoke 'SecAccessControlCreateWithFlags' with an argument list of type '(CFAllocatorRef!, AnyObject!, SecAccessControlCreateFlags, inout CFErrorRef!)'

相反,如果我传递以下变量(同样,前面加上地址运算符):

var accessControlError:UnsafeMutablePointer<Unmanaged<CFError>?>

(与原型(prototype)自动完成建议的类型相同),我得到:

Cannot invoke 'SecAccessControlCreateWithFlags' with an argument list of type '(CFAllocatorRef!, AnyObject!, SecAccessControlCreateFlags, inout UnsafeMutablePointer?>)'

...那么,是什么给了?

编辑:忘记错误参数。我似乎两次使用地址(即指向指针的指针)。相反,我应该这样做:

var accessControlError:UnsafeMutablePointer<Unmanaged<CFError>?> = nil
// ^ Already a 'pointer'

let allocator:CFAllocatorRef! = kCFAllocatorDefault
let protection:AnyObject! = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
let flags:SecAccessControlCreateFlags = SecAccessControlCreateFlags.UserPresence

let accessControlRef = SecAccessControlCreateWithFlags(
allocator,
protection,
flags,
accessControlError // <- Notice the lack of '&'
)

来源:this answer 中的示例代码.

最佳答案

(好的,所以一段时间内没有人添加任何新见解,所以我将用我上次编辑的内容回答我自己的问题:)

回答:忘记错误参数。我似乎两次使用地址(即指向指针的指针)。相反,我应该这样做:

let accessControlError:UnsafeMutablePointer<Unmanaged<CFError>?> = nil
// ^ Already a 'pointer'

let allocator:CFAllocator! = kCFAllocatorDefault
let protection:AnyObject! = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
let flags:SecAccessControlCreateFlags = SecAccessControlCreateFlags.userPresence

let accessControlRef = SecAccessControlCreateWithFlags(
allocator,
protection,
flags,
accessControlError // <- Notice the lack of '&'
)

关于ios - Swift 中的 SecAccessControlCreateWithFlags(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531706/

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