Swift – Hello World! (Variables & Simple Values) Part1

This post is about learning to define variables, constants and simple values and saying Hello World! in a Swift way.

Hello World!

It’a classic, printing “Hello World!” first in a new programming language. It’s so easy to print a text in Swift too.

print(Hello, world!)

let for constants, var for variables.

let is used for defining constants and var for variables.

let myPiNumber = 3.1415
var myCircleRadius = 5

Swift is very clever to understand(/infer) what type your value is. If you type 99, it knows this is integer or for 100.0, it knows that’s is double. You don’t have to specify your values’ types (integer, double, float, string). If you want  to show a value as specific type rather than Swift’s selection. You can explicitly do it by defining as showing at following third and forth rows.

let implicitInteger = 99
let implicitDouble = 100.0
let explicitDouble: Double = 100
let floatnum1 : Float = 34.3

Values are never implicitly converted to another type. If you need to convert a value to a different type, explicitly make an instance of the desired type.

let prefixOfSentence = "The developer's old is "
var howOldIsDeveloper = 26
var howOldIsDeveloperSentence = label + String(width)

There is a simple way to concat variables that may have other types than strings. String() can be wrote simply as \().

var developersOldSentence = label + \(width)

You can use operators such as – , + , %  in \() . So you can make calculations right before using contacting your strings.

let birthYear = 1990
var currentYear = 2016
var developersOldSentence = "I am \(currentYear - birthYear) years old."


Lets talk a little bit about Arrays. You can define your static arrays as follows.

let languagesThatIKnow = ["pascal", "c", "c#", "java", "objective-c", "swift"]
let languagesAndExperiences = [ "pascal" : 1.5,
                                "c" : 3,
                                "java" : 8,
                                "objective-c" : 4]

If you don’t know how many item you will have in your array, but you know which type they are, you can define your in [] brackets by specifying the type of them inside, and use () for calling initialization. Same is for dictionaries, you can specify key and object type respectively inside of the [] brackets.

Pseudo for defining dictionaries –> [key type :value type]()

var languagesThatIKnow = [String]()
var languagesThatIHaveExperince= [String: Float]()

If you don’t know which type you will use for keys or objects in your array / dictionary and these values can ben inferred, you can simply use empty [] brackets / [:] brackets.

var languagesThatIKnow = []
var languagesThatIHaveExperince = [:]

See you in another chapter!


One thought on “Swift – Hello World! (Variables & Simple Values) Part1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s