//Variable Declaration -
To declare a variable , the data type of the variable is inferred from the value assigned to the variable .
To declare a variable , the data type of the variable is inferred from the value assigned to the variable .
var str = "Hello, playground"
//change the value of the variable
str = "Hello Swift"
//Error --- The "str" variable has a datatype of string , and cannot be re-assinged as an Int , float etc..
//You can also explicitly declare the data type in variable
var name :String = "Hello , Swift"
var age: Int = 45
commonly used data types include String, Int, Float, Double, Bool. Sample variable declaration for the different data types -
var S:String = "Swift"
var W: Bool = true
var I: Int = 67
var F: Float = 13.3
var T: Double = 2.2
You can use x.dynamicType to find out the data type of var "X"
let string = "Hello World!”
let array = [“Hello”, “Swift”]
let dictionary = [“dictionary”: 1]
print(string.dynamicType) // "String"
// Get type name as a string
String(string.dynamicType) // "String"
String(array.dynamicType) // "Array<String>"
String(dictionary.dynamicType) // "Dictionary<String, Int>"
// Get full type as a string
String(reflecting: string.dynamicType) // "Swift.String"
String(reflecting: array.dynamicType) // "Swift.Array<Swift.String>"
String(reflecting: dictionary.dynamicType) // "Swift.Dictionary<Swift.String, Swift.Int>"
No comments:
Post a Comment