An Intertech Course Introduction to Swift for iOS Jason Shapiro, Intertech.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
JavaScript, Third Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CS0007: Introduction to Computer Programming Introduction to Arrays.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Programming Languages and Paradigms Object-Oriented Programming.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
May 9, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part I COMP346/ Operating Systems Revision 1.6 February 9, 2004.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 8: Arrays.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Introduction to Building Windows 8.1 & Windows Phone Applications.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CS212: Object Oriented Analysis and Design
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Object-Oriented Programming: Inheritance and Polymorphism.
Swift. Introduced in 2014 Replaces Objective-C as “recommended development language” “safer, succinct, readable” Emphasizes type safety.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Swift by Kevin Gamboa Teky Alvarado Hieu Tran Elizabeth Sanchez
C# for C++ Programmers 1.
What, Where and Why Swift
Java Primer 1: Types, Classes and Operators
Object Oriented Programming
JavaScript Syntax and Semantics
Chapter 3 Introduction to Classes, Objects Methods and Strings
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Java Programming Language
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Object-Oriented Programming: Inheritance and Polymorphism
Object Oriented Programming in java
Java Programming Language
Classes, Objects and Methods
Presentation transcript:

An Intertech Course Introduction to Swift for iOS Jason Shapiro, Intertech

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 2 Intertech Training Training through hands-on, real world business examples. Our site, your site, or live online – globally. Agile/Scrum, Citrix, VMware, Oracle, IBM, Microsoft, Java/Open Source, and web and mobile technologies. Consulting  Design and develop software that powers businesses and governments of all sizes.  On-site consulting, outsourcing, and mentoring.  Agile,.NET, Java, SQL Server, mobile development including iPhone and Android platforms and more…. Instructors Who Consult, Consultants Who Teach Our Company  Over 35 awards for growth, innovation and workplace best practices.  99.7% satisfaction score from our consulting and training customers.  Yearly “Best Places to Work” winner in Minnesota.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 3 Welcome Jason S. Shapiro – Intertech Blog: My LinkedIn Profile - Intertech MVP, Senior Instructor, and Software Architect 20+ Years of Professional Software Development and Architecture Experience (Java EE, iOS, Web, Agile, etc.) Master of Science in Software Engineering (2004) Scrum Alliance Certified ScrumMaster

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 4 Welcome About this Presentation: This is a beginner to intermediate level presentation. The assumption is that you are have basic Object Oriented programming experience (any OO language... it does not need to be Objective-C). A few part of the presentation will refer to Objective-C. The final two demos are geared towards those who have experience building iOS apps.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 5 Agenda What is Swift? Swift vs. Objective-C The Swift Programming Language Variables and Constants Strings Collections Control Structures Functions Access Modifiers Optionals Classes Demos Xcode 6’s “Playground” Single View (w/ outlets and actions) Table View Recap Questions

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 6 What is Swift? A brand new programming language for iOS and OS X development. Secretly in development for several years at Apple. Announced at the WWDC in June of Used to write applications for iOS 7+ and OS X Mavericks+ Xcode 6 includes Swift support (templates, “Playground,” etc.) Works with Objective-C It’s possible to add Swift to existing Objective-C programs (and call existing Objective-C libraries). Designed to be succinct. Reasonable defaults are assumed, resulting in much less coding. Syntax often includes the option to be more verbose for those that prefer a more “classic” style of coding.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 7 What is Swift: Objective-C Example

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 8 What is Swift: Swift Example

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 9 Swift vs. Objective C Objective-C is STILL a “first class language” for iOS development. Swift can work side by side with Objective-C.... or you can ignore Swift altogether and continue to develop iOS 8 / OS X Yosemite apps with Objective-C. Objective-C is a very verbose language. Swift tries to remove code that can reasonably be assumed. For example, in most (though not all) situations, you do not need to include semicolons or parentheses, no need for a main function, or a header file... even, in some cases, internal parameter names! Objective-C variables & properties require explicit types declared. Swift infers the type based on the value (though an explicit type may be included).

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 10 Swift vs. Objective C Objective-C has a special syntax for defining properties Swift does not differentiate between a variable and a property. If you make a variable, you’ve got a property! Objective-C expects that you’ll be smart enough to call an init method for a new object. Swift treats init methods like constructors. If you instantiate an object, it calls a matching init method. Objective-C inherits all init methods, which can complicate an instantiation path. If you provide one or init methods, you will not inherit any from your ancestors.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 11 Swift vs. Objective C Objective-C requires primitive/scalar values to be explicitly wrapped before adding to a collection. Swift allows direct addition of primitives/scalar values without any additional code. Objective-C carries over a lot of syntactical “baggage” by being a sub- set of C. Swift uses modern programming syntax... no need to use * for a pointer, etc. Swift includes MANY constructs and features not found in Objective-C Including: Optionals, Deinitializers, Syntax for Designated vs. Convenience Initializers, Generics, Lazy Property Initialization, Most Unicode Characters for identifiers etc.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 12 Swift: Variables and Constants Swift encourages the use of constants (for code safety & performance). If you know a value will not change, use the “let” keyword. If a value may need to change, use “var” instead of “let.” Notice that in both examples, the type (String) is not explicitly defined. i.e. String instructor = “Jason Shapiro” The type is inferred by the value. Regardless, Swift is a “Strongly Typed” language - once defined, instructor can only hold String values.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 13 Swift: Variables and Constants Constant values cannot change, however, if the value is an object, the object’s mutable properties may be modified. When a variable isn’t defined at the time of declaration, a type needs to be included:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 14 Swift: Strings Strings are created by using a standard literal syntax (quotes). If the String is immutable, use “let,” otherwise use “var.” Concatenation of multiple Strings is accomplished with the “+” or the “+=“ operator.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 15 Swift: Strings Simple concatenation (no special formatting) of Strings and Variables/Constants uses a “\(variable)” syntax: If you want to use String format specifiers, the String class offers an init method (constructor) for that very purpose. This involves a topic we haven’t covered yet (Object Instantiation), but is included here for reference:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 16 Swift: Collections An array is created using a square bracket literal syntax. Once again, use “let” for an immutable array, and “var” for a mutable array: Additional elements are added using the += operator:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 17 Swift: Collections Array access is accomplished by traditional subscripting: Subscripting can include ranges:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 18 Swift: Collections Dictionaries also use a square bracket literal syntax: Dictionaries use subscripting to add additional items:... and values are retrieved using the same subscripting syntax:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 19 Swift: Control Structures Conditionals do not require parentheses, but do require curly braces.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 20 Swift: Control Structures Switch statements must be “exhaustive.” If your set of case statements don’t hit every possible value, you are required to add a “default.”

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 21 Swift: Control Structures Multiple case values can be added by using a comma. Multiple case statement can be executed using the “fallthrough” keyword.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 22 Swift: Control Structures Simple For-In loops do not require parentheses: For-In loops have the ability to return tuples (multiple values) Dictionaries return both the key and the value (rather than just the key).

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 23 Swift: Control Structures Other loops look just like C based loops, however the parentheses are omitted.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 24 Swift: Functions Functions look similar to C styled functions with a few key differences: They are declared with the keyword “func.” Method parameters are declared with the same “identifier:type” syntax that variables and constants use. Parameters are treated as constants (unless preceded w/ “var”) Method parameters may declare default values Return values are declared at the end of a function signature.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 25 Swift: Functions Invoking a function that has default values requires that the parameter names are used: If the method left off the default values, the parameter names cannot be used:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 26 Swift: Functions In order to use parameter names in the function invocation (when default values are not defined), you must add “external names” to the function signature:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 27 Swift: Functions Functions have the ability to return multiple values per call (a tuple) Alternatively, the tuple values can be named and accessed with a dot operator:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 28 Swift: Optionals When a method is invoked that could potentially return “no value,” the API author has to decide what should be returned in it’s place. Should it be nil? false? 0? Optionals standardize what is returned when “no value” is appropriate. In all cases, regardless if the return type is a primitive or object type, a “no value” is returned as “nil.” In fact, nil is so special in Swift, that additional requirements are placed on variables. Variables cannot be nil unless they are declared as an optional. Variables that are not optionals MUST be initialized before they are used.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 29 Swift: Optionals In fact, nil is so special in Swift, that additional requirements are placed on variables. Variables cannot be nil unless they are declared as an optional. Variables that are not optionals MUST be initialized before they are used. An optional is defined by adding a question mark:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 30 Swift: Optionals Optional values are set just like normal variable values, however accessing the values requires a little more work: In order to pull the value out of an optional, you need to add the “!” (force unwrapping) operator. Recall that we saw this used when accessing a dictionary value.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 31 Swift: Optionals Similarly, we can access the courseName by using the “!” (force unwrapping) operator: There is, however a problem... although the “!” operator works when the optional is not nil, it throws an exception if it is nil:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 32 Swift: Optionals In order to execute a safe unwrapping, use an optional in a conditional statement: A function can be declared to return an optional by adding a “?” to the signature. If the value is not found, return nil... even if it’s for a primitive.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 33 Swift: Optionals... and to safely call a function that returns an optional: However, one of the goal’s of Swift is to be as succinct as possible. Another syntax is available which tests and automatically unwraps the value if it is not nil.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 34 Swift: Classes Classes are typically made up of state and behavior (though it is possible for a class to contain only one or the other). Unlike Objective-C, only one file is used for a Class... no more header files! There is no need to inherit from a base class like NSObject. State is expressed as either stored or a computed properties. Behavior is expressed as one or more method.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 35 Swift: Classes Here are several example of how a class may be defined:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 36 Swift: Classes An initializer is required for a class, and follow these rules: The syntax looks like a function, but leaves off the “func” keyword and the return value. The name must be init() – however it can be overloaded with different parameter lists. If no inheritance is used for a class, and it contains some state, you must provide at least one initializer explicitly.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 37 Swift: Classes If inheritance is used AND you provide inline initialization for all of your properties, you will inherit your parent’s initializers. If you provide even one explicit initializer, you will not inherit any initializers from your parent. AND you must call a parent initializer as the LAST statement in your explicit initializer.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 38 Swift: Classes The type of initializers we’ve been looking at are known as “designated initializers.” Only designated initializers are allowed (and must) call a parent’s initializer. Classes that have convenience initializers (those that call other initializers in the same class) must be designated as such. If you try to call a super initializer in a convenience initializer, you will get a compile error.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 39 Swift: Classes

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 40 Swift: Classes Instantiating an object The value is the name of the Class, along with the parameter list of one of its initializers. Yes! Initializers are called implicitly as instantiation time! If there are parameters, their names are required.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 41 Swift: Classes Stored Properties: Any variable or constant that is a member of a class is a stored property. Accessed through the dot operator. No instance variable is produced – there is only one way to access a stored property (through the dot operator). Optionals can be used for stored properties as well. Other than optionals, all stored properties must be initialized prior to being used.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 42 Swift: Classes Lazy Stored Properties: If the initialization of a property is expensive, you have the option to declare that it should not be initialized unless (and until) someone accesses it.

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 43 Swift: Classes Computed Properties: Must be a variable (cannot be a constant) Dynamically created when method is invoked (no “backing store”) Define the getter and optionally the setter:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 44 Swift: Classes Methods Methods are just functions that are included as members of the class!

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 45 Demo: Xcode 6’s “Playground”

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 46 Demo: Single View App (IBOutlets & IBActions)

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 47 Demo: Table View

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 48 Recap What is Swift? Swift vs. Objective-C The Swift Programming Language Variables and Constants Strings Collections Control Structures Functions Access Modifiers Optionals Classes Demos Xcode 6’s “Playground” Single View (w/ outlets and actions) Table View

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 49 Suggested Resources Apple’s Swift Blog: The Swift Programming Language: Swift_Programming_Language/ Stackoverflow Q&A on Swift: Intertech Blog – iOS Topics:

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 50 Next Oxygen Blast Webinars Visit our Free Developer Learning on our siteFree Developer Learning Next iOS Development Training (iPhone & iPad) Upcoming Webinars and Training

Introduction to Core Data for iOS Copyright © Intertech, Inc Slide 51 Questions or Feedback? Jason Shapiro – Thank You