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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27304158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:19:06  来源:igfitidea点击:

Type 'Boolean' does not conform to protocol 'BooleanType'

objective-cxcodeswiftosx-yosemite

提问by Chris Calo

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 文档(和教程化)创建 Launch Helper 时,我似乎遇到了将 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'

Type 'Boolean' does not conform to protocol 'BooleanType'

I have tried casting to Boolin 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

SMLoginItemSetEnabled(launchDaemon, true) as Bool

which yields the error:

这会产生错误:

'Boolean' is not convertible to 'Bool'

'Boolean' is not convertible to 'Bool'

...seriously?

...严重地?

回答by Martin R

Booleanis 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 Booleantype (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 BooleanLiteralConvertibleextension allows the automatic conversion of the second argument trueto Boolean.
  • The BooleanTypeextension allows the automatic conversion of the Booleanreturn value of the function to Boolfor the if-statement.
  • BooleanLiteralConvertible扩展允许将第二个参数自动转换trueBoolean.
  • BooleanType扩展允许将Boolean函数的返回值自动转换Bool为 if 语句。


Update:As of Swift 2 / Xcode 7 beta 5,the "historic Mac type" Booleanis mapped to Swift as Bool, which makes the above extension methods obsolete.

更新:Swift 2 / Xcode 7 beta 5 开始,“历史 Mac 类型”Boolean被映射到 Swift as Bool,这使得上述扩展方法过时了。

回答by Loebre

Right, I had a similar issue trying to get the BOOL return of an objective-C method in Swift.

是的,我在尝试在 Swift 中获取 Objective-C 方法的 BOOL 返回时遇到了类似的问题。

Obj-C:

对象-C:

- (BOOL)isLogging
{
    return isLogging;
}

Swift:

迅速:

    if (self.isLogging().boolValue)
    {
        ...
    }

this was the way that I got rid of the error.

这是我摆脱错误的方式。