Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: CInt to Int conversion?

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?

like image 442
jhurliman Avatar asked Jun 06 '14 22:06

jhurliman


People also ask

How do I convert a string to an int in Swift?

Using NSString We can convert a numeric string into an integer value indirectly. Firstly, we can convert a string into an NSString then we can use “integerValue” property used with NSStrings to convert an NSString into an integer value.

What is int32 in Swift?

A 32-bit signed integer value type.


1 Answers

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

like image 98
Connor Avatar answered Sep 17 '22 19:09

Connor