Anton
Anton

Reputation: 6033

Convert CGFloat to CFloat in Swift

I'm simply trying to round the CGFloat return value of CGRectGetWidth.

override func layoutSubviews()  {
    let roundedWidth = roundf(CGRectGetWidth(self.bounds))
    ...
}

The compiler won't let me, giving the error:

'NSNumber' is not a subtype of 'CFloat'.

I guess there is some basic thing I am missing here. roundf is taking a CFloat as argument, so how can I convert my CGFloat to CFloat to make the conversion?

Update:

Now using round instead of roundf but I am still getting the same error. I've tried cleaning the project and restarting Xcode.

enter image description here

Upvotes: 9

Views: 3800

Answers (4)

MacMark
MacMark

Reputation: 6549

CGFloat() works with all architectures for me.

Upvotes: 1

Jerry Jones
Jerry Jones

Reputation: 5403

The problem is that CGFloat is platform dependent (just like it is in ObjC). In a 32 bit environment, CGFloat is type aliased to CFloat - in a 64 bit environment, to CDouble. In ObjC, without the more pedantic warnings in place, round was happy to consume your float, and roundf your doubles.

Swift doesn't allow implicit type conversions.

Try building for an iPhone 5 and a iPhone 5s simulator, I suspect you'll see some differences.

Upvotes: 5

Anton
Anton

Reputation: 6033

For some reason I needed to make an explicit typecast to CDouble to make it work.

let roundedWidth = round(CDouble(CGRectGetWidth(self.bounds)))

I find this pretty strange since a CGFloat is a CDouble by definition. Seems like the compilator got a bit confused here for some reason.

Upvotes: 1

Ben Gottlieb
Ben Gottlieb

Reputation: 85532

CGRect members are CGFloats, which, despite their name, are actually CDoubles. Thus, you need to use round(), not roundf()

override func layoutSubviews()  {
    let roundedWidth = round(CGRectGetWidth(self.bounds))
    ...
}

Upvotes: 5

Related Questions