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 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 = tipString.toInt() var mutableInferredDouble = 1.0 init() { extension Double { myProperty = "Foo" var optionalDouble:Double? = nil init (string: String) { } optionalDouble = 1.0 self = if let definiteDouble = optionalDouble { Double(string.bridgeToObjectiveC().doubl // More methods... definiteDouble eValue) } } } } tipString = "24.99" Methods Int Variable types 1, 2, 500, 10000 var tip = Double(string:tipString)
Float 1.5, 3.14, 578.234 func doIt() -> Int { return 0 Double Bool true, false Array Quick Examples }
String Kermit, Gonzo, Ms. func doIt(a:Int) -> Int { var person1 = "Ray" Piggy return a var person2 = "Brian" ClassName UIView, UIButton, etc } var array:String[] = [person1, person2]
func doIt(a:Int, b:Int) -> Int { array += "Waldo" return a+b Control Flow for person in array { }
println("person: \(person)") var condition = true } if condition { var waldo = array[2] Creating/Using an Instance } else {
var a = MyClass() } Dictionary Quick Examples a.myProperty var val = 5 var dict:Dictionary<String, String> = a.doIt() switch val { ["Frog": "Kermit", "Pig": "Ms. Piggy", a.doIt(1) case 1: "Weirdo": "Gonzo" ] a.doIt(2, b:3) "foo" dict["Weirdo"] = "Felipe" case 2: dict["Frog"] = nil // delete frog "bar" Enums default: for (type, muppet) in dict { println("type: \(type), muppet: "baz" enum CollisionType: Int { \(muppet)") } case Player = 1 } case Enemy = 2 // omits upper value, use ... to include } for i in 0..3 { var type = CollisionType.Player
}
Source: raywenderlich.com. Visit for more iOS resources and tutorials! Version 0.1. Copyright 2014 Ray Wenderlich. All rights reserved.