[英]Translating Objective-C to Swift - Error: Type 'Int' does not conform to protocol 'BooleanType'
我在Google和SO上進行了搜索,但沒有找到有關此問題的任何有用幫助。 我正在嘗試將此代碼從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)
}
}
}
但我總是在行中收到錯誤“類型'Int'不符合協議'BooleanType'”:if(chunks.count){
是什么導致此錯誤? 快速其余代碼是否正確,還是有其他錯誤?
chunks.count
具有類型Int
,但是if
語句需要一個布爾表達式。 這與(Objective-)C不同,后者的if語句的控制表達式可以具有任何標量類型,並與零進行比較。
所以對應的Swift代碼
if ([chunks count]) { ... }
是
if chunks.count != 0 { ... }
我自己解決了答案。
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.