【发布时间】:2015-02-05 23:52:30
【问题描述】:
我在 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) {
是什么导致了这个错误? swift中的其余代码是否正确或是否有任何其他错误?
【问题讨论】:
标签: objective-c swift methods nsstring boolean