Getting started with iOS

Getting started with iOS

Swift VS Objective-C

image

Programming paradigm

  • Swift can be protocol-oriented programming, functional programming, object-oriented programming.

  • Objective-C is mainly based on object-oriented programming, of course, you can also introduce a class library similar to ReactiveCocoa for functional programming.

Type safety

  • Swift is a type-safe language. Programmers are encouraged to be clear about the type of value in the code. If a string is used in the code, then you cannot mistakenly pass an integer Int to it. Because Swift is type-safe, it does type checking when the code is compiled, and flags any mismatched types as an error. This allows programmers to discover and correct errors as early as possible in development.

  • Objective-C does not. If you declare an NSString variable, you can still pass an NSNumber to it. Although the compiler will complain, you can still use it as an NSNumber.

Language characteristics

  • Swift syntax has a more concise way of defining classes, extensions and properties. Headers are dropped throughout the project, and the introduction of header files is more modern.

  • Objective-C is more cumbersome to define classes and methods.

Development and application

image

  • Since 2014, Swift has released 15 versions, including 5 major versions and more than 10 minor versions. The current 5.x version has just been basically stable, and there are few third-party library sources.

  • Swift was officially open-sourced in December 2015. Currently, Swift can be applied to many fields, including server-side development and TensorFlow (based on data flow programming, which is used to program various machine learning algorithms). There is also a Swift language version

  • Objective-C has only two versions from the 1980s to the present, with mature development and rich three-party library sources, mainly used in the development of apple ecological applications.

Choice

With a combination of factors, for the introduction of iOS development, choosing Swift to start is more friendly and more future-proof, but to deeply master iOS development, Objective-C is also an essential foundation.

Swift basic introduction

Variables and constants

  • statement

    • Use the keyword let to declare constants

    • Use the keyword var to declare variables

    • Multiple variables or constants can be declared on one line, separated by commas

image

image

  • Type annotation

    • Provide a type annotation when declaring a variable or constant to clarify the type of value that the variable or constant can store

    • The method of adding type annotations is to add a colon after the name of the variable or constant, followed by a space, and finally add the type name to be used

    • You can define multiple related variables of the same type in a line, separated by commas, as long as the type tag is added after the last variable name

image

  • Variable and constant naming

    • Constant and variable names can use almost any character, even Unicode characters

    • Constant and variable names cannot contain blank characters, mathematical symbols, arrows, reserved (or invalid) Unicode code points, lines, and tabs. It cannot start with a number, although the number can be used almost anywhere else in the name

image

  • Print constants and variables

    • print(_:separator:terminator:)

    • String interpolation

image

Basic data types

  • Integer

    • Swift provides signed, unsigned integers in 8, 16, 32 and 64 bits

    • Naming method: for example, the type of 8-bit unsigned integer is UInt8, and the type of 32-bit signed integer is Int32. The min and max attributes are used to access the minimum and maximum values ​​of each integer type

    • Swift provides an additional integer type: Int, which has the same length as the native word of the current platform. Swift also provides a UInt type to represent unsigned integers related to platform length. It is recommended to use Int wherever integers are used

  • Floating point type

    • Double: 64-bit floating point number with at least 15 digits of precision

    • Float: 32-bit floating point number, with a precision of at least 6 digits

    • In the case where both types are possible, the Double type is recommended.

image

  • Bool

    • Bool: true and false, Swift's type safety mechanism will prevent you from replacing Bool with a non-Boolean value

image

Type alias

  • Type alias is an alternative name defined for an existing type

  • You can define a type alias by keyword typealias

  • Aliases are very useful when you want to refer to an existing type by a name that looks more appropriate in the context and can be expressed

image

Tuple tuple

  • Tuple combines multiple values ​​into a single compound value

  • The values ​​in the tuple can be of any type, and need not be of the same type

image

  • Element naming

    • Each element in the tuple can specify the corresponding element name

    • If there is no element with a specified name, you can also use the subscript method to quote

