📜 ⬆️ ⬇️

The book "Swift. Basics of developing applications for iOS and macOS. 4th ed. supplemented and revised "

image This book contains comprehensive information for everyone who wants to learn how to program in the wonderful Swift language in order to create their own iOS applications (including for macOS, tvOS and watchOS) or programs for the Linux operating system. In the course of reading the book, you will find not only theoretical information, but also a large number of practical examples and tasks, performing which you will deepen your knowledge of the material being studied.

In the course of a long and fruitful communication with many of you, a lot of ideas were developed, thanks to which the new edition became really useful. In comparison with the previous edition, this book contains the following changes and additions:

- All material is updated in accordance with Swift version 4.1 and Xcode 9.
- Added a large number of new educational material, in particular, related to the practical development of applications for iOS.
- Improved chapter on String data type.
- Considered the wishes and comments of users on the design and content.
- Fixed typos found. Highlighted material intended for novice programmers in separate blocks to allow readers with experience in developing other languages ​​not to be distracted by the material they do not need.

Book structure


The book consists of five large parts and one application:
')
Part I. Preparing for the development of Swift-applications . In the first part, you will begin your journey into the world of Swift, follow the most important and necessary steps before starting to develop your own applications. You will learn how to create your own Apple ID account, how to connect to the apple developers program, where to get the Swift development environment, how to work with it.

Part II. Basic Swift features . After becoming familiar with the Xcode development environment, which allows you to begin learning a programming language, you will learn the basic features of Swift. You will learn what Swift syntax has, what are variables and constants, what data types exist and how to use all this when developing programs.

Part III. Fixed assets Swift . The third part focuses on the consideration and study of the most simple, but very interesting means of Swift. You may not have heard of some of them (for example, tuples), others (for example, arrays) you probably used in other languages.

Part IV. Nontrivial Swift features . The fourth part describes in detail the techniques for working with the most powerful and functional tools Swift. You will use the material of this part with enviable regularity when creating your own applications in the future. Also, a distinctive feature of this part is a lot of practical work on creating the first interactive application.

Part V. Basics of application development . At the end of a long and exciting way of learning the language and creating some simple applications in the Xcode Playground, you will plunge into the world of developing full-fledged programs. From this part you will learn the basics of creating interfaces and running programs in Xcode under the hood. All this in the future will allow you to successfully master new material and create wonderful projects.

Application. Changes and innovations Swift 4.0 . If you studied any of the previous versions of Swift, then the information provided in this application will allow you to quickly get acquainted with all the innovations and changes that have brought a new version of the programming language.

Excerpt from a book. 29.3. Type Restrictions


Sometimes it is useful to specify certain constraints on the data types of the universal template. As an example, we have already considered the Dictionary data type, where there is a requirement for a key: the data type must comply with the Hashable protocol.

Universal templates allow you to impose certain requirements and restrictions on the data type of a value. You can specify a list of types with which the value type must match. If the element of this list is a protocol (which is also a data type), then the compliance of the value type with this protocol is checked; if the type is a class, structure, or enumeration, then it is checked whether the type of the value corresponds to this type.

To define constraints, you must pass a list of type names separated by a colon after the placeholder type name. We implement a function that searches for an element in an array and returns its index (Listing 29.7).

NOTE To provide the functionality of comparing two values ​​in Swift, there is a special protocol Equatable. It requires the data type supporting it to implement a functional of comparing two values ​​using the equality (==) and inequality (! =) Operators. In other words, if the data type supports this protocol, then its values ​​can be compared with each other.

Listing 29.7

1  func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? { 2      for (index, value) in array.enumerated() { 3          if value == valueToFind { 4              return index 5          } 6      } 7      return nil 8  } 9  var myArray = [3.14159, 0.1, 0.25] 10  let firstIndex = findIndex(array: myArray, valueToFind: 0.1) // 1 11  let secondIndex = findIndex(array: myArray, valueToFind: 31) // nil 

The type parameter is written as <T: Equatable>. This means "any type that supports the Equatable protocol." As a result, the search in the transferred array is performed without errors, since the data type Int supports the Equatable protocol, therefore values ​​of this type can be accepted for processing.

29.4. Generic Extensions


Swift allows you to extend the described universal types. In this case, the names of the placeholders used in the type description can also be specified in the extension.
Let us extend the previously described universal type Stack by adding a computed property to it that returns the top element of the stack without deleting it (Listing 29.8).

Listing 29.8

 1  extension Stack { 2      var topItem: T? { 3          return items.isEmpty ? nil : items[items.count1] 4      } 5  } 

The topItem property uses the type name T placeholder to specify the type of the property. This property is optional, as the value in the stack may be missing. In this case, it returns nil.

29.5. Related types


When defining a protocol, it is convenient to use associated types that indicate some, as yet unknown, data type. The associated type allows you to specify a data type placeholder that will be used when filling the protocol. In fact, the data type is not indicated until the protocol is accepted by any object type. Linked types are specified using the associatedtype keyword, followed by the name of the associated type.

Define the Container protocol using the associated type ItemType (Listing 29.9).

Listing 29.9

 1  protocol Container { 2      associatedtype  ItemType 3      mutating func append(item: ItemType) 4      var count: Int { get } 5      subscript(i: Int) -> ItemType { get } 6  } 

The Container protocol can be used in various collections, such as the Stack collection type described earlier. In this case, the data type used in the properties and methods of the protocol is not known in advance.

To solve the problem, the associated type ItemType is used, which is determined only when the protocol is accepted by the data type. An example of adopting a protocol for execution by the Stack data type is shown in Listing 29.10.

Listing 29.10

 1  struct Stack<T>: Container { 2      typealias ItemType = T 3      var items = [T]() 4      var count: Int { 5          return items.count 6      } 7      init(){} 8      init(_ elements: T...){ 9          self.items = elements 10      } 11      subscript(i: Int) -> T { 12          return items[i] 13      } 14      mutating func push(item: T) { 15          items.append(item) 16      } 17      mutating func pop() -> T { 18          return items.removeLast() 19      } 20      mutating func append(item: T) { 21          items.append(item) 22      } 23  } 

Since the Stack type now supports the Container protocol, three new elements have appeared in it: a property, a method, and a subscript. The typealias keyword indicates which data type is bound in a given object type.

NOTE Please note that in the description of the protocol the keyword associatedtype is used, and in the description of the structure, typealias is used.

Since the name placeholder is used as the item type of the append property and the subscript return value, Swift can independently determine that the placeholder T points to the ItemType type corresponding to the data type in the Container protocol. In this case, it is not necessary to specify the associatedtype keyword: if you delete it, the type will continue to work without errors.

»More information about the book can be found on the publisher's website.
» Table of Contents
» Excerpt

For Habrozhiteley 20% discount coupon - Swift

Source: https://habr.com/ru/post/358102/


All Articles