Swift, wave, come on.

Source: Internet
Author: User

Understand what the teacher says!

var myset:set<int> = [1,2,3,3];//set collection is unordered collection

var arr = [1,2,3];//array ordered collection

var dic = [1: "A", 2: "B"];//dictionaries are unordered collections

For item in arr {//1. To get the length of the array

Print (item)

}

for (Index,value) in Arr.enumerate () {//2. Get the length of the array

Print ("Index is\ (Index) value is\ (value)")

}

for (Key,value) in dic {//Get the length in the dictionary

Print ("\ (key) \ (value)")

}

For k in Dic.keys {//traversal of all keys inside the dictionary

Print (k);

}

For V in dic.values {//Traverse dictionary all values

Print (v);

}

Declaration of the class

A class is a reference type, which means that an instance of the class is assigned to the heap

Class: Properties, methods, constructors, destructors, indexers (subscript)

Class Person {

Store properties, which are returned to their values when they are created

Property Watcher

var p = 20;

let P2 = 40;

var name = "Ch" {

Willset {//assignment will call the contents of Willset

Print ("before");

Print (NewValue); This is the value of the new assignment.

print (name);//This is the value of the original name

}

Didset (old) {//assignment will call the contents of Willset. Here (old) is the name of the custom print (old)

Print ("after");

print (old);//This is the original name value

print (name);//This is the name of the newly assigned value

}

}

Computed Properties, Get,set

Can not only set, only get the time, the expression is read-only computed properties, may be abbreviated

The computed attribute is not a property observer.

The computed attribute is only a var definition, cannot be defined with let, and does not need to be copied from the beginning, and there is no need

A property observer is not required to calculate a property, which can be used to write multiple lines of code.

var Age:int {

get {

return 100;

}

set {

}

}

Read-only computed properties

var Readonlyage:int {

get {

return 100;

}

}

Read-only computed properties can also be defined like this

var Readonlyage2:int {

This is where you can write multiple lines of code.

return 100;

}

Init () {

This is the method that is called when Swift initializes (automatic)

Print ("Init");

}

Deinit {

This is the method that is called when Swift is destroyed (automatic)

Print ("Deinit");

}

}

var p = person ();

P.name = "CX";

P.age = 30;

p = person ();

P.P2 = 50; Here the error let to define the value of the P2 can not be modified

let P2 = person ();//constant pointer

P2.P = 60;

P2.P2 = 70;//Here error let the value of P2 defined is not modifiable

P2 = person;let P2 = person () indicates that the value of P2 cannot be changed, and this p2 = person means that the P2 assignment was re-assigned. So the error

Three equals is to determine whether two variables point to the same object

var p3 = person ();

p = = = P3;

var P4 = P;

P4 = = = P;

Class A {

Init () {

Print ("a init");

}

}

Class B {

Lazy var p = A ();

}

Let B = B ();//use of lazy can make unnecessary or complex initialization is not always done, some storage properties initialization is very time-consuming, add lazy reasonable

The modifiers are static and can be used with these two properties: class,static

Stored property, only static, calculated property Two keywords can be used

Method is also two key words can be

The difference is that the class-decorated representation subclass inherits the parent class and can override these static members

Class Image {

static var name = "my";//Store property

Sclass var name2 = "AAA";//Error store property cannot be modified with class

static Var age:int {

get {

return 100;

}

set {

}

}

static Func SomeMethod () {

Self in a static method does not refer to an instance of a class, but to the class itself

Self refers to the class itself when it appears in a static member, otherwise it is an instance

}

Class Func someMethod2 () {

}

}

Class Xiangce {

var images = [1,2,3,4,5];

Subscript subscript, can have multiple parameters

Subscript (Index:int)->int {

get {

return Images[index];

}

set {

Images[index] = newvalue;

}

}

Func getImage (index:int), Int {

return Images[index];

}

}

var xc = XIANGCE ();

XC[0] = 1200; Modify the value of subscript by subscript

XC[0];

Xc.getimage (0)

Definition of global variables and local variables

Global variables: Defined outside the class, acting on the entire project

Local variables: defined within a class or method, used in classes, and in methods

Swift, wave, come on.

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.