Summary of Swift syntax

Source: Internet
Author: User

Summary of Swift syntax


1. Optional is an enumeration
An enum of Optional is just
Enum optional<t>{
Case None
Case Some (T)
}
Statement of the 2.Array
var a=array<string> ()
var a=[string] ()
Let a=["a", "B", "C") cannot be changed
Let B=a[3]
Traversing an array can use the for
For array in a{
println ("\ (array)")
}
3.Dictionary
var dic=dictionary<string,int> ()
var dic=[string:int] ()
dic=["Title1": 1, "Title2": 2]

Accessing the value of a dictionary will return optional
Let value=dic["Title3"] returns nil
Using tuple traversal Dictionary
for (Key,value) in dic {
println ("\ (key) =\ (value)")
}
4. Structure range A starting point, an end point, called a reasonable type, range is a model
struct range<t>{
var startindex:t
var endindex:t
}
A. The type in Range in array is int, because array is indexed by int range<int>
Index of String in B.string range<string.index>
C. Declaration
Let array=["a", "B", "C", "D"]
Let subarray1=array[2...3] (["C", "D"]) closed interval
Let Subarray2=array[2..<3] (["C"]) left closed right open interval
Traverse
For i in [27 ... 104]{
println ("i=\ (i)")
}
5.NSObject (all classes in Swift can inherit NSObject)
Base classes for all classes in objective-c, such as Uiviewcontroller
6.NSNumber class with numbers
Let N=nsnumber (double:35.5)
will be converted to int or double with its own value.
Let Intversion=n.intvalue/n.doublevaule/n.boolvalue
7.NSDate storage date, can also get the current date and time
Calendar,dateformatter,datecomponents
8.NSData bit Packet
Passing untyped data, raw data
9. Data structure class, enumeration, structure
Same point: A. Declaration is similar to B. can have its own properties and methods C. can have their own functions
Different points: A. The enum itself cannot store any value, and the value can be stored in the enumeration's associated information
B. Enumeration can have computed properties
C. Structs and classes can be initialized
D. The nature of the inheritance of classes, types and conversions belong to the nature of the class
E. Enumerations and struct-bodies are value-passing, and classes are reference-passing
10.override overriding the keyword of the parent class method
Final can mark a method as final, indicating that the method cannot be overridden
11. Classes and instances have their own methods and properties


var d:double =-10
If D.issignminus {//issignminus is a negative number
D=double.abs (d)//Take absolute value
}
Issignminus is a variable of instance d, or a property of D.
ABS is a method of the double class (all object sharing) that passes an instance of the double type that needs to be manipulated into the method
class method static Func abs (d:double), Double
12. Name of the parameter
All the methods have an internal name and an external name for all parameters
Internal name: The name of a local variable in the method, preceded by a colon in the method
External name: The caller is ready to use the call method before the colon
A. Using an external name
Func foo (extenal internal:int) {
Let Local=internal
}
Func Bar () {
Let Result=foo (external:123)
}
B. Do not use the external name _ is ignored, all methods by default is used in this way
Func foo (internal:int)
Func foo (_ Internal:int) {
Let Local=internal
}
Func Bar () {
Let Result=foo (123)
}
C. Enforcing the use of internal names #
Func foo (#internal: Int) {
Let Local=internal
}
Func Bar () {
Let Result=foo (internal:123)
}
D. Not the first parameter is recommended to use internal and external names, of course, you can use _ Ignore the appearance name, but this is not the standard practice
Func foo (First:int,externalsencond second:double) {
Let Local=second
}
Func Bar () {
Let Result=foo (123,externalsencond:5.5)
}
13.Property observers (attribute observation)
var somestoredproperty:int = 42{
Called before the willset{newvalue}//property is set
didset{oldvalue}//is called after the property is set and can be used to update the user interface
}
Override Var somestoredproperty{
Willset{newvalue}
Didset{oldvalue}
}
Usage scenario: A. You need to store the property in your own class, and when other methods set its value, get a notification
B. When you inherit some classes, you don't need to make any changes to the attributes in those classes, but when they change, you want to know
14.lazy is initialized only when it is used.
A. If you use a lot of resources, lazy is a good choice.
Lazy var brain=calculatorbrain ()
B. When initializing, put a curly brace in the back, that is, by executing the closure to initialize the property, but the closure will be executed when it is used.
Lazy var someproperty:type={
Return <the constructed vaule>
}()
C. You can use a method to initialize, only marked as lazy, because the methods in the class are not callable until the class is initialized.
Lazy var myproperty=self.initializemyproperty ()
15.initialization initialization
A. Classes without a parent class, with an init () method by default
B. If a struct is not initialized, it will default to an initialization method that takes all properties as arguments, without implementing
struct mystruct{
var x:int=42
var y:string= "Moltuae"
Init (x:int,y:string)
}
C. In the initialization method, you can set the value of any property, including constants and variables
D. You can call your own Init method Self.init () in a class or struct, and you can call the initialization method of the parent class in a class Super.init ()







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of Swift syntax

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.