1 ///////////////keywords used in declarations/////////////////2 3 //1.class Declaration of a class4 Public classAClass {5 //. class declares a class function6 Public classFunc test (Let Num:int)void{7 8 }9 }Ten One //2.deinit anti-initialization A deinit{} - - the //3. Enumeration - Public enumUibuttontype:int { - - CaseCustom//No button type +@available (IOS7.0, *)//suitable for iOS7.0 or above, any platform - CaseSystem//Standard System button + A Casedetaildisclosure at CaseInfolight - CaseInfodark - CaseContactadd - - Public Staticvar Roundedrect:uibuttontype {Get}//Deprecated, use Uibuttontypesystem instead - } in - to //4.extension (Extended string Class) + extension String { - PublicTypealias Index =String.CharacterView.Index the Publicvar Startindex:index {Get } * Publicvar Endindex:index {Get } $ PublicSubscript (I:index), Character {Get }Panax Notoginseng } - the //5.func Function Declaration +Func test (Let Num:int)void{} A the //6.import Import Framework or class + Import Foundation - Import UIKit $ $ //7.1 Init constructor function - - //initializers for creating colors the PublicInit (white:cgfloat, alpha:cgfloat) - PublicInit (hue:cgfloat, Saturation:cgfloat, Brightness:cgfloat, alpha:cgfloat)Wuyi the //7.2 Convenience Convenient constructor, mainly for some variables to do some default value settings - Publicconvenience init (color:uicolor) Wu - //8.let declares a constant Var declares a variable About $ //9.protocol Protocol - Publicprotocol Nsobjectprotocol { - - PublicFunc isequal (Object: Anyobject?) -Bool A Publicvar Hash:int {Get } + the Publicvar superclass:anyclass? {Get } - $ PublicFunc ' self ' () Self the the PublicFunc Performselector (aselector:selector), unmanaged<anyobject>! the PublicFunc performselector (Aselector:selector, WithobjectObject: anyobject!) unmanaged<anyobject>! the PublicFunc performselector (Aselector:selector, Withobject object1:anyobject!, Withobject object2:anyobject!) unmanaged<anyobject>! - in PublicFunc IsProxy ()Bool the the PublicFunc Iskindofclass (Aclass:anyclass)Bool About PublicFunc Ismemberofclass (Aclass:anyclass)Bool the@available (IOS2.0, *) the PublicFunc Conformstoprotocol (Aprotocol:protocol)Bool the + PublicFunc Respondstoselector (Aselector:selector)Bool - the Publicvar description:string {Get }Bayi theOptional Publicvar debugdescription:string {Get } the } - - //10.1 static variable the the the //10.2 static function (in enum and struct) the classstatic functions (in classes and protocols) - the/*in the context of a non-class type, we uniformly use static to describe the scope of the type. Include when expressing type methods and type properties in enum and struct. In these two value types, we can declare and use stored properties within the scope of the type to evaluate properties and methods. theThe class keyword is much more clear than it is, and is specifically used in the context of the class type, and can be used to modify the class method and the computed properties of the class. It is important to note that the storage properties are not present in class
theone of the more special is protocol. In swift, class, struct, and enum can be implemented protocol. So what keyword should we use if we want to define a method or a computed attribute on a Type field in protocol? The answer is to use class to define, but in the implementation of the rule: Use class keyword in class, and still use static--in struct or enum although it is defined in protocol with class:94 */ the structPoint { the Let x:double the Let y:double98 //Store Properties About StaticLet zero = point (x:0Y:0) - //Calculated Properties101 Staticvar ones: [Point] {102 return[Point (x:1Y:1),103Point (x:-1Y:1),104Point (x:1, Y:-1), thePoint (x:-1, Y:-1)]106 }107 //Type Method108 StaticFunc Add (P1:point, P2:point),Point {109 returnPoint (x:p1.x + p2.x, Y:p1.y +p2.y) the }111 } the 113 protocol MyProtocol { the classFunc foo ()String the } the structMystruct:myprotocol {117 StaticFunc foo ()String {118 return "MyStruct"119 } - }121 enumMyenum:myprotocol {122 StaticFunc foo ()String {123 return "MyEnum"124 } the }126 classMyclass:myprotocol {127 classFunc foo ()String { - return "MyClass"129 } the }131 the //11. New Class Someclaa
New structural body SomeStruct133 //variable Someproperty134 //Method SomeFunction135 classsomeclass{136 }137 138 structSomeStruct {139 $ }141 142 enumsomeenumeration{143 144 }145 146 Protocol someprotocol{147 148 149 } Max //to keep a class compliant with a protocol, you need to add a protocol name after the type, separated by a colon: as part of the type definition. When complying with multiple protocols, each protocol is separated by commas. 151 structsomestruct:firstprotocol,anotherprotocol{ the Properties and Methods153 }154 //if a class has a parent class that adheres to the protocol, the parent class should be placed before the protocol name, separated by commas155 classsomeclass:superclass,firstprotocol,anotherprotocol{156< #properties and methods#>157}
The keywords that Swift uses in the declaration