Last active
August 29, 2015 14:17
-
-
Save yassinagx/52ca8825a9da3305b71f to your computer and use it in GitHub Desktop.
Swift/IOS
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
Variable | |
var hello = "Hello World" | |
Constant | |
let celerity = 299792458 | |
Explicit type | |
var hello: String = "Hello World" | |
var celerity: Int = 299792458 | |
print("Hello World") | |
Print with new line | |
println("Hello World") | |
Interpolation | |
var name = "Peter" | |
println("Hello /(name)") | |
New line | |
print("\n") | |
New tab | |
print("\t") | |
Cast | |
var areaInMeter = Double(area) / 10.76 | |
Increment | |
year++ | |
Decrement | |
year-- | |
Negatif | |
var off = !true | |
Unary operator | |
var year = 2015 | |
new_year = ++year | |
Array | |
var animals = ["Dog", "Cat", "Fish"] | |
Array explicit type | |
var animals: [String] = ["Dog", "Cat", "Fish"] | |
Array count | |
animals.count | |
Array remove element | |
animals.removeAtIndex(1) | |
Array add element | |
animals.append("Octopus") | |
animals += ["Octopus"] | |
Dictonary | |
var countries = ["FR": "France", "CA": "Canada", "BE": "Belgium"] | |
For in loop | |
for animal in animals{ | |
println(animal) | |
} | |
Range | |
1...10 | |
While | |
year = 1995 | |
while year < 2015{ | |
println("At \(year)") | |
year++ | |
} | |
Do while | |
do { | |
println("At \(year)") | |
year++ | |
} while year < 2015 | |
If | |
if year == 2014{ | |
println("Swift birth") | |
} | |
Else | |
if year == 2014{ | |
println("Swift birth") | |
} else { | |
println("Uninteresting year") | |
} | |
Switch | |
switch year{ | |
case 2014: | |
println("Swift birth") | |
default: | |
println("Uninteresting year") | |
} | |
Switch | |
switch year{ | |
case 1900...1999: | |
println("20 century") | |
case 2004, 2008, 2012: | |
println("Olympic Games") | |
default: | |
println(year) | |
} | |
Basic Function | |
func calculateArea(){ | |
let height = 10 | |
let width = 8 | |
println("The area of the room is \(height * width)") | |
} | |
Function with parameters | |
func calculateArea(height: Int, width: Int){ | |
println("The area of the room is \(height * width)") | |
} | |
calculateArea(100, 200) | |
Function with parameters name | |
func calculateArea(#height: Int, #width: Int){ | |
println("The area of the room is \(height * width)") | |
} | |
calculateArea(height: 100, width: 200) | |
Function with return | |
func calculateArea(#height: Int, #width: Int) -> Int{ | |
return height * width | |
} | |
Function return tuple | |
func searchCountry (#countryName: String) -> (Bool,String) { | |
let countries = ["France", "Canada", "Belgium"] | |
var found = (false,"\(countryName) is unavailable country") | |
for c in countries { | |
if c == countryName { | |
found = (true,"\(name) is available country)") | |
} | |
} | |
return found | |
} | |
Optional | |
func searchCountry (#countryName: String) -> String? { | |
let countries = ["France", "Canada", "Belgium"] | |
for c in countries { | |
if c == countryName { | |
return c | |
} | |
} | |
return nil | |
} | |
if let countryName = searchCountry(countryName: "Canada"){ | |
println("\(countryName) is available country") | |
} | |
Optional Chaining | |
if let countryName = searchCountry(countryName: "Canada")?.uppercaseString{ | |
println("\(countryName) is available country") | |
} | |
Enum | |
enum Day{ | |
case Monday | |
case Thuesday | |
// More | |
} | |
Day.Monday | |
Enum raw values | |
enum Day: Int{ | |
case Monday = 1 | |
case Thuesday = 2 | |
// More | |
} | |
Day.Monday.rawValue | |
Enum associated values | |
enum Status{ | |
case Success(String) | |
case Failure(String) | |
} | |
let downloadStatus = Status.Failure("Network connection unavailable") | |
Struct | |
struct Person { | |
var firstName: String | |
var lastName: String | |
init(firstName: String, lastName: String){ | |
self.firstName = firstName | |
self.lastName = lastName | |
} | |
} | |
var pierre = Person(firstName: "Pierre", lastName: "Dupont") | |
Overriding | |
class Train: Vehicle { | |
override func makeNoise() { | |
println("Choo Choo") | |
} | |
} | |
Convenience | |
init(content: String, sender: String, recipient: String) | |
{ | |
self.content = content | |
super.init(sender: sender, recipient: recipient) | |
} | |
convenience init() | |
{ | |
self.init(content: "") | |
} | |
Getter and Setter | |
class Temperature { | |
var celsius: Float = 0.0 | |
var fahrenheit: Float { | |
get{ | |
return (celsius * 1.8) + 32 | |
} | |
set { | |
celsius = (newValue - 32) / 1.8 | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment