Upload
intertech-training
View
535
Download
4
Tags:
Embed Size (px)
DESCRIPTION
Over the past few years, a significant segment of developers have voiced their frustration with Apple's decision to use Objective-C for iOS programming. Complaints often stated that the language is too complex, verbose, and fragile to be used in rapid software development. It turns out Apple had not only been listening to these complaints, they've been actively and secretly addressing them! With the upcoming release of iOS 8 and Xcode 6 developers will have the option to work with a brand new programming language: Swift! In this webinar, Jason Shapiro will discuss: * What is Swift? * The Syntax of the Swift Programming Language * Objective-C vs. Swift * Criticism and Concerns About Swift * The Impact of Swift on Existing Objective-C Apps.
Citation preview
An Intertech Course
Introduction to Swift for iOS
Jason Shapiro, Intertech
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 3
Welcome• Jason S. Shapiro – [email protected]
• Intertech Blog: http://www.intertech.com/blog• My LinkedIn Profile - http://linkedin.com/in/jshapiro
• 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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 2014.• 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. • www.Intertech.com • 800-866-9884 Slide 7
What is Swift: Objective-C Example
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 8
What is Swift: Swift Example
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 10
Swift vs. Objective C
• Objective-C has a special syntax for defining properties (@property)• 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 17
Swift: Collections
• Array access is accomplished by traditional subscripting:
• Subscripting can include ranges:
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 19
Swift: Control Structures
• Conditionals do not require parentheses, but do require curly braces.
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 35
Swift: Classes
• Here are several example of how a class may be defined:
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 39
Swift: Classes
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 45
Demo: Xcode 6’s “Playground”
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 46
Demo: Single View App (IBOutlets & IBActions)
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 47
Demo: Table View
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 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. • www.Intertech.com • 800-866-9884 Slide 49
Suggested Resources• Apple’s Swift Blog:
https://developer.apple.com/swift/blog/
• The Swift Programming Language: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
• Stackoverflow Q&A on Swift: http://stackoverflow.com/questions/tagged/swift
• Intertech Blog – iOS Topics:http://www.intertech.com/Blog/category/ios-ipad-iphone/
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 50
Next Oxygen Blast Webinars• Visit our Free Developer Learning on our site
Next iOS Development Training (iPhone & iPad)• http://www.intertech.com/Training/Mobile/iOS/iOS
Upcoming Webinars and Training
Introduction to Core Data for iOS
Copyright © Intertech, Inc. • www.Intertech.com • 800-866-9884 Slide 51
Questions or Feedback?Jason Shapiro – [email protected]
Thank You