Skip to content

tonyszhang/iOS-PartTime-Swift-Playgrounds-W1D2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Swift Cheat Sheet

Variables

var myNumber = 5  
myNumber = 6 // setting the value of myNumber to now be 6 instead of 5
var myDouble:Double = 5 // explicitly defining  myDouble to be a Double type  

Constants

let myNumber = 5
myNumber = 4 // error, not allowed to mutate myNumber

Types

Int: A whole number ex) 5  
Double: A number with more precision ex) 5.55  
String: A collection of characters inside quotation marks ex) "Hello"
Bool: true or false

Control Flow

var firstNumber = 1
var secondNumber = 1

if firstNumber == secondNumber {

	// write code in here to run if firstNumber is equal to secondNumber

} else {

	// write code in here to run if firstNumber is NOT equal to secondNumber
	
}
var finalNumber = 5

for i in 1...finalNumber {
	print(i)
}

// prints the numbers 1 to 5

Functions and Methods

func greet() {
	print("Hello!")
}

func greet(name:String) {
	print("Hello \(name)")
}

func greet(name:String) -> String{
	return "Hello \(name)"
}

Arrays

var scores = [74, 94, 88]
scores[0] // returns 74
scores[3] // error, nothing in index 3
scores[3] = 99 // sets the value at index 3 to be 99

var scores = [Int]() // initializes a blank array where every element in this array is an Int

Dictionaries

var scores:[String:Int] = ["Dan": 74, "Paul": 94]
scores["Tom"] = 88 //scores now has a value for the key "Tom"

var scores = [String:Int]() // initializes a blank dictionary where the key is of type String, and the value is of type Int

Classes

class ClassName : ParentClass {

	let aVariable = 0
	
	func aFunction() -> String {
		returns "A String"
	}
	

}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%