SecAccessControlCreateWithFlags() in Swift



我试图在安全框架中调用函数,从swift代码。暂时忘记"error out"(最后一个)参数,如果我像这样调用函数:

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

显示如下错误:

找不到类型'SecAccessControlCreateFlags'的初始化项接受类型为'(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的自动完成功能原型)…它的编译没有问题。发生什么事了?

下一步,错误参数。我应该通过什么考试?迁移我的Objective-C代码,我想传递以下变量(当然,前面加上&):

var accessControlError:CFErrorRef! = nil

…它给出了错误:

不能用参数列表调用'SecAccessControlCreateWithFlags'类型'(CFAllocatorRef!, AnyObject !SecAccessControlCreateFlags,inout CFErrorRef !)"

相反,我传递以下变量(同样以地址操作符为前缀):

var accessControlError:UnsafeMutablePointer<Unmanaged<CFError>?>

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

不能用参数列表调用'SecAccessControlCreateWithFlags'类型'(CFAllocatorRef!, AnyObject !SecAccessControlCreateFlags,inout UnsafeMutablePointer ?>)的

…那么,发生了什么呢?

EDIT:忘记error参数。我似乎把地址取了两次(即指针指向指针)。相反,我应该这样做:

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 '&'
)

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

ANSWER:忘记error参数。我似乎把地址取了两次(即指针指向指针)。相反,我应该这样做:

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 '&'
) 

相关内容

  • 没有找到相关文章

最新更新