Swift for Beginners: Develop and Design (2nd Edition)

Swift for Beginners: Develop and Design (2nd Edition)

Boisy G. Pitre

Language: English

Pages: 336

ISBN: 0134289773

Format: PDF / Kindle (mobi) / ePub


LEARNING A NEW PROGRAMMING LANGUAGE can be daunting. With Swift, Apple has lowered the barrier of entry for developing iOS and OS X apps by giving developers an innovative programming language for Cocoa and Cocoa Touch. Now in its second edition, Swift for Beginners has been updated to accommodate the evolving features of this rapidly adopted language.

If you are new to Swift, this book is for you. If you have never used C, C++, or Objective-C, this book is definitely for you. With this handson guide, you’ll quickly be writing Swift code, using Playgrounds to instantly see the results of your work. Author Boisy G. Pitre gives you a solid grounding in key Swift language concepts—including variables, constants, types, arrays, and dictionaries—before he shows you how to use Swift’s innovative Xcode integrated development environment to create apps for iOS and OS X.

THIS BOOK INCLUDES:

  • Detailed instruction, ample illustrations, and clear examples
  • Best practices from an experienced Mac and iOS developer
  • Emphasis on how to use Xcode, Playgrounds, and the REPL

 COMPANION WEBSITE:

www.peachpit.com/swiftbeginners2 includes additional resources.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

is helpful: Click here to view code image 40> let binaryNumber = 0b110011 binaryNumber: Int = 51 41> let octalNumber = 0o12 octalNumber: Int = 10 42> let hexadecimalNumber = 0x32 hexadecimalNumber: Int = 50 43> The prefix for binary numbers is 0b, for octal numbers it’s 0o, and for hexadecimal numbers it’s 0x. Of course, no prefix implies a decimal number. Scientific Notation An alternate way of representing a number is to provide it in scientific notation. Such notation is useful to express

increasing speed of a car. Click here to view code image // Speed Limit Simulation var speedLimit = 75 var carSpeed = 0 while (carSpeed < 100) { carSpeed++ switch carSpeed { case 0..<20: println("\(carSpeed): You're going really slow") case 20..<30: println("\(carSpeed): Pick up the pace") case 30..<40: println("\(carSpeed): Tap the accelerator") case 40..<50: println("\(carSpeed): Hitting your stride") case 50..<60: println("\(carSpeed): Moving at a good clip") case 60..<70:

variable. This is a great feature of the Swift language; essentially, it’s a “don’t care” symbol that tells Swift to disregard any use of a variable. In this case, we don’t need to use a variable to iterate through the loop, because the loop variable is not used anywhere in the for-in loop. The code on lines 277 through 279 uses the repeat method on the integer literal 5. The println("repeat this string") statement is embedded in the passed closure, which shows the results in the Timeline pane.

liking. You’ll refer to the navigator area quite a bit when working with your project. On the right side of the project window is the utilities area. This area is automatically populated with information about the currently selected file in the navigator area. It’s also a place where you can set information about various resources that compose your application. The debug area rests at the bottom of the project window. It is currently hidden from your view, but we’ll look at it soon. As you

copying the code. Select the AppDelegate.swift file in the Xcode navigator area, and then drag to select lines 44 through 50. Choose Edit > Copy to copy the selected code to the copy buffer. Click line 51 in the editor area, press Return once, and choose Edit > Paste to paste the copied code to that location (Figure 8.14). FIGURE 8.14 The contents of the file after adding the new method Now, you need to do some renaming. Double-click the buttonClicked method name on line 52, and rename it to

Download sample

Download