NSUInteger和NSInteger如何转换为Swift

4

在Swift 1.2中(没错,我还没换到引起我烦恼的Xcode 7),我有以下的表视图代理方法:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.coreDataSource.numberOfRowsInSection(section)
}

导致Xcode 6.4报错:

无法使用类型为'(Int)'的参数列表调用'numberOfRowsInSection'

我的CoreDataSource方法是从Objective-C桥接过来的,具有以下.h声明:

- (NSUInteger) numberOfRowsInSection: (NSUInteger) section;

如果我在我的表格视图代理方法中应用UInt强制转换:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.coreDataSource.numberOfRowsInSection(UInt(section))
}

现在Xcode出现了以下错误信息:
'UInt' 无法转换为 'Int'
看起来是一种进退两难的情况。
根据苹果文档中所说,Swift将NSUInteger和NSInteger桥接到Int。
你有什么想法吗?

1
你能把整个的返回语句放入 Int 中吗(返回 Int(self.coreDataSource…))? - Aaron Brager
哦哦哦哦。太好了!通常只需要另一双眼睛。我没有注意到有两个Int/UInt转换正在进行。现在已经解决了。谢谢! - Chris Prince
1个回答

9

将整个返回语句包装在 Int 中(return Int(self.coreDataSource…))。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接