简体   繁体   English

类型'Boolean'不符合协议'BooleanType'

[英]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) { ... }
  • The BooleanLiteralConvertible extension allows the automatic conversion of the second argument true to Boolean . BooleanLiteralConvertible扩展允许将第二个参数true自动转换为Boolean
  • The 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.

相关问题 类型“ AnyObject”不符合协议“ BooleanType” - Type 'AnyObject' does not conform to protocol 'BooleanType' 将Objective-C转换为Swift-错误:类型&#39;Int&#39;不符合协议&#39;BooleanType&#39; - Translating Objective-C to Swift - Error: Type 'Int' does not conform to protocol 'BooleanType' Swift - 类型“*”不符合协议“*” - Swift - Type '*' does not conform to protocol '*' 类型“ ViewController”不符合协议“ FBSDKLoginButtonDelegate” - The type 'ViewController' does not conform to protocol 'FBSDKLoginButtonDelegate' 类型MyViewController不符合协议&#39;STPPaymentContextDelegate&#39; - Type MyViewController does not conform to protocol 'STPPaymentContextDelegate' 类型“ ViewController”不符合协议“ MCSessionDelegate” - Type 'ViewController' does not conform to protocol 'MCSessionDelegate' 输入id <Protocol1> 不符合id <Protocol2> - 但确实如此! - Type id <Protocol1> does not conform to id <Protocol2> — but it does! 属性不符合协议 - Property does not conform to protocol 快速字典填充问题:类型“ AnyObject”不符合协议“ NSCopying” - swift dictionary population issue: type 'AnyObject' does not conform to protocol 'NSCopying' UIViewController! 不符合协议“ LogicValue” - UIViewController! Does not conform to protocol “LogicValue”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM