/ Published in: Objective C
Notes about reference types
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Variables and CONSTANTS can reference two types of values: Values and Reference types import Cocoa var str = "Hello, playground" /** ** ** ** ** ** ** ** ** ** Value Types 1: They only have a single reference to them. 2: Types of Objects: Structures, Enumerations, Tuples, 2: Assignment to value types results in a copy to the value. ** ** ** ** ** ** ** ** ** **/ /** ** ** ** ** ** ** ** ** ** Reference Types 1: They use a memory management model like objective C 2: Types of Objects: Classe and Functions 3: There are no copies of references types, only associations. 4: Assigning a var or let to a Class or Function only increases the reference count to the value. Assigment DOES NOT MAKE A NEW COPY OF THE VALUE 5: Single inheritance model 6: Supports Polymorphism 7: Supports Classes and Instances 8: Classes & instances can have their own methods 9: Classes & instances can have their own properties ** ** ** ** ** ** ** ** ** **/ class Person { var firstName:String var lastName:String init(firstName:String, lastName:String){ self.firstName = firstName self.lastName = lastName } func saySomething(message:String){ println("\(firstName) \(lastName) says '\(message)'") } class func saySomething(message:String) { println("...and the people all say \(message)") } } //New Instance let person = Person(firstName: "Chris", lastName: "Aiv") person.saySomething("HEllo World") //Call class function Person.saySomething("OH YAY") func printPerson(p:Person){ println("Person is \(p.firstName) \(p.lastName)") } let stooges = ["Larry", "Moe", "Curly"] stooges.filter({ (name:String) -> Bool in return name.hasPrefix("L") }) /** ** ** ** ** ** ** ** ** ** Various ways to call a function ** ** ** ** ** ** ** ** ** **/ //func printperson(person:Person) //func printPerson(person /** ** ** ** ** ** ** ** ** ** Various ways to call a closure ** ** ** ** ** ** ** ** ** **/ //Explicit stooges.filter({(name: String) -> Bool in return name.hasPrefix("L") }) //Shorthand stooges.filter({$0.hasPrefix("L")}) //Last argument in the class stooges.filter() { name -> Bool in name.hasPrefix("L") }