Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/test.swift
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (60 sloc)
1.38 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
SWIFT TOUR | |
implicit variables and immutable variables | |
explicit variables | |
values need to be explicitly converted | |
use \() to convert a variable to a string (in quotes) | |
arrays and dictionaries | |
*/ | |
// array | |
let colours:[String] = ["red", "orange", "green"] | |
print(colours.count) | |
print("\(colours)") | |
var shoppingList = [String]() | |
shoppingList.append("bread") | |
shoppingList.append("butter") | |
print("\(shoppingList)") | |
for item in shoppingList { | |
print("\(item)") | |
} | |
// dictionary | |
var book = [ | |
"isbn": "1491943122", | |
"title": "Learning Node" | |
] | |
var ages = [String:Int]() | |
ages["john"] = 42 | |
ages["jane"] = 24 | |
print("\(ages)") | |
for (name, age) in ages { | |
print("\(name) is \(age) years old") | |
} | |
// normally functions use their parameter names as labels so you need to have a custom argument label before each parameter name. | |
func printPerson(withName name:String, andAge age:Int) { | |
print("\(name) is \(age) years old") | |
} | |
printPerson(withName: "frank", andAge: 64) | |
// use _ if you want to omit the first argument label. | |
func add(_ num1:Int, and num2:Int) -> Int { | |
return num1 + num2 | |
} | |
print(add(24, and: 42)) | |
// OOP | |
/* | |
objects and classes | |
importing from separate file | |
create instance by putting parenthesis before class name. | |
private instance variables | |
properties (getters and setters) | |
methods | |
static | |
*/ | |
// ASYNC | |
/* | |
closures (callbacks) | |
*/ | |
// FOUNDATION FRAMEWORK | |
// UNIT TESTING | |
/* | |
importing XCUnit | |
*/ |