iphone Application Programming Lab 3: Swift Types and Custom Operator + A02 discussion

Similar documents
iphone Application Programming Lecture 3: Swift Part 2

iphone Application Programming Lecture 3: Swift Part 2

iphone Application Programming Lab 2: MVC and Delegation + A01 discussion

(infinitespeak.wordpress.com) Classes and Structs. Dr. Sarah Abraham

Mobile Application Programming. Swift Classes

iphone Application Programming Lecture 4: Foundation Classes and Design Patterns

SWIFT - PROTOCOLS. Protocols also follow the similar syntax as that of classes, structures, and enumerations:

Mobile Application Programming. Swift Classes

Introduction to Programming Using Java (98-388)

ITP 342 Mobile App Dev. Fundamentals

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

ios Application Development Lecture 5: Protocols, Extensions,TabBar an Scroll Views

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Announcements. Today s Topics

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

COMP-520 GoLite Tutorial

CSE 431S Type Checking. Washington University Spring 2013

Polymorphism COSC346

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Programming I. Course 9 Introduction to programming

Short Notes of CS201

Week. Lecture Topic day (including assignment/test) 1 st 1 st Introduction to Module 1 st. Practical

CS201 - Introduction to Programming Glossary By

Rx in the real world. 1 Rob Ciolli

CSC 581: Mobile App Development Spring 2019

Fundamentals of Programming Languages

Review of the C Programming Language

Assignment I: Concentration

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Enhancing your apps for the next dimension of touch

Object-Oriented Programming

Inheritance. Transitivity

3. Java - Language Constructs I

Review of the C Programming Language for Principles of Operating Systems

What Code Generation Can Do for You. Marc-Antoine Sauvé

C++ (Non for C Programmer) (BT307) 40 Hours

Data Storage. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CGS 2405 Advanced Programming with C++ Course Justification

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

ios Development - Xcode IDE

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

C++ Important Questions with Answers

Classwide Programming 1

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Gerontechnology II. Collecting Smart Phone Sensor Data for Gerontechnology. Using ios

ios Application Development Lecture 2: Seminar and Unit 1

Data Structures and Algorithms

CS 32. Lecture 2: objects good?

Object Oriented Programming

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Tentative Teaching Plan Department of Software Engineering Name of Teacher Dr. Naeem Ahmed Mahoto Course Name Computer Programming

Xcode & Swift: Introduction

CPS 506 Comparative Programming Languages. Programming Language

COMP 202 Java in one week

ITP 342 Mobile App Dev. Fundamentals

CERTIFICATE IN WEB PROGRAMMING

CHAPTER 1: INTRODUCING C# 3

Spring MVC. PA 165, Lecture 8. Martin Kuba

Chapter 5 Object-Oriented Programming

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Object Oriented Programming

View Concepts. iphone Application Programming Lecture 4: User Interface Design. SDK provide many types of Views to show your content

Miscellaneous Topics

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

ITP 342 Advanced Mobile App Dev. Memory

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Get Unique study materials from

Android Application Development using Kotlin

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Operators and Expressions

CS193E Lecture 7. Document-based Applications NSTableView Key-Value Coding

Programming Languages Third Edition. Chapter 7 Basic Semantics

2. Reachability in garbage collection is just an approximation of garbage.

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Mobile Development Lab 3

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Tokens, Expressions and Control Structures

CMSC 330: Organization of Programming Languages

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance

Advanced Notifications

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

ios Application Development Lecture 3: Unit 2

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Basic Types, Variables, Literals, Constants

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

When Swift met classic algorithms and data structures. Caveats & Tips

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Subtyping (Dynamic Polymorphism)

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

ios DeCal : Lecture 2 Structure of ios Applications: MVC and Auto Layout

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Industrial Programming

CS 211 Programming Practicum Fall 2018

Inheritance and Polymorphism

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Transcription:

Lab 3: Swift Types and Custom Operator + A02 discussion Nur Al-huda Hamdan RWTH Aachen University Winter Semester 2015/2016 http://hci.rwth-aachen.de/iphone

Learning Objectives Discuss A02 Another implementation for A02 Concepts: swift types (class, struct, enum), extensions, operators, typealias, access control, closures, first responder, alert view Introduce A03 2

A02 Discussion KVO is another mechanism from object-to-object combination, especially Model Controller The observed object should inherit from NSObject. The observed property should be dynamic The observing object should declare a context variable and override observevalueforkeypath. Don't forget to remove the observer in deinint Device orientation (inherited from UIViewController) Support orientations in overridden function supportedinterfaceorientations React to orientation change in willrotatetointerfaceorientation Swap views (portrait and landscape), or swap view controllers in a navigation controller, or implement constraints manually??? The info.plist and the project settings should be equivalent when configuring your app let tempvalues = (-80...80).map { $0 3

Store and Retrieve Data from NSUserDefaults 4 override func viewdidload() { super.viewdidload() let row = selectedrow() func selectedrow() -> Int { let selectedrow = NSUserDefaults.standardUserDefaults().objectForKey(userDefaultsLastRowKey) as? Int if let _ = selectedrow { return selectedrow else { func pickerview(pickerview: UIPickerView, didselectrow row: Int, incomponent component: Int) { saveselectedrow(row) func saveselectedrow(row: Int) { let defaults = NSUserDefaults.standardUserDefaults() defaults.setinteger(row, forkey: userdefaultslastrowkey) defaults.synchronize()

A02 Reimplemented Functionality, extend to include Kelvin App structure: MVC Conversion algorithm 5

App Structure Root View Model(s) Read input: temperature value, from unit, to unit Convert temperature value (from unit to unit) in value in unit out value out unit Write output: converted temperature value V from a design specification sheet C connect to V and M M? View controller in outlet in outlet out outlet out outlet convert action response action(s) 6

Models We have a temperature of 2 properties: value and unit and a convert function Class or Struct? to encapsulate Temperature We have 3 types of temperature units Enum TemperatureUnit Enums can have functions that operate on their cases 7

Classes Inheritance Initializers initialize all members before calling the parent initializer (2-phase init) Support for de-initializers Provide reference semantics Are (usually) created on the heap class Person { var firstname: String var lastname: String var available = true init(firstname: String, lastname: String) { self.firstname = firstname self.lastname = lastname func marry(other: Person, taketheirname: Bool) { if (taketheirname) { self.lastname = other.lastname self.available = false Good for shared data, large data, or as a resource handle func stringify() -> String { return firstname + " " + lastname + (available? " is still available!" : " is married.") 8

Structs Collection of named properties Can have initializers and methods Provide value semantics Are (usually) created on the stack Can conform to protocols, can have extensions, but no inheritance Good for data aggregation without implicit struct MapPoint: Stringifyable { var longitude: Double var latitude: Double func rhumbdistance(other: MapPoint) -> Double { let dlong = self.longitude - other.longitude let dlat = self.latitude - other.latitude return sqrt(dlong * dlong + dlat * dlat) func stringify() -> String { return "(\(longitude); \(latitude))" sharing 9

Structs vs. Classes Structs Classes short lived objects long lived objects objects that are created often controller and view objects model objects class hierarchies data capsules (represent only their values) objects in the true sense (representing some identity) If unsure, try a struct first; you can change it later 10

Models We have a temperature of 2 properties: value and unit and a convert function Class or Struct? Temperature We have 3 types of temperature units Enum TemperatureUnit Enums can have functions that operate on their cases 11

Enumerations Represent a finite number of states There are two distinct types of enumerations in Swift Raw value enumerations Similar to Java or C enumerations Associated value enumerations Similar to tagged unions (e.g. in Haskell) 12

Raw Value Enumerations Much more powerful than C enumerations Can have methods and initializers, can have extensions and can conform to protocols More flexible than Java enumerations Can be defined over other underlying types (String, Character, all numeric types) enum TrainClass: String, Stringifyable { case S = "S-Bahn" case RB = "Regionalbahn" case RE = "Regional-Express" case IC = "Intercity" case ICE = "Intercity Express" static let allcases = [S, RB, RE, IC, ICE] func ontime() -> Bool { if self ==.S self ==.ICE { return true return false func stringify() -> String { return self.rawvalue 13

Conversion Algorithm a. execute returned function on value b. creates new Temperature this is the value and units struct convert struct convert return to me a conversion function unit is, target unit is enum conversionfunction create a conversion function between these units enum conversionfunction the compound ConversionFunctions operator 14 ConversionFunction operator

Conversion Algorithm We want a function that takes temperature value, converts it to celsius, and then from celsius to target unit In the enum TemperatureUnit typealias ConversionFunction = (Double) -> Double a. execute returned function on value b. creates new Temperature somecase.conversionfunction, which struct convert calls somecase.conversionfunctiontocelsius ConversionFunction enum conversionfunction somecase.conversionfunctionfromcelsius We will make conversionfunction call a custom operator the compound ConversionFunctions targetunitcase.conversionfunctionfromcelsius() currentunitcase.conversionfunctiontocelsius() returns a compound function (type ConversionFunction) operator conversionfunctionfromcelsius(conversionfunctiontocelsius(value)) 15

Conversion Algorithm struct Temperature { let value : Double let unit : TemperatureUnit func convert(totargetunit targetunit: TemperatureUnit) -> Temperature {unit.conversionfunction.. typealias ConversionFunction = (Double) -> Double enum TemperatureUnit : String { case celsius = " C" case fahrenheit = " F" case kelvin = K private func conversionfunctionfromcelsius() -> ConversionFunction { private func conversionfunctiontocelsius() -> ConversionFunction { func conversionfunction(tounit targetunit: TemperatureUnit) -> ConversionFunction { infix operator {associativity right precedence 150 16 func (lhs: ConversionFunction, rhs: ConversionFunction) -> ConversionFunction { return {(value : Double) -> Double in return lhs(rhs(value))

Access Control private entities are available only from within the source file where they are defined internal entities are available to the entire module that includes the definition (e.g. an app or framework target) the default case public entities are intended for use as API, and can be accessed by any file that imports the module, e.g. as a framework used in several of your projects Apply to classes, structures, and enumerations, properties, methods, initializers, and subscripts Global constants, variables, functions, and protocols can be restricted to a certain context 17

Custom Operators Operators can be declared at global scope Can have prefix, infix or postfix modifiers Infix operators have associativity and precedence values infix operator {associativity right precedence 150 func (lhs: ConversionFunction, rhs: ConversionFunction) -> ConversionFunction { return {(value : Double) -> Double in return lhs(rhs(value)) Operators are implemented as functions at global scope Be very conservative when overloading operators! 18

Extensions Similar to Objective-C categories Can extend Structs, Classes, Enumerations Can add functions, computed properties, nested types Can declare protocol conformance extension Temperature : CustomStringConvertible { var description : String { get { return (NSString(format:"%.2d", self.value) as String) + self.unit.rawvalue Cannot override existing functionality Often useful to clean up code structure 19

20

Design Specification Font: Title 2 Font: Subhead y: 1537 Width: 149 Horizontally centred 21

A03 Part 1: Swift types and UI focus Part 2: Undo Part 3: Storage Part 4: Unit tests 22