Swift iOS 24-Hour Trainer. Mishra Abhishek
Чтение книги онлайн.
Читать онлайн книгу Swift iOS 24-Hour Trainer - Mishra Abhishek страница 9
• 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
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).
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.
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
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:
If you are familiar with programming in C or Objective-C, you will immediately notice that Swift statements do not need to end in a semicolon.
A variable quantity is one whose value can change over the life of the application. A variable is defined using the var keyword as follows:
There are a few rules that you must stick to when it comes to naming constants and variables. Constants and variables cannot begin with a number, contain spaces, or contain mathematical symbols. You cannot change a constant into a variable or vice versa.
Data Types
Unlike C or Objective-C, Swift does not require you to specify a data type when you are declaring a constant or variable. The Swift compiler uses type inference to work out the data type from the value you assign. If, however, you wish to be explicit, you can specify the data type of a constant or variable while declaring them as follows:
Once a constant or variable has been created with a certain type, its type cannot be changed. Table 3.1 lists some of the common data types in Swift.
Table 3.1 Common Swift Data Types
Variables in Swift are classed as either value types or reference types depending on how they behave when they are passed as parameters to a method. (A method is a block of code that will be described later).
A value type is a variable whose value is copied when it is passed as a parameter to a method. Any changes made by the method to the variable only apply to its private copy of the original variable and do not affect the value of the original variable in any way.
A reference type, on the other hand, is passed by reference. If the receiving method changes the value of a reference type then the change will be visible outside the scope of the function.
Comments
Comments