Swift 中的 SecAccessControlCreateWithFlags()
SecAccessControlCreateWithFlags() in Swift
我正在尝试从 swift 代码调用 Security.framework 中的函数。暂时忘记 "error out" (最后一个)参数,如果我这样调用函数:
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 的自动完成功能原型)...编译没有问题。 怎么回事?
接下来,错误参数。我应该通过什么?迁移我的 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>?>
(与原型自动完成建议的类型相同),我得到:
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 '&'
)
我正在尝试从 swift 代码调用 Security.framework 中的函数。暂时忘记 "error out" (最后一个)参数,如果我这样调用函数:
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 的自动完成功能原型)...编译没有问题。 怎么回事?
接下来,错误参数。我应该通过什么?迁移我的 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>?>
(与原型自动完成建议的类型相同),我得到:
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 '&'
)