NSUInteger and NSInteger bridging to Swift
Asked Answered
S

1

5

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?

Subside answered 4/8, 2015 at 2:55 Comment(2)
Can you wrap the entire return statement in Int (return Int(self.coreDataSource…))Stringfellow
Ohhhhh. Good! It often takes just another set of eyes. I wasn't noticing that there were two Int/UInt conversions taking place. That's got it now. Thanks!Subside
S
10

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

Stringfellow answered 4/8, 2015 at 3:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.