Prévia do material em texto
<p>Swift Cheat Sheet {cc} Insert a New Entity // This assumes the encapsulating Type has a reference to managed object context let newEntity - inManagedObjectContext: self.managedObjectContext) as! MyEntity // Set properties do try self.managedObjectContext.save() catch ( // Do something in response to error condition Fetch All Entities // This assumes the type has a reference to managed object context let fetchRequest - NSFetchRequest(entityName: "MyEntity") do { let fetchedEntities - try elf.managedObjectContext.executeFetchRequest(fetchReques as! [MyEntity] // Do something with fetchedEntities catch ( // Do something in response to error condition } Class Implementation Methods fund dolt() -> Int { return 0 } class MyClass: OptionalSuperClass, Int { OptionalProtocol1 OptionalProtocol2 { return a } var myProperty:String fund dolt(a:Int, b:Int) -> Int { var return a+b // More properties... } // You only need if subclassing Creating or Using Instance override init() { myProperty = "Random" var a = MyClass() a.myproperty a.dolt() // More methods... a.dolt(1) a.dolt(2, b:3) Control Flow val 5 Enums switch val { case 1: "ran" enum CollisionType: Int { var condition = true // Omits the if condition { case 2: case Player = 1 "name" // upper value } else case Enemy = 2 } default: ) } "here" } var type = CollisionType.Player Declaring Variables String Quick Examples var mutableDouble:Double - 1.0 var personOne = "Bob" mutableDouble = 2.0 var personTwo = "Steve" let constantDouble:Double = 1.0 var combinedString = "\(personOne):Hey, \(personTwo)! // constantDouble 2.0// error How's it going?" var tipString = "725" var - 1.0 var tiplnt = optionalDouble:Double? = nil NSString(string: tipString).intValue optionalDouble 1.0 tipString = "7.25" if let definiteDouble optionalDouble { var tipDouble = definiteDouble ) NSString(string: tipString).doubleValue Array Quick Examples Dictionary Quick Examples var person1 = "Bob" var person2 = "Steve" var dict[String: String] - "Ninja": "Leonardo", var "Sensai": "Master Splinter", "Enemy": "Shredder") dict["Enemy") = "Baddie" array.append("Names") = nil // to delete Ninja for person in array { for (type, character) in dict { print("person: \(person)") print("type: \(type), character: \(character)") } } var Swift Cheat Sheet I Resource by</p>