Swift Flashcards
python: To read a txt file into python, type
file = open(“/words.txt”, “r”)
words = file.read()
file.close()
python: To turn a list of words into a dict with the keys being the unique words and the values being their frequency in the list, type
from collections import Counter
counts = Counter(my_word_list)
swift: UIkit stands for
User Interface Kit
swift: var variableName only needs to be used
upon instantiation.
swift variable names should be written in
camelcase
swift: A constant is
an immutable variable
swift: To create a constant, type
let constantName = “value”
swift: To explicitly set the type of a variable, type
var myVariable: String = “value”
python: To turn a list of words into a dict with the keys being the unique words and the values being their frequency in the list, type
from collections import Counter
word_list = [‘apple’,’egg’,’apple’,’banana’,’egg’,’apple’]
counts = Counter(list1)
xcode: To show the console
click view / assistant editor / show assistant editor
swift: To print to the console, use the command,
print()
swift: To print something on the console on its own line, type
println()
swift: To evaluate a variable that is inside a string, type
println(“My name is (name_var)”)
swift: Interpolation is
evaluating a variable within a string
swift: To add a line break to a string type
\n
swift: To add a tab to a string type
\t
swift: To add an emoji to a string
click edit / emoji and symbols
swift: A double is
a float that can have 15 decimal places.
swift: If you do not specify the type “float” swift will assume a number with a decimal is a
double
swift: Booleans are written
lowercase
swift: To specify the type of a variable as a boolean, type
var my_var: Bool = false
swift: To convert a variable that is an integer into a Double, type
Double(myInt)
swift: When doing math, to control the order of operations, just use
parentheses
swift: When the operators in a math equation have the same priority, the equation is evaluated from
left to right
swift: To add 1 to an integer variable, type
myInt++
swift: To add 1 to an integer variable and assign it to another variable at the same time, you must put the
++ as a prefix. eg. ++myInt
swift: To subtract 1 from an integer variable in a short way, type
myInt–
swift: To evaluate to the opposite of the given boolean value, type
!false
swift: To specify that an array is a list of only strings, type
var myList: [String] = [“String1”, “String2”]
swift: To append items to the end of an array (using +=), type
myArray += [“New item1”, “New item2”]
swift: To return the attribute of an array that is the sum of the items, type
mylist.count
swift: To append a new item to an array, type
myArray.append(“new item”)
swift: To pop the last item of an array, type
myArray.removeLast()
swift: To remove an item from an array by its index, type
myArray.removeAtIndex(2)
swift: To insert a value into an array at index 2, type
myArray.insert(“value”, atIndex: 2)
swift: To create a single line comment in swift, type
//
swift: To create a dictionary, type
myDict = [“key”: “value”, “key”, “value”]
swift: To add a new dictionary item to myDict, type
myDict[“new key”] = “new value”
swift: myDict[“key name”] returns an
optional
swift: To pop/remove a dict item based on its key, type
myDict.removeValueForKey(“key name”)
swift: To make a for loop, type
for item in myArray {
println(item)
}
swift: to create a for loop for a range of 1 to 10, type
for item in 1…10 {
println(item)
}
swift: to create a rang of 1 to 10 using the greater than symbol, type
1..>9
swift: To make a loop that runs once before validating a condition, type
i = 0
do {
print(“string”)
} while i
swift: To create a while loop that increments, type
i = 0
while i
swift: To create a for loop that increments on one line, type
for var i = 0; i
swift: To create an if, else if, else, chained statement, type
if i 3 { print("more") } else { print("equal") }
to create a for loop that prints a string when the item is equal to 3 or 4, using a switch statement, type
for item in myArray { switch item { case 3, 4: print("string") default: print("") } }
swift: the “and”, “or” and “not”, operators are
&& || !
swift: To create a function, type
func functionName() {
}
swift: to define a function with parameters, type
func functionName(param1: Int, param2: String) {
}
swift: To create a function that has a return, you must
specify the return type, e.g.
func functionName() -> int { return 5 }
swift: To label function parameters so they are easily viewable in tab completion, type
func functionName(param1 1param1: Int, param2 param2: String) {
}
swift: When function params are labeled, you must
pass those labels in with the values when you call the function.
swift: When labeling your function parameters, you can succinctly make your param labels the same as the param names by typing
param1
swift: To declare the return of a function as a tuple with a boolean and a string, type
func functionName(param1: Int, param2: String) -> (Bool, String) {
}
swift: To create a tuple, use
parentheses and a comma
swift: To return the first index of a tuple, type
myTuple.0
swift: To unpack a tuple into variable names, type
var (name1, name2) = myTuple
swift: To set names to the indexes of a tuple that return from a function, so they can later be accesed as an attribute, type
func functionName() -> (name1: Bool, name2: String) {
}
swift: To unwrap the value of an optional, type
myOptional!
swift: If you have a function that only has a return if a condition is met, swift will make you
create another return for when the condition is not met.
swift: To be able to use a nil return type for when the required condition in a function is not met, you must
make the return type an optional by typing func functionName() -> String? { return nil }
swift: To safely unwrap an optional return with no chance of it crashing, type
if let myConstant = functionName("param") { myConstant }
swift: an optional can either have
a value or a nil
swift: The safest way to unwrap an optional is to use
an if let statement
swift: To turn a string into an integer, type
myString.toInt()
swift: To change a function call from a string to an Int, type
myFunction(“param1”)?.toInt()
swift: an enum allows you to create
a new type and set the possible values for that type.
swift: To create an enum for the days of the week, type
enum Days {
case Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
swift: One benefit of an enum is that
xcode will yell at you if you spell it wrong.
swift: To choose a member from an enum, type
Enumname.Membername
swift: To access the raw value of an enum member, type
Enumname.Membername.rawValue
swift: To assign raw values to each of the members of an enum, type
enum Days: Int {
case Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, Sunday = 7
}
swift: Adding a raw value to enum members allows you to
perform arithmetic on them, e.g.
Day.Monday.rawValue - Day.Monday.rawValue
swift: If you assign a raw value of 1 to the first member of an enum, swift will automatically
add incremental raw values to the rest of the members.
swift: To create a function with a switch statement in it, type
func functionName(param1: Int) -> String { switch param1 { case 1...5: return "string" case 6...10: return "string" default: return "string" } }
swift: When create a switch statement, you must end the case statements with a
colon
swift: To create an instance of an enum member by its raw value, type
var enumInstance = EnumName(rawValue: value)
swift: var myEnumInstance = EnumName(rawValue: value) returns
an optional, just in case you pass in a rawValue that does not exist.
swift: The two ways to unwrap an optional are
bang or if let
swift: Each raw value for every enum member must be
unique
swift: To give an enum member an associated value that is a string, type
enum TypeName { class MemberName(String) }
let enumInstance = Typename.MemberName(“Set Associated Value”)
swift: To create an type member instance and pass in the associated value, type
var enumInstance = TypeName.MemberName(“Associated value string”)