4

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?

Chris Prince
  • 7,288
  • 2
  • 48
  • 66
  • 1
    Can you wrap the entire return statement in `Int` (return `Int(self.coreDataSource…)`) – Aaron Brager Aug 04 '15 at 03:02
  • 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! – Chris Prince Aug 04 '15 at 03:31

1 Answers1

9

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

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287