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
Type 'Boolean' does not conform to protocol 'BooleanType'
提问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 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
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
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 argumenttrue
toBoolean
. - The
BooleanType
extension allows the automatic conversion of theBoolean
return value of the function toBool
for the if-statement.
- 该
BooleanLiteralConvertible
扩展允许将第二个参数自动转换true
为Boolean
. - 该
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 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.
这是我摆脱错误的方式。