简体   繁体   中英

NSUInteger and NSInteger bridging to Swift

With Swift 1.2 (yes, I've not switched over to Xcode 7, which is causing me grief), I have the following table view delegate method:

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

which causes Xcode 6.4 to give the error:

Cannot invoke 'numberOfRowsInSection' with an argument list of type '(Int)'

My CoreDataSource method, bridged from Objective-C, has the following .h declaration:

- (NSUInteger) numberOfRowsInSection: (NSUInteger) section;

If I apply a UInt coercion in my table view delegate method:

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

Xcode now gives the error:

'UInt' is not convertible to 'Int'

So, it looks to be damed if you do, damed if you don't kind of scenario.

From Apple's docs , they say:

Swift bridges NSUInteger and NSInteger to Int.

Thoughts?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM