[英]Type 'Boolean' does not conform to protocol 'BooleanType'
In attempting to create a Launch Helper as per the Apple docs (and tutorial-ized ), I seem to be hitting a hiccup caused by porting the Objective-C code into Swift... who's compiler couldn't be any more redundant in this case. 在尝试根据Apple文档(和教程化 )创建启动助手时,我似乎遇到了将Objective-C代码移植到Swift中导致的打嗝...谁的编译器在这方面不再多余案件。
import ServiceManagement
let launchDaemon: CFStringRef = "com.example.ApplicationLauncher"
if SMLoginItemSetEnabled(launchDaemon, true) // Error appears here
{
// ...
}
The error seems to consistently be: 错误似乎一直是:
Type 'Boolean' does not conform to protocol 'BooleanType'
I have tried casting to Bool
in a number of locations, in case I'm simply dealing with a redundant, archaic primitive (either brought in by Obj-C or Core Foundation), to no avail. 我曾尝试在很多地方
Bool
,以防我只是处理冗余的古老原语 (由Obj-C或Core Foundation引入),但无济于事。
Just in case, I have tried casting the response: 为了以防万一,我尝试了回复:
SMLoginItemSetEnabled(launchDaemon, true) as Bool
which yields the error: 产生错误:
'Boolean' is not convertible to 'Bool'
...seriously? ......当真?
Boolean
is a "historic Mac type" and declared as Boolean
是“历史Mac类型”并声明为
typealias Boolean = UInt8
so this compiles: 所以这个编译:
if SMLoginItemSetEnabled(launchDaemon, Boolean(1)) != 0 { ... }
With the following extension methods for the Boolean
type (and I am not sure if this has been posted before, I cannot find it right now): 使用以下
Boolean
类型的扩展方法(我不确定之前是否已发布,我现在无法找到它):
extension Boolean : BooleanLiteralConvertible {
public init(booleanLiteral value: Bool) {
self = value ? 1 : 0
}
}
extension Boolean : BooleanType {
public var boolValue : Bool {
return self != 0
}
}
you can just write 你可以写
if SMLoginItemSetEnabled(launchDaemon, true) { ... }
BooleanLiteralConvertible
extension allows the automatic conversion of the second argument true
to Boolean
. BooleanLiteralConvertible
扩展允许将第二个参数true
自动转换为Boolean
。 BooleanType
extension allows the automatic conversion of the Boolean
return value of the function to Bool
for the if-statement. BooleanType
扩展允许的自动转换Boolean
函数的返回值Bool
的if语句。 Update: As of Swift 2 / Xcode 7 beta 5, the "historic Mac type" Boolean
is mapped to Swift as Bool
, which makes the above extension methods obsolete. 更新:从Swift 2 / Xcode 7 beta 5开始, “历史Mac类型”
Boolean
被映射到Swift作为Bool
,这使得上述扩展方法过时了。
Right, I had a similar issue trying to get the BOOL return of an objective-C method in Swift. 是的,我有一个类似的问题试图在Swift中获得BOOL返回Objective-C方法。
Obj-C: OBJ-C:
- (BOOL)isLogging
{
return isLogging;
}
Swift: 迅速:
if (self.isLogging().boolValue)
{
...
}
this was the way that I got rid of the error. 这是我摆脱错误的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.