Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Int vs Integer in Swift

Tags:

Does anyone know what the difference is between these two types?

The docs only refer to Int but Xcode 6 auto complete only gives me Integer when I type. I started using Integer when porting code only to find that you have to cast between the two types.

For instance the following code gives the error Could not find an overload for '+' that accepts the supplied arguments.

var number1 : Int = 5 var number2 : Integer = 10 number1 + number2 
like image 864
Berry Blue Avatar asked Jun 05 '14 06:06

Berry Blue


People also ask

What is the difference between Int and uint8_t?

Int8 is an Integer type which can store positive and negative values. UInt8 is an unsigned integer which can store only positive values. You can easily convert UInt8 to Int8 but if you want to convert Int8 to UInt8 then make sure value should be positive.

What type of integer is denoted by Int8?

An 8-bit signed integer value type.

What is data type in Swift?

Swift offers a collection of built-in data types which are string, integer, floating-point numbers, and Booleans. These data types are also found in most programming languages.

How do you create an Int in Swift?

To create an Integer variable in Swift, declare the variable as Int, or assign any integer value to the variable, or assign new Int instance to the variable.


1 Answers

An Int is the type whilst an Integer is a protocol it implements.

You should be using Int in declarations, i.e:

var num: Int = 5 

which is also the type that's inferred for integer literals when a type isn't specified, i.e:

var num = 5 
like image 188
mythz Avatar answered Sep 20 '22 07:09

mythz