[go: up one dir, main page]

0% found this document useful (0 votes)
222 views1 page

Swift CheatSheet

The document provides a cheat sheet and quick reference for Swift 3.0. It includes examples and syntax for classes, variables, methods, control flow, arrays, dictionaries, enums and more. The document is intended as a reference for common Swift concepts and code.

Uploaded by

Hamid Jahangir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views1 page

Swift CheatSheet

The document provides a cheat sheet and quick reference for Swift 3.0. It includes examples and syntax for classes, variables, methods, control flow, arrays, dictionaries, enums and more. The document is intended as a reference for common Swift concepts and code.

Uploaded by

Hamid Jahangir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Swift 3.

0 Cheat Sheet and Quick Reference


Class Implementation Declaring Variables String Quick Examples

class MyClass : OptionalSuperClass, var mutableDouble:Double = 1.0 var personOne = "Ray"
OptionalProtocol1, OptionalProtocol2 { mutableDouble = 2.0 var personTwo = "Brian"
var combinedString = "\(personOne):
var myProperty:String let constantDouble:Double = 1.0 Hello, \(personTwo)!"
var myOptionalProperty:String? // constantDouble = 2.0 // error var tipString = "2499"
// More properties... var tipInt = Int(tipString)
var mutableInferredDouble = 1.0
// Only need override if subclassing tipString = "24.99"
override init() { var optionalDouble:Double? = nil var tipDouble = Double(tipString)
myProperty = "Foo" optionalDouble = 1.0
} if let definiteDouble = optionalDouble {
definiteDouble
Array Quick Examples
// More methods... }
} var person1 = "Ray"
var person2 = "Brian"
Variable types
var array:[String] = [person1, person2]
Methods Int
Float
1, 2, 500, 10000
1.5, 3.14, 578.234
array.append("Waldo")
for person in array {
Double
func doIt() -> Int { print("person: \(person)")
Bool true, false
return 0 }
String Kermit, Gonzo, Ms.
} var waldo = array[2]
Piggy
func doIt(a:Int) -> Int { ClassName UIView, UIButton, etc
return a
}
Dictionary Quick Examples
func doIt(a:Int, b:Int) -> Int { Control Flow var dict:[String: String] = ["Frog":
return a+b
} "Kermit", "Pig": "Ms. Piggy",
var condition = true
"Weirdo": "Gonzo" ]
if condition {
dict["Weirdo"] = "Felipe"
} else {
dict["Frog"] = nil // delete frog
Creating/Using an Instance }
for (type, muppet) in dict {
print("type: \(type), muppet:
var a = MyClass() var val = 5
\(muppet)")
a.myProperty switch val {
}
a.doIt() case 1:
a.doIt(a:1) "foo"
a.doIt(a:2, b:3) case 2:
"bar"
default:
Enums "baz"
}
enum CollisionType: Int {
case player = 1 // omits upper value, use ... to include
case enemy = 2 for i in 0..<3 {
} }
var type = CollisionType.player

Source: raywenderlich.com. Visit for more iOS resources and tutorials! Version 0.7. Copyright 2016 Ray Wenderlich. All rights reserved.

You might also like