Lecture 1 - Intro to Swift

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Programming Languages and Paradigms The C Programming Language.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Chapter 4 Making Decisions
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Ruby! Useful as a scripting language – script: A small program meant for one time use – Targeted towards small to medium size projects Use by: – Amazon,
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Swift. Introduced in 2014 Replaces Objective-C as “recommended development language” “safer, succinct, readable” Emphasizes type safety.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Information and Computer Sciences University of Hawaii, Manoa
G. Pullaiah College of Engineering and Technology
Branching statements.
Chapter 10 Programming Fundamentals with JavaScript
Java Language Basics.
The Machine Model Memory
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 3 Control Statements
Introduction to Apple mobile technologies- I393
Selections Java.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Python: Control Structures
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Programming Languages and Paradigms
Programming for Mobile Technologies
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Paradigms
Chapter 8: Control Structures
Control Structures – Selection
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
Statements, Comments & Simple Arithmetic
Chapter 10 Programming Fundamentals with JavaScript
Starting JavaProgramming
Introduction to Programming
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
PHP.
CMSC 202 Java Primer 2.
University of Kurdistan
Introduction to Programming Using Python PART 2
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Statement-Level Control Structures
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Intro to Programming Concepts
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Fundamental Programming
Decisions, decisions, decisions
PHP an introduction.
Programming Languages and Paradigms
CprE 185: Intro to Problem Solving (using C)
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
CSC 581: Mobile App Development
Selection Control Structure
Controlling Program Flow
Presentation transcript:

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

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

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

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

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

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

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

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

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!).") }

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

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"

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)")

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 */ }

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

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)")

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

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"]

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

References Swift 3 Programming Language Guide Swift Blog Online Swift REPL