简体   繁体   English

将Objective-C转换为Swift-错误:类型'Int'不符合协议'BooleanType'

[英]Translating Objective-C to Swift - Error: Type 'Int' does not conform to protocol 'BooleanType'

I searched on google and on SO but didn't find any useful help for this issue. 我在Google和SO上进行了搜索,但没有找到有关此问题的任何有用帮助。 I'm trying to translate this code from objective-c to swift: 我正在尝试将此代码从Objective-C转换为Swift:

- (void)metaTitleUpdated:(NSString *)title {
NSLog(@"delegate title updated to %@", title);

NSArray *chunks = [title componentsSeparatedByString:@";"];
if ([chunks count]) {
    NSArray *streamTitle = [[chunks objectAtIndex:0] componentsSeparatedByString:@"="];
    if ([streamTitle count] > 1) {
        titleLabel.text = [streamTitle objectAtIndex:1];
    }
}
}

so far i have translated it to this: 到目前为止,我已经将其翻译为:

func metaTitleUpdated(input: String) {
    println("delegate title updated to \(title)")

    let chunks: NSArray = title!.componentsSeparatedByString(";")
    if (chunks.count) {
        let streamTitle = chunks .objectAtIndex(0) .componentsSeparatedByString(";")
        if (streamTitle.count > 1) {
        titleLabel.text = streamTitle.objectAtIndex(1)
        }
    }

}    

but i always get the error "Type 'Int' does not conform to protocol 'BooleanType'" in the line: if (chunks.count) { 但我总是在行中收到错误“类型'Int'不符合协议'BooleanType'”:if(chunks.count){

What does cause this error? 是什么导致此错误? Is the rest of the code in swift correct or are there any other errors? 快速其余代码是否正确,还是有其他错误?

chunks.count has the type Int , but the if statement requires a boolean expression. chunks.count具有类型Int ,但是if语句需要一个布尔表达式。 This is different from (Objective-)C, where the controlling expression of an if statement can have any scalar type and is compared with zero. 这与(Objective-)C不同,后者的if语句的控制表达式可以具有任何标量类型,并与零进行比较。

So the corresponding Swift code for 所以对应的Swift代码

if ([chunks count]) { ... }

is

if chunks.count != 0 { ... }

I solved the answer by myself. 我自己解决了答案。

func metaTitleUpdated(title: String) {
    var StreamTitle = split(title) {$0 == "="}
    var derRichtigeTitel: String = StreamTitle[1]
  titleLabel.text = derRichtigeTitel
    println("delegate title updated to \(derRichtigeTitel)")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM