When you declare a variable or a constant in Swift you do not need to explicitly specify what datatype it belongs to. But if you want to define datatype for a variable or a constant, you can do it using
Type Annotations
Syntax (var) : var varName: DataType
Syntax (let) : let varName: DataType
In the below example, constant myInt is a variable of type Integer and myString is a constant of type String.
//
// main.swift
// Swift Tutorials 2014-15
// Constants in Swift
//
// Created by Code2care on 15/09/14.
// Copyright (c) 2014 Code2care. All rights reserved.
//
let myInt:Int = 20
var myString:String = "Swift is easy to learn!"
println("\(myInt)")
println("\(myString)")
Output :
20
Swift is easy to learn!
Program ended with exit code: 0
In the below example, we have tried to assing myInt of type Integer a String value and myString of type String a is a Integer value, resulting to errors.
//
// main.swift
// Swift Tutorials 2014-15
// Constants in Swift
//
// Created by Code2care on 15/09/14.
// Copyright (c) 2014 Code2care. All rights reserved.
//
let myInt:Int = "Swift is easy to learn!"
var myString:String = 20
println("\(myInt)")
println("\(myString)")
Output :
Type 'int' does not confirm to protocol error 'StringLiteralConvertible'
Type 'String' does not confirm to protocol error 'InregerLiteralConvertible'