Last active
August 29, 2015 14:03
-
-
Save nazywamsiepawel/44ea8f53fbe463d5e1c7 to your computer and use it in GitHub Desktop.
Swift Notes
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 notes */ | |
import Cocoa | |
/* Use let to make a contant and var to make a variable, implicitly infer the type */ | |
var cheesyVariable = 100 | |
let cheesyConstant = 666 | |
let implicitDouble = 70.0 | |
//cheesyConstant++; //cheesyConstant is a constant, cannot modify it | |
/* | |
Whenever possible, use let. It's a good practice and will allow the compiler to perform optimizations. | |
Specify the type after colon : | |
*/ | |
var explicitDouble: Double = 70; | |
let teamSize: Int = 100 | |
let aintThisMad: Bool = true | |
var explicitString: String = "Lorem ipsum" | |
var inferredString = "סוף הדרך" | |
var 猫 = "cat" | |
let euroSymbol: Character = "€" | |
var emptyString = "" | |
var emptyString2 = String() | |
/* Use \() to include values in strings */ | |
let steaks = 123; | |
let steakType = "sirloin" | |
let dinnerSummary = "I have \(steakType) \(steaks) steaks!" | |
let dinnerSummary2 = "Now I have \(steakType) \(steaks*10) steaks!" | |
let concatenateString = (dinnerSummary + " I changed my mind " + dinnerSummary2 + "and a " + 猫).uppercaseString | |
/*Use countElements to get length of the string */ | |
concatenateString.substringFromIndex(countElements(concatenateString)-11) | |
/*use .componentsSeparatedByCharactersInSet() to explode string to array */ | |
var myString = "One-Two-Three-1 2 3" | |
var array : String[] = myString.componentsSeparatedByCharactersInSet(NSCharacterSet (charactersInString: "- ")) | |
/* optionals */ | |
var optionalString: String? = "Hello" | |
optionalString == nil | |
var optionalName: String? = nil //"John Appleseed" | |
var greeting = "Hello!" | |
if let name = optionalName { | |
greeting = "Hello, \(name)" | |
} | |
/* FOR IN */ | |
let individualScores = [75, 43, 103, 87, 12] | |
var teamScore = 0 | |
for score in individualScores { | |
if score > 50 { | |
teamScore += 3 | |
} else { | |
teamScore += 1 | |
} | |
} | |
teamScore | |
var shoppingList: String[] = ["Eggs", "Milk"] | |
var arrayWithVariousTypes = [1, 1, 1, 1, 2, "asdf", shoppingList] | |
var airports: Dictionary<String, String> = ["TYO": "Tokyo", "DUB": "Dublin"] | |
arrayWithVariousTypes.count | |
arrayWithVariousTypes | |
if let airportName = airports["LAX"]{ | |
println("The name of the airport is \(airportName)") | |
} else { | |
print("not found\n") | |
} | |
var dict = [ | |
"a" : 1, | |
"b" : 2, | |
"c" : 3 | |
] | |
"Iterate" | |
for (key, val) in dict { | |
println(key, " is ", val) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment