Swift Data Types
Data type are used to describe the information of value. Because programming language is not work without values, They are need some input value and statement to perform some operation. Internally every statement use values and data (variable value and object values). That the reason data is divide in Swift and other programming language to specific section and categories. Programming language are provide special inbuilt operation and functionality and behaviour. Such as memory allocation, size capacity etc.
In Swift Programming data are divide in following category.
Booleans
Bool are used to create Boolean type of variable in Swift Programming. that is capable to contain two type of letteral value. Either true or false.
//define boolean variables
var areYouProgrammer:Bool
areYouProgrammer=true
var findBugs=false //that is a boolean variable
Conditional statement in swift is completely based on expression of boolean. When find non boolean value compiler are produce compilation error. See this example.
if 123 {
print("123")
}
Error
test.swift:1:4: error: 'Int' is not convertible to 'Bool'
if 123 {
^~~
In this example given if expression value is integer value. their resultant are not produce boolean result (true or false). In this situation compiler produce error. So avoid this type of situation using of provide valid compression condition, or also used boolean variables and value. For example
if 123 <= 500 {
//work on expression boolean result (if true)
print("123")
}
//or other case
var findBug=true
if findBug {
print("Error are Exist")
}else{
print("Yo @ No error Found")
}
Output
123
Error are Exist
Int (Integers)
Integer is capable to store whole number of value that cannot contains any fraction parts. Normal numbers are used to control iterations, mathematical calculation and define control statement.
Swift is provide various form of integers. Such as signed integers (positive and negative values) and unsigned integers(only positive) variants of 1 byte (8 bit), 2 bytes (16 bit), 4 bytes (32 bit) and 8 bytes (64 bytes).
Number is based on size that are divided into 4 category.
Size in bits | Signed | Range Signed Int | Unsigned | Range Unsigned Int |
---|---|---|---|---|
8 | Int8 | -128 to 127 | UInt8 | 255 |
16 | Int16 | -32768 to 32767 | UInt16 | 65535 |
32 | Int32 | -(2^31) to (2^31)-1 | UInt32 | (2^32)-1 (4294967295) |
64 | Int64 , Int | -(2^63) to (2^63)-1 | UInt64 | (2^64)-1 |
//Signed Integers
var intEigtBit : Int8 = -100
var intEixteenBit : Int16 = 100
var intThirtyTwoBit : Int32 = -100
var intSixtyFourBit : Int64 = 100
print("Integer 8 bit signed size : \( MemoryLayout.size(ofValue: intEigtBit)) Bytes ")
print("Integer 16 bit signed size : \( MemoryLayout.size(ofValue: intEixteenBit)) Bytes ")
print("Integer 32 bit signed size : \( MemoryLayout.size(ofValue: intThirtyTwoBit)) Bytes ")
print("Integer 64 bit signed size : \( MemoryLayout.size(ofValue: intSixtyFourBit)) Bytes ")
//UnSigned Integers
var unIntEigtBit : UInt8 = 100
var unIntEixteenBit : UInt16 = 100
var unIntThirtyTwoBit : UInt32 = 100
var unIntSixtyFourBit : UInt64 = 100
print("Integer 8 bit unsigned size : \( MemoryLayout.size(ofValue: unIntEigtBit)) Bytes ")
print("Integer 16 bit unsigned size : \( MemoryLayout.size(ofValue: unIntEixteenBit)) Bytes ")
print("Integer 32 bit unsigned size : \( MemoryLayout.size(ofValue: unIntThirtyTwoBit)) Bytes ")
print("Integer 64 bit unsigned size : \( MemoryLayout.size(ofValue: unIntSixtyFourBit)) Bytes ")
Output
Integer 8 bit signed size : 1 Bytes
Integer 16 bit signed size : 2 Bytes
Integer 32 bit signed size : 4 Bytes
Integer 64 bit signed size : 8 Bytes
Integer 8 bit unsigned size : 1 Bytes
Integer 16 bit unsigned size : 2 Bytes
Integer 32 bit unsigned size : 4 Bytes
Integer 64 bit unsigned size : 8 Bytes
Observe that size of similar variant of integers in form of signed and unsigned. there memory allocation size is same. But unsigned integer is capable to store positive numbers including zero. and signed integer are capable to store positive and negative numbers. Check their size limitation in following ways.
//Displaying maximum and minimum size of unsigned integers
print("Max size of UInt8 : \(UInt8.max) ")
print("Min size of UInt8 : \(UInt8.min) ")
print("Max size of UInt16 : \(UInt16.max) ")
print("Min size of UInt16 : \(UInt16.min) ")
print("Max size of UInt32 : \(UInt32.max) ")
print("Min size of UInt32 : \(UInt32.min) ")
print("Max size of UInt64 : \(UInt64.max) ")
print("Min size of UInt64 : \(UInt64.min) ")
Output
Max size of UInt8 : 255
Min size of UInt8 : 0
Max size of UInt16 : 65535
Min size of UInt16 : 0
Max size of UInt32 : 4294967295
Min size of UInt32 : 0
Max size of UInt64 : 18446744073709551615
Min size of UInt64 : 0
//Displaying maximum and minimum size of signed integers
print("Max size of Int8 : \(Int8.max) ")
print("Min size of Int8 : \(Int8.min) ")
print("Max size of Int16 : \(Int16.max) ")
print("Min size of Int16 : \(Int16.min) ")
print("Max size of Int32 : \(Int32.max) ")
print("Min size of Int32 : \(Int32.min) ")
print("Max size of Int64 : \(Int64.max) ")
print("Min size of Int64 : \(Int64.min) ")
Output
Max size of Int8 : 127
Min size of Int8 : -128
Max size of Int16 : 32767
Min size of Int16 : -32768
Max size of Int32 : 2147483647
Min size of Int32 : -2147483648
Max size of Int64 : 9223372036854775807
Min size of Int64 : -9223372036854775808
Integer Literals
Literals is an constant values which are assigned to variable, That can be form of variable and a value. For example.
var binaryData = 0b1010 //Binary literal 10 in decimal
var octalData = 0o31 //Octal literal, 25 in decimal
var decimal = 24 //Decimal literal
var hexaDecimal = 0x16 //Hexa Decimal, 22 in decimal
var decimalFormat=100_2_00 // 100200
print("binaryData : \(binaryData)")
print("octalData : \(octalData)")
print("decimal : \(decimal)")
print("hexaDecimal : \(hexaDecimal)")
print("Decimal Format : \(decimalFormat)")
Output
binaryData : 10
octalData : 25
decimal : 24
hexaDecimal : 22
Decimal Format : 100200
By default number data type is support decimal system. When we assign any value that are internally decimal value are assigned.
Convert number to string
let number : Int = 12345
var textValue=String(number) //convert number to base 10
print(textValue)
textValue=String(number,radix:16)
print(textValue) //base 16
textValue=String(number,radix:2)
print(textValue) //base 2 binary format
textValue=String(number,radix:8)
print(textValue) //base 82 octal format
Output
12345
3039
11000000111001
30071
Convert string to number
Similar way we can convert string to integer.
let textValue : String = "12345"
var number = Int(textValue)! //string to number to base 10
print(number)
Output
12345
Floating Point Numbers
This is variant of number but that is capable to stored fractional component. There is two type of data type is available in swift programming that are held to fraction floating point values. Double and Float.
Size of Double data type are 64 bit (8 bytes). And that can hold the 15 decimal digits. size of Float data type are 32 bit (4 bytes). And that can hold the value of 6 decimal digits.
Without define data type of variable assigned a value that are contain floating component. Then complier are always chooses double data type
Character and Strings
String is an sequence of ordered characters, That are define inside a quotes. In other words, String is collection of characters which are defined inside a quotes.
extension String {
//Swift 4
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
}
let textData="ABCD" //String literals
print(textData)
print(textData[0]) //A
print(textData[1]) //B
Output
ABCD
A
B
String var data type is mutable that means there are possible to assigned new values to string variable. And concatenate the two string values by using of (+) operators.
User defined data type
Structure (struct), Enumeration (enum) and class are used to make custom user defined data type in Swift Programming language.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment