Introduction to Swift Programming Language

Slide Note
Embed
Share

Swift is a modern programming language known for its evolution and development since 2010. It is a general-purpose, compiled language that supports both object-oriented and functional programming paradigms. Key features include constants, variables, operators, strings, optionals, and control flows like if-else statements and switch statements.


Uploaded on Sep 12, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Lecture 1 - Intro to Swift Ioannis Pavlidis Dinesh Majeti Ashik Khatri

  2. Swift - Evolution Development on Swift was begun in July 2010 by Chris Lattner, Version 1.0 released on September 9, 2014 Swift 2.0 announced at WWDC 2015; made available for publishing apps in September 21, 2015. Swift made open source on December 3, 2015 A Swift 3.0 is expected to be released in Fall 2016

  3. Swift - Overview General-purpose, multi-paradigm, compiled programming language. Object-oriented language with functional concepts. Typing discipline: Static, Strong, Inferred. File names end with .swift

  4. Constants and Variables Constants and Variables let maximumNumberOfLoginAttempts = 10 var currentLoginAttempt = 0 let myDoubleArray = [2.1, 3.5, 1.1] Type Annotations var welcomeMessage: String

  5. Operators Supports all standard Arithmetic, Comparison, Logical, Bitwise, Assignment, Range (0..<3) Operators Supports Ternary Conditional ( Condition ? X : Y ). Unary Plus and Minus

  6. Strings String Interpolation let multiplier = 3 let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)" // message is 3 times 2.5 is 7.5 Can be applied to print statements String Concatenation let str = "Hello" + " swift lovers" // "Hello swift lovers" Methods on string print(str.characters.count) // 18

  7. Optionals? You use optionals in situations where a value may be absent. An optional says: There is a value, and it equals x OR There isn t a value at all Example: var serverResponseCode: Int? = 404 Optional variable can be set to nil (absence of a value of a certain type. Not a pointer) Example: serverResponseCode = nil nil cannot be used with non-optional constants and variables Optional variable without providing a default value: var surveyAnswer: String? // surveyAnswer is automatically set to nil

  8. Control Flows If-else statements if x >= y { print( x is greater or equal to y ) } else { print( y is greater ) } Supports switch statements

  9. Control Flows - unwrapping optionals let possibleNumber = "123" // possibleNumber is type String let convertedNumber = Int(possibleNumber) // convertedNumber is type Int? if convertedNumber != nil { // now that we know that convertedNumber has a value, force unwrap using ! print("convertedNumber has integer value of \(convertedNumber!).") }

  10. Control Flows - Optional Binding - I Use to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. Can be used with if and while statements if let actualNumber = Int(possibleNumber) { print("\"\(possibleNumber)\" has an integer value of \(actualNumber)") } else { print("\"\(possibleNumber)\" could not be converted to an integer") } // Prints "123" has an integer value of 123

  11. Control Flows - Optional Binding - II Can also include multiple optional bindings in a single if statement if let firstNumber = Int("4"), secondNumber = Int("42") where firstNumber < secondNumber { print("\(firstNumber) < \(secondNumber)") } // Prints "4 < 42"

  12. Control Flows - Early Exit A guard statement, like an if statement, executes statements depending on the Boolean value of an expression. Unlike an if statement, a guard statement always has an else clause func foo(possibleNumber : String?) { guard let actualNumber = Int(possibleNumber) else { return } print("Number is: \(actualNumber)") }

  13. Control Flows - Loops Swift supports for, for-in, while, and do...while loops for var index = 0; index < 3; index += 1 { } for item in someArray { } while index < 20 { } do { } while index < 20 for i in 0..<3 { /* this will loop 3 times */ }

  14. Functions - I func sayHello(personName: String) -> String { return "Hello, " + personName + "!" } print(sayHello("Bob")) // Prints "Hello, Bob! Supports nested functions Create function inside a function!

  15. Functions - II Functions with Multiple Return Values func minMax(array: [Int]) -> (min: Int, max: Int) { var currentMin = array[0] var currentMax = array[0] // some logic ... return (currentMin, currentMax) } let bounds = minMax([8, -6, 2, 109, 3, 71]) print("min is \(bounds.min) and max is \(bounds.max)")

  16. Closures - I Closures are self-contained blocks of functionality that can be passed around and used in your code. { (<parameters>) -> <return type> in <statements> } Functions are actually special cases of closures. global functions they have a name and cannot capture any values nested functions they have a name and can capture values from their enclosing functions closure expressions they don t have a name and can capture values from their context

  17. Closures - II Example: let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] let sorted = names.sort() // ["Alex", "Barry", "Chris", "Daniella", "Ewa"] let reversed1 = names.sort({ (s1: String, s2: String) -> Bool in return s2 > s2 }) // ["Ewa", "Daniella", "Chris", "Barry", "Alex"] let reversed2 = names.sort({s1, s2 in s1 > s2}) // ["Ewa", "Daniella", "Chris", "Barry", "Alex"] let reversed3 = names.sort(>) // ["Ewa", "Daniella", "Chris", "Barry", "Alex"]

  18. Classes class Person { var name: String var gender = Male var age: Int? init(name: String) { self.name = name } // other functions and variables } let bob = Person(name: Bob ) bob.age = 18

  19. References Swift 3 Programming Language Guide Swift 2 Programming Language Guide Swift Blog Online Swift REPL

Related