Ennio E. Meier
Ennio E. Meier

Reputation: 25

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. I'm trying to translate this code from objective-c to 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) {

What does cause this error? Is the rest of the code in swift correct or are there any other errors?

Upvotes: 0

Views: 829

Answers (2)

Ennio E. Meier
Ennio E. Meier

Reputation: 25

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)")
}

Upvotes: 0

Martin R
Martin R

Reputation: 539685

chunks.count has the type Int, but the if statement requires a boolean expression. This is different from (Objective-)C, where the controlling expression of an if statement can have any scalar type and is compared with zero.

So the corresponding Swift code for

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

is

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

Upvotes: 2

Related Questions