A Few Interesting Things in Apple's Swift Programming Language
Upcoming SlideShare
Loading in...5
×
 

A Few Interesting Things in Apple's Swift Programming Language

on

  • 1,223 views

Learn about Apple's new programming language, Swift.

Learn about Apple's new programming language, Swift.

Statistics

Views

Total Views
1,223
Views on SlideShare
1,007
Embed Views
216

Actions

Likes
1
Downloads
25
Comments
0

7 Embeds 216

http://blog.smartlogicsolutions.com 200
https://twitter.com 8
http://feedly.com 4
http://digg.com 1
http://reader.aol.com 1
http://www.google.com 1
http://localhost 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • “Swift is a type safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code expects a String, you can’t pass it an Int by mistake. <br /> <br /> Because Swift is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.” <br /> <br /> But it also has a feature called Type Inference which means if the compiler can infer the type of a variable from the context of its declaration, you don’t need to explicitly write the type, it will be inferred.
  • Type inference means, that you even if you don’t provide a type annotation, but the var is still strongly typed -- so you can’t clobber it with a different type.
  • Swift provides two collection types: arrays and dictionaries, for storing collections of values. <br /> <br /> Collections are strongly typed, so you can’t insert the wrong value into a collection, and you always know what you’re going to get when you retrieve a value. <br /> <br /> As with other types, the type can be inferred <br />
  • For collections: <br /> an array = can’t change the size, but can’t replace a value at an index <br /> dictionary = can’t change values <br />
  • Values of most types can never be nil. <br /> <br /> Every value must be initialized before it is used. <br /> <br /> So how do you deal with the possibility of the absence of a value? <br /> <br />
  • Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes.” <br /> “Optionals are an example of the fact that Swift is a type safe language. Swift helps you to be clear about the types of values your code can work with.” <br /> <br /> Use optionals to safely work with possibly missing values: <br /> missing values are nil <br /> present values are wrapped in an optional <br />
  • “Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes.” <br /> “Optionals are an example of the fact that Swift is a type safe language. Swift helps you to be clear about the types of values your code can work with.” <br /> <br /> “Swift’s nil is not the same as nil in Objective-C. In Objective-C, nil is a pointer to a non-existent object. In Swift, nil is not a pointer—it is the absence of a value of a certain type. Optionals of any type can be set to nil, not just object types.” <br />
  • Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes.” <br /> “Optionals are an example of the fact that Swift is a type safe language. Swift helps you to be clear about the types of values your code can work with.” <br /> <br /> Use optionals to safely work with possibly missing values: <br /> missing values are nil <br /> present values are wrapped in an optional <br />
  • <br /> var possibleNumber = "2" <br /> if let actualNumber = possibleNumber.toInt() { <br /> println("(possibleNumber) has an integer value of (actualNumber)") <br /> } else { <br /> println("(possibleNumber) could not be converted to an integer") <br /> } <br /> <br />
  • <br /> Say we have a person -- and people in our domain always have names, but they may not have addresses. So we have the name type as a String, but the address type is an optional Address class. We have this instance method (function) on Address called sendLetter, and we want to send a letter to each person who has an address. <br /> <br /> DEMO <br /> <br /> so, if the optional is nil it just swallows the method call. No undefined method for nilclass error.
  • One of the nice things, I think, about Objective C its verbosity. I like the self-documenting nature of named parameters. Swift is a lot more flexible in that it lets you use named parameters or get rid of them all together, or even use different external param names for callers than the names used within the function definition.
  • Enumerations define a type for a group of related values and you can work with those values in a type-safe way. Some cool things about enumerations in Swift, is 1) they don’t need member values, unlike in C, North, South, East, West don’t equal 1, 2, 3, or 4 or whatever. <br /> <br /> Another cool thing is an enumeration can have associated values. So, if you have a Status enum with a status of OnTime and of Delayed -- the Delayed status can have an associated delay of minutes and maybe a string that contains the reason for the delay. <br /> <br />
  • Swift has a really powerful switch statement. It is exhaustive, which means the compiler forces you to check for every possible value for the type that you are checking. Now, if you are switching on integers you don’t have to have a line for ‘n’ to infinity -- you can use the default case.
  • Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages. One of the coolest things about closures is that closures can capture and store references to any constants and variables from the context in which they are defined. This is known as closing over those constants and variables, hence the name “closures”. Ruby has closures in the form of lambdas and procs, so I don’t really need to say much about them in Swift, other than to show the expression syntax for them in Swift. <br />
  • <br /> struct Stack { <br /> var items = T[]() <br /> mutating func push(item: T) { <br /> items.append(item) <br /> } <br /> mutating func pop() -> T { <br /> return items.removeLast() <br /> } <br /> } <br /> <br /> var stack = Stack() <br />

A Few Interesting Things in Apple's Swift Programming Language A Few Interesting Things in Apple's Swift Programming Language Presentation Transcript

  • A Few Interesting Things In Swift Ara Hacopian B’more on Rails 6/10/2014
  • Ara Hacopian http://www.smartlogic.io http://www.twitter.com/ahacop http://www.github.com/ahacop SmartLogic
  • Some Cool Stuff ● Type Inference ● Explicit Mutability ● Optionals ● Named Parameters ● Enumerations ● Exhaustive Switch ● Closures ● Generics
  • Type Inference You can do this: var x : Int = 0 var y : String = “foobar” var z : Bool = true Or, you can just do this: var x = 0 var y = “foobar” var z = true
  • Type Inference var x = 1 x = “quux” ← COMPILER ERROR
  • Typed Collections var mammals: String[] = [“dog”, “cat”] var people = [“dan”, “eric”, “tom”] var numbers = [1, 2, 3] numbers.append(“ara”) ←COMPILER ERROR
  • Explicit Mutability var x = 0 x = 1 x // x == 1 let y = 0 y = 1 ← COMPILER ERROR
  • Never Be Nil var x : Int = 0 x = nil ← COMPILER ERROR
  • Optionals Optionals handle the absence of a value. Either: 1. there is some value, and it equals x var foo : Int? = 41 // { Some 41 } 1. or, there is no value at all var bar : Int? // nil
  • Optional Declaration var intOptional : Int? var stringOptional : String? var boolOptional : Bool?
  • Optionals: Unwrapping You can unwrap the value contained in the optional by using the bang operator, like so: var foo : Int? = 41 foo! == 41 // true But if it’s nil, you will get a runtime error, so don’t do that.
  • Optional Binding var possibleNumber = "2" if let actualNumber = possibleNumber.toInt() { println("(possibleNumber) has an integer value of (actualNumber)") } else { println("(possibleNumber) could not be converted to an integer") }
  • Optional Chaining class Person { var name : String var address : Address? init(name:String, atAddress address:Address?=nil) { self.name = name self.address = address } } class Address { func sendLetter() { println("Sent a letter!") } } let ara = Person(name: "Ara") let paul = Person(name: "Paul", atAddress:Address()) let people = [ara, paul] for person in people { person.address?.sendLetter() }
  • External Param Names func join( string s1: String, toString s2: String, withJoiner joiner: String) -> String { return s1 + joiner + s2 } join(string: "hello", toString: "world", withJoiner: ", ")
  • Enumerations enum Compass { case North, South, West, East } enum Status { case OnTime, Delayed(Int, String) }
  • Exhaustive Switch In Swift, the compiler enforces that a switch statement contains a case for every possible value for that type. enum Status { case OnTime, Delayed(Int, String) } let trainStatus = Status.Delayed(5, “banana on tracks”) switch trainStatus { case .OnTime: println(“Train is on time!”) case .Delayed(let minutes, let reason): println(“Train delay: (minutes) minute(s), Reason: (reason)”)
  • Closures Just like blocks in ObjC...but there’s a lot of optional syntax. let names = ["Paul", "Ara", "Eric", "Tom", "Dan"] func backwards(s1: String, s2: String) -> Bool { return s1 > s2 } 1. sort(names, backwards) 2. sort(names, { (s1: String, s2: String) -> Bool in return s1 > s2 }) 3. sort(names, { s1, s2 in return s1 > s2 } ) 4. sort(names, { s1, s2 in s1 > s2 } ) 5. sort(names, { $0 > $1 } ) 6. sort(names, >)
  • Generics struct IntStack { var items = Int[]() mutating func push(item: Int) { items.append(item) } mutating func pop() -> Int { return items.removeLast() } }
  • Generics struct Stack<T> { var items = T[]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } }
  • Questions? http://www.smartlogic.io/ http://www.twitter.com/ahacop http://www.github.com/ahacop