Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Objective-C转换为Swift-错误:Type';Int';不符合协议';布尔型';_Objective C_Swift_Methods_Nsstring_Boolean - Fatal编程技术网

将Objective-C转换为Swift-错误:Type';Int';不符合协议';布尔型';

将Objective-C转换为Swift-错误:Type';Int';不符合协议';布尔型';,objective-c,swift,methods,nsstring,boolean,Objective C,Swift,Methods,Nsstring,Boolean,我在谷歌等网站上搜索,但没有找到任何有用的帮助。 我正在尝试将此代码从objective-c转换为swift: - (void)metaTitleUpdated:(NSString *)title { NSLog(@"delegate title updated to %@", title); NSArray *chunks = [title componentsSeparatedByString:@";"]; if ([chunks count]) { NSArray *stream

我在谷歌等网站上搜索,但没有找到任何有用的帮助。 我正在尝试将此代码从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];
    }
}
}
到目前为止,我已将其翻译为:

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

}    
但我总是在:if(chunks.count)行中出现错误“Type'Int'不符合协议'BooleanType'”{


导致此错误的原因是什么?swift中的其余代码是否正确或是否存在任何其他错误?

chunks.count
的类型为
Int
,但
if
语句需要布尔表达式。 这与(Objective-)C不同,在(Objective-)C中,if语句的控制表达式可以具有任何标量类型,并与零进行比较

所以对应的Swift代码

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


我自己解决了这个问题

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

你的意思是
chunks.count>0
?:)@GuyKogus:Oops,是的。谢谢!解决了错误谢谢。但是现在我得到一个错误:“[AnyObject]”在“titleLabel.text=streamTitle.objectAtIndex(1)”行没有名为“objectAtIndex”的成员这有什么问题?@EnnioE.Meier:这意味着streamTitle是一个Swift数组,而不是NSArray。您可以尝试
让streamTitle:NSArray=…
,就像您对chunk定义所做的那样。或者,使用Swift数组索引。我现在将代码更改为:func metaTitleUpdated(输入:String){println(“委托标题已更新为(标题)”)如果chunks.count!=0{let StreamTitle:NSArray=chunks.objectAtIndex(0)。如果(StreamTitle.count>1){titleLabel.text!=StreamTitle.objectAtIndex(1),则让chunks:NSArray=title(“;”)组件通过字符串(=”)分离作为NSString}},但我在运行时遇到一个错误,说“致命错误:在展开可选值时意外发现nil”
func metaTitleUpdated(title: String) {
    var StreamTitle = split(title) {$0 == "="}
    var derRichtigeTitel: String = StreamTitle[1]
  titleLabel.text = derRichtigeTitel
    println("delegate title updated to \(derRichtigeTitel)")
}