Swift language Learning (iv) use of a collection of strings and arrays

Source: Internet
Author: User

Swift provides its own set of C and objective-c base types, including int for integer, double and float for floating-point values, Boolean for Boolean, and string for text data. Swift also provides an enhanced version of both the array and dictionary collection types.

We know that the most basic types used in iOS development may be the number of strings, as well as the use of some collection types, so let's simply learn the use of string strings and collection types such as arrays, dictionary, etc.

One, the string

1, string literal, such as "Jeck" literal

2. The difference between a string in swift and a string in OC

The string in swift does not have a variable type and an immutable type, and in Swift, the string is immutable, depending on the type of the declared variable, if the definition of let is immutable, if VAR is defined as variable

The string in Swift is a value type, and in OC, no special declaration is a reference type, do not worry about memory problems, Swift has been optimized,

3, the variable type in swift and OC in the NSString do a perfect docking, in other words, is Swiftstrig can use NSString method

Here's a quick introduction to some of the ways to use the string:

4. String interpolation

There is no format placeholder in the SWIFT output statement, and to achieve this, we use the string interpolation format: "\ ()"

Ii. collections in Swift

The difference between a collection in Swift and a collection in OC:

1. The collection class in Swift is also a value type, and the reference type in OC

2. In Swift, it is also the concept of using reference counting to manage memory, but the reference count applies only to object types, and value types do not need to be managed

3. In swift, the method of reference count management memory is different from the method used in OC to manage memory (see following)

4. Arrays in Swift can hold any type, but in OC only the object type is stored

1. Arrays (Array)

In Swift, the array also has no variable and immutable types, and depending on what keyword is used to modify it, Var is mutable and let is immutable

In Swift, a literal is used to assign a value to a number

When we declare the type of the elements in the array beforehand, the inner elements must be identical

 

Of course we can also write like this:

Here we introduce the elements of the array to add, delete, change, check:

(1), array add element

(2), array delete element

Removes the element with the specified subscript by means of the following face

(3), change the elements in the array


(4), array query

Array initialization, declaring an array that stores a fixed type

1.

2.

Creating an array of a specific data type using the constructor method

2. Dictionary (Dictionary)

Use literals to assign values to a dictionary

1. Increase of dictionary key value pairs

2. Deletion of dictionary key value pairs

3. Modification of dictionary key value pairs

4, dictionary key value pairs of query

The printing results are as follows:

There are two ways to initialize a dictionary

A dictionary of fixed-key-value types is created using the dictionary initialization method

Collection comparison in Swift and OC

In OC, the arrays and dictionaries that we use are reference types, whereas Swift is a value type because in swift, the bottom layer of these associative classes is the struct

enumeration value types, functions, closures are reference types

Swift language Learning (iv) use of a collection of strings and arrays

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.