Mishra Abhishek

Swift iOS 24-Hour Trainer


Скачать книгу

applications is covered in Lessons 29 and 30.

Screenshot of iOS Simulator Custom Location Dialog Box.

Figure 2.23

      The simulator can also simulate changing locations. This is particularly useful if your app is designed to be used while on the move. From the Debug arrow Location menu, you can select from a list of prerecorded location sets. The simulator will then periodically cycle between the locations in the selected set. The sets are:

      • Apple Stores

      • City Bicycle Ride

      • City Run

      • Freeway Drive

      Installing and Uninstalling Applications

      To install an application to the iOS Simulator, you need to open its corresponding .xcodeproj file in Xcode and click the Run button in the Xcode toolbar.

      You cannot delete the default iOS Simulator applications (such as Photos, Settings, Game Center, Safari, and so on). To uninstall (delete) one of your applications from the iOS Simulator, click and hold the mouse button down on the icon of the app until all the icons start to wiggle. Once they start to wiggle, you will notice an X button on the top-left corner of each icon.

      Release the mouse button if you are still holding it down; the icons will still continue to wiggle. Click the X button on the icon of the app you want to delete. An alert window will appear asking you to confirm this action.

      Limitations of the iOS Simulator

      As good as the iOS Simulator may be, it has its limitations. For starters, you cannot make calls, send or receive text messages, or install apps from the App Store.

      The performance of the iOS Simulator depends on the speed of your Mac, and in certain cases your application may appear to execute much faster on your Mac (in the iOS Simulator) than it does on the real device.

      Accelerometer, camera, and microphone functions are not supported in the iOS Simulator. If you are developing OpenGL/ES-based applications, you should keep in mind that several OpenGL/ES functions are not supported on the iOS Simulator.

      The iOS Simulator is a useful tool to test your apps but it is definitely not a replacement for testing on a real device.

      Try It

      In this Try It, you launch Xcode and open the project that you created in the Try It for Lesson 1. This project was built using the Single View Application template. Once the project is opened in Xcode, you will open a file in the editor area and display the assistant editor, debugger, and utilities areas.

      Lesson Requirements

      • Launch Xcode.

      • Create a new project using a template.

      • Open a file in the editor area.

      • Show the assistant editor.

      • Show the debug area.

      • Show the utilities area.

      Hints

      This Try It builds on the HelloSwift project you created at the end of Lesson 1.

      Step-by-Step

      1. Open the HelloSwift project you created at the end of Lesson 1 by double-clicking the HelloSwift.xcodeproj file in the finder.

      2. Open the AppDelegate.swift file in the Xcode editor. Ensure the project navigator is visible and the iOSTest project is open.

      3. Show the assistant editor using the editor selector buttons on the Xcode toolbar.

      4. Show the debug area using the view selector buttons on the Xcode toolbar.

      5. Show the utilities area using the view selector buttons on the Xcode toolbar.

      REFERENCE

      To see some of the examples from this lesson, watch the Lesson 2 video online at www.wrox.com/go/swiftiosvid.

      Lesson 3

      Introducing Swift

      Prior to the launch of iOS8, Objective-C was the official language used to make native applications. With the launch of iOS 8, Apple provided an alternative language called Swift. Now it is possible to code iOS (and Mac OSX) applications in both Objective-C and Swift. This book targets Swift 2.0, which is supported on iOS 9 and later. This lesson introduces some of the basic concepts of Swift.

      Introducing Xcode Playgrounds

      Playgrounds are a new feature of Xcode (available from versions 6 and above) that allow you to rapidly prototype Swift code. You cannot create a complete app in a playground, but if you want to quickly try out an algorithm or just want to get a feel for the Swift programming language, then playgrounds are for you.

To create a playground, you can either select the Get started with a playground option in the Xcode welcome screen (Figure 3.1), or select the File arrow New arrow Playground menu item.

Screenshot of Xcode welcome screen with Get started with a playground option encircled.

Figure 3.1

Xcode will then ask you to provide a name for the playground as well as the platform. In this book, only iOS playgrounds are explored (Figure 3.2).

Screenshot of Choose options for your new plaground dialog box and Name option filled in with Playground1 and Platform set to iOS .

Figure 3.2

      Xcode will then prompt you to provide a location where the playground should be saved on your hard disk. You can, of course, use any location of your choice.

The main playground screen is divided into two parts (Figure 3.3).

      • Editor area: This forms the left-hand side of the playground screen and is where you type your Swift statements. Every time you press Enter on your keyboard to type a new line, the playground will try to execute the line you have just finished.

      • Results area: This forms the right-hand side of the playground and is where results are displayed. When the playground executes a line of Swift code, it will try and put the result in the same vertical position as the line of code that was executed.

Screenshot of main playground screen in Xcode with sample code for “Hello World!” on the left and result executed on the right.

Figure 3.3

      If the Swift code you have typed in the editor area contains print statements, then the output of these statements will be visible in the console. To display the console in a playground, use the View arrow Debug Area arrow Activate Console menu item.

      Constants and Variables

      The let keyword is used to create a constant. A constant is a quantity whose value cannot change once it is assigned. The following statement creates a constant called maximumScore with a value of 200: