Swift for iOS Development (1)-About Swift

version

Xcode 11.0
Swift 5.1

Preface

My Swift series of articles mainly record some basic knowledge points and personal experience, which is convenient for future viewing. For more authoritative and comprehensive tutorials, please refer to the following:

  1. Chinese version of the Apple official Swift Tutorial: SwiftGG . The translation project by the numbbbbb initiated and led, has been Apple's official recognition (Translations section) GitHub connection. At The-SWIFT-Programming-Language-in-chinese .
  2. Novice tutorial Swift tutorial .

About Swift

Swift is an open source programming language that supports multiple programming paradigms and compilation. Apple released it at WWDC (Apple Developer Conference) in 2014 for the development of iOS, macOS, watchOS and tvOS applications.
On June 8, 2015, Apple announced at WWDC 2015 that Swift will open source code, including compilers and standard libraries.
Swift can use the same operating environment as Objective-C on macOS and iOS platforms.
Swift uses modern programming models to avoid a large number of common programming errors:

  • Variables are always initialized before use.
  • Check for errors that the array index is out of range.
  • Check if the integer overflows.
  • Optional values ​​ensure that nil values ​​are handled explicitly.
  • The memory is automatically managed.
  • Error handling allows control recovery from unexpected failures.

Swift historical version

See Swift version history .

Some differences between Swift and OC

Here are just a few outlines, and other specific differences will be discussed later when they are encountered in subsequent chapters.

  1. A class of OC is composed of two files, .h and .m, while Swift has only one file.
  2. OC must have a main function main() as the entry point of the program. Swift does not need main(), the code in the global scope will be automatically regarded as the entry point of the program, starting from the first sentence and executed downward.
  3. OC import files use import<> or import "", Swift import libraries use import+ library name, use this project. swift file does not need to import, can be used directly.
  4. You need to add a semicolon ";" at the end of the OC code line (even adding two semicolons will not report an error), but Swift does not need to add a semicolon, you can add a semicolon if you like (adding two semicolons will report an error). If two lines of code are written on the same line, they need to be separated by a semicolon.
  5. The OC printing method is NSLog, while Swift uses print.
  6. Swift's nil is not the same as Objective-C's nil. In Objective-C, nil is a pointer to an object that does not exist. In Swift, nil is not a pointer-it is a definite value used to indicate missing values. Any type of optional state can be set to nil, not just object types.
  7. [Any] of Swift3 can represent any type of value, whether it is a class, enumeration, structure or any other Swift type, this corresponds to the [id] type in OC.
  8. Use let to declare constants and var to declare variables.

Hello, world!

Here is an introduction to Apple’s artifact for learning Swift-playground.
Playground is integrated in Xcode, supports code preview, writes a line of code to produce a line of results (on the right). It is like an interactive document, allowing programmers to use Run the Swift code without compiling and running the application and view the results in real time.
Let's start our first Swift project.

  1. Create a new playground

Create a new playground
2. Select a blank template

Create a blank template
3. Create name and save address

Name and save path
4. Run

Click the "Play" button to run the program

Note: The playground does not have a command + R shortcut key like the Xcode project, but we can manually add shortcut keys to the playground.
Open the computer system preferences >> keyboard >> shortcut keys >> application shortcut keys, click "+" Button, select Xcode for the application, fill in Run Playground in the menu title, and the shortcut keys can be set by yourself, but they cannot be repeated with the existing shortcut keys in Xcode (such as ⌘R, ⌘B, etc.). After the addition is complete, we don’t need to run it every time Click the "Play" button with the mouse.
Playground custom run shortcut keys

Guess you like

Origin blog.csdn.net/u012078168/article/details/103601020