Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift 2 Blueprints

You're reading from   Swift 2 Blueprints Swift Blueprints

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher Packt
ISBN-13 9781783980765
Length 276 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Cecil Costa Cecil Costa
Author Profile Icon Cecil Costa
Cecil Costa
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Exploring Xcode FREE CHAPTER 2. Creating a City Information App with Customized Table Views 3. Creating a Photo Sharing App 4. Simulating Home Automation with HomeKit 5. Health Analyzing App Using HealthKit 6. Creating a Game App Using SpriteKit 7. Creating an Apple Watch App 8. AVFoundation Index

New Swift features

Swift is a new language, but it has been changed a few times. While this book was being written, Swift had passed through the 1.2 and 2.0 versions. These versions have some syntax differences compared to the previous ones.

Before the Swift 1.2 version, you couldn't use more than one let statement on an if statement, so you probably had to write codes like the following one:

    if let name = product.name {
      if let category = product.category{
        if let price = product.price {
          if price >= 0 {
            println("The product is valid")
          }
        }
      }
    }

Now, you can reduce the number of lines and write a code like the next one:

    if let name = product.name, category = product.category, price = product.price where price >= 0 {
      println("The product is valid")
    }

Constants can be initialized after their declaration and before using them, like the following code:

    let discount:Double
    if onSale {
      discount = 0.2
    }else {
      discount = 0
    }

Downcasts and casts that don't return options must use the as! operator instead of the old as operator. It means that the casting from typical functions that return AnyObject must use the new operator. For example, the call to retrieve the index paths of a table view will be as follows:

var paths = tableView.indexPathsForRowsInRect(rect) as! [NSIndexPath]

If you have a huge project done with the first Swift version, you might be scared about the number of errors you have for small things like using the new as! operator. Fortunately, there is an option to update our Swift code in the Edit menu in order to convert our code to the latest Swift version syntax, as it is demonstrated in the following screenshot:

New Swift features

When Swift was announced, it brought with itself new data types that replaced the equivalent Objective-C containers, such as Array for NSArray, String to replace NSString, and Dictionary for NSDictionary, but there was a missing type: NSSet. Now, there is Set that follows the same rule as its colleagues: it must specify the data type that it will store (generics) and it is interchangeable with NSSet.

A new attribute was introduced in the Swift language, it is called final. If you have a final class, no class can inherit from it. If you have an attribute or a method, you can't override it.

You have been reading a chapter from
Swift 2 Blueprints
Published in: Oct 2015
Publisher: Packt
ISBN-13: 9781783980765
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime