Swift: CInt to Int conversion?
Asked Answered
B

2

13

What is the most concise way to convert a CInt to an Int (the native integer type) in Swift? Right now I'm using:

NSNumber.numberWithInt(myCInt).integerValue

Is this the preferred approach?

Brose answered 6/6, 2014 at 22:26 Comment(0)
H
24

You can do it like this:

var c: CInt = 4
var i: Int = Int(c)

CInt is a type alias of Int32 which is one of the types Int can take in its init.

Here are the types I've found you can use like that: UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, UInt, Float, Double, Float80

Hallucinatory answered 6/6, 2014 at 22:28 Comment(1)
Simple, thanks! It looks like you can go the other way too: CInt(myInt)Brose
O
0

You can try this:

        var myCInt : CInt = 4

        var newNumber : NSNumber = NSNumber(int: myCInt)
Oquassa answered 17/11, 2014 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.