Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 326
Chris Prince Avatar asked Jan 08 '23 20:01

Chris Prince


1 Answers

Wrap the entire return statement in Int (return Int(self.coreDataSource…)).

like image 191
Aaron Brager Avatar answered Jan 15 '23 03:01

Aaron Brager