image

  • Tuple modification

    • The tuple defined by var is a variable tuple, and let defines an immutable tuple

    • Whether it is a variable or immutable tuple, the tuple cannot add and delete elements after it is created

    • The elements of variable tuples can be modified, but their types cannot be changed

    • any type can be changed to any type

image

  • Tuple decomposition

    • To decompose the contents of a tuple into individual constants or variables

    • If only a part of the data needs to be used, the unneeded data can be replaced with a downward line (_)

image

  • As function return value

    • Use Tuple to return multiple values ​​for a function

    • The Tuple of the return value can be named in the return type part of the function

image

Optional

  • By adding? After the variable type, it means:

    • There is a value here, which is equal to x or

    • There is no value here

  • You can set an optional variable to no value by assigning it a nil

    • In Swift, nil is not a pointer, it is a special type with missing values, any type of option can be set to nil and not just the object type

image

  • Optional-If statement and forced expansion

    • The option cannot be used directly. It needs to be used after being expanded (meaning that it is clearly known that there is a value in this option and needs to be expanded)

image

  • Optional-force expansion

    • Using! To obtain a non-existent optional value will cause a run-time error. Before using! To force expansion, you must ensure that the optional item contains a non-nil value.

image

  • Optional-binding

    • You can use option binding to determine whether the option contains a value, and if so, assign the value to a temporary constant or variable

    • Optional binding can be used with if and while statements to check the value inside the option and assign it to a variable or constant

    • The same if statement contains multiple option bindings, separated by commas. If any optional binding result is nil or boolean value is false, then the entire if judgment will be regarded as false

image

  • Optional-implicit expansion

    • Some options will always have values ​​once they are set, in this case, you can remove the need for inspection, and you do n’t have to expand every time you visit

    • Write implicit expansion options by adding an exclamation point (String!) Instead of a question mark (String?) After the declared type

    • Implicit expansion options are mainly used in the initialization process of Swift classes

image

String

  • initialization

    • Literals: string literals are fixed-order text characters enclosed in double quotes (")

    • Multi-line literals: Multi-line string literals are a series of characters enclosed in three double quotes, using a backslash (\) to wrap

    • Initializer syntax

    • isEmpty checks if it is an empty string

image

image

image

  • Special characters in the string

    • Escape special characters \ 0 (null character), \ (backslash), \ t (horizontal tab), \ n (line feed), \ r (carriage return), "(double quote) and '( apostrophe)
  • String manipulation

    • var can be modified

    • unmodifiable by let

image

  • Operation character

    • for-in loop through each independent Character in String

    • String value can be constructed by passing in Character array

image

  • String concatenation

    • Use the plus operator (+) to create a new string

    • Add a String value to the end of an existing String value using the plus assignment symbol (+ =)

    • Use the append () method of the String type to append the Character value to the end of a String variable

  • String interpolation

    • String interpolation is a method of constructing new String values ​​from string literals that mix constants, variables, literals, and expressions

    • Each element you insert into the string literal must be wrapped in a pair of parentheses, and then prefixed with a backslash

image

  • String index

    • Each String value has an associated index type, String.Index, which is equivalent to the position of each Character in the string

    • startIndex property to access the position of the first Character in String.

    • The endIndex property is the position after the last character in the String

    • If String is empty, startIndex and endIndex are equal

    • The usage of string array cannot be directly accessed through subscripts. The subscript method of String does not support Int subscripts at all.

    • Use index (before :) and index (after :) methods to access before and after a given index

    • To access an index farther from a given index, you can use index (_: offsetBy :)

    • Use the indices property to access the index of each character in the string

image

image

  • insert

    • To insert characters, use the insert (_: at :) method

    • To insert the content of another string into a specific index, use the insert (contentsOf: at :) method

image

  • delete

    • To remove characters, use the remove (at :) method

    • To remove a small range of specific strings, use the removeSubrange (_ :) method

image

  • String comparison

    • String and character equality (== and! =)

    • Prefix equality hasPrefix (_ :)

    • Suffix equality hasSuffix (_ :)

image

 

Guess you like

Origin www.cnblogs.com/liuxiaokun/p/12684663.html