Getting Started with Swift 2

Source: Internet
Author: User
Tags closure

Import foundation//defines an optional type that indicates that the variable can be Nil//var intnumber:int? = 100////in use, add one after the variable! Represents a split package that can be obtained as a true value//print (intnumber!) if var intnewnumber = Intnumber{//print (intnumber!) }else{//print ("error")//}///%%%%%%%%%%%%%%%%%%%%%% struct%%%%%%%%%%%%%%%%%%%%%%%%//uses a struct to define a struct//format: struct + struct name {struct implementation}struct Frame {//store property is responsible for storing the value of the property var x:float var y:float var width:float var Height:flo The AT//calculation attribute is responsible for storing the property for the operation to get the property//Get method: Add {} After the property type to declare and implement the Get method using the Get keyword//set method: Ibid//set method can not appear alone, must be paired with the Get method. But get can var centerx:float{get{return x + WIDTH/2}} var centery:float{retur N y + height/2}//struct property static var mystruct = "I am a struct attribute"//struct can declare and implement a function Func Sayhi () {print ("Hello,  I am a big Tree ")}//Init method//Swift in struct can declare and implement an Init method init (x newx:float, y newy:float, Width newwidth:float, Height newheight:float) {x = newx y = newy width = newwidth HeigHT = newheight}//struct-body method//struct-body method can only be called struct property static func saymy () {print (self.mystruct)}}print (F RAME.MYSTRUCT)//How to create an object//the first var frame:frame = Frame.init (X:10, Y:10, width:100, height:100)//the second Var frame1:fr Ame = Frame (X:10, Y:10, width:100, height:100) print (Frame1.centerx) frame1.sayhi ()//called by struct, not with Object frame.saymy ()// &&&&&&&&&&&&&&&&&&&&&& &&&&&& category &&&&&&&&&&&&&&& &&&&&&&&&&&&&//CALSS + Tired name * {class implementation}class Person{//When writing properties to a class, this        attribute if there is no initial value then it is optional type: var name = String?         var name = "Hehehe" var gender = "You guessed" var age = 108 var width:float{get{return 100} The set {//Set method NewValue represents the new value Self.width = newvalue}}//static modifier is class Property static Var Ho Bby:striNg?    = "Manganese"//ordinary functions cannot invoke class properties. Class properties can only be called by class functions.  Func Sayhi () {print ("You Ya Self.width")} init (name newname:string, gender newgender:string, age NewAge:    INT) {name = NewName gender = Newgender age = newage} static func saymy () {print (self.hobby!)  }}var Person:person = person (name: "Boss", Gender: "Dick", Age:3) print (Person.name) person.sayhi () person.saymy ()//class Dog {//var name:string?//var style:string?//static var hobby:string? = "hehe hey"//func Sayhh () {//print ( "Wang Woo")//}//init (name newname:string, style newstyle:string) {//name = newname//style = newstyle//}/ /static Func saymy () {/////print (self.hobby!) }//}//var Dog:dog = Dog (name: "Wangcai", Style: "Bosimao")//print (dog.name!, dog.style!) DOG.SAYHH ()//dog.saymy ()//////class Student:person {////var number:int?//////Override the Parent class method: You need to add an override modifier/ /override Func Sayhi () {////super.sayhi () plus super represents a method that completely calls the parent class.//}//override init function of parent class//Override init (name newname:string, gender newgender:string, age newage:int) {// Super.init (Name:newname, Gender:newgender, age:newage)/////////////////////To recreate an Init method with}//    t//init (number Newnumber:int) {//Super.init (name: "Zhang San", Gender: "Male", age:1)//number = newnumber//// }////}//^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ the ^^ ^^ the ^^ the ^^ of the Protocol ^^ the ^^ ^^ ^^ ^\//declares that the agreement of a Protocol//Declaration is a protocol that all methods must be implemented by the SWIFT protocol. Protocol m ydelegate{//A Protocol method func test ()//function modified with mutating can modify the properties of the struct within the struct//function with mutating modifier in the protocol. You can not use Mut when complying with the agreement The ating modifier has no effect in the class, but the value of the property cannot be modified in the struct.   There is no function that uses the mutating modifier, but the result of using the mutating modifier when implemented is to re-declare a function that is not related to the protocol. mutating func Change ()}//uses the @objc decorated protocol to indicate that this Protocol is OC Protocol//The function that can declare an optional implementation in front of the function is decorated with optional, this function is optional implementation function//When implementing this function, In front of the function to add @objc decoration//@objc modification of the protocol, can not abide in the structure of @objc protocol newdelegate{//optional Implementation optional Func newtest () func newchange () }//class Teacher:person, MyDelegate, newdelegate{   Func test () {print ("AAA")}//The function after using mutating in the protocol is a normal function in the class func change () {} @objc func NewText () {} @objc func Newchange () {}}let teacher:teacher = teacher (name: "A", Gender: "Male", age:111)  struct struct mystruct:mydelegate//@objc modified protocol, can not be adhered to in the struct without writing newdeleate{var name:string func Test () {// The function in the struct cannot directly change the properties in the struct} mutating func () {name = "111"}}//$$$$$$$$$$$$$$$$$$$$$ extension $$$$$$$$$$$$$ $$$$$$$$$$$$$\//extension + class name indicates that you want to extend this class.//extension can only extend the function cannot extend the properties//extension can give a class extension agent protocol extension Person:mydeleg ate{func Test () {} func change () {} Func Eat () {}}//extension can extend a new protocol to the struct//extensio N frame:mydelegate{//func text () {//}//func change () {//}//}//!#@@#$^%$%&^&* (&&^$%!^&^ *&*)%$#$#@ closure pack @%^%*%^#@[email protected]#[email protected]!^[email protected]?? Declares a closed-packet var Myblock: (Closed-color format) var Myblock:((A:int, B:int)->int)//The first method to implement the closure Myblock = {(A:int, b:int), Int in return a > B? a:b}//the second Kind    To implement closures, omit parameter types and return values Myblock = {A, b in return a > B? a:b}//the third way to implement closure omitted parameter type omit return value omit returns keyword Myblock = {A, b in a > B? a:b}//the fourth method omits the use of the $ notation to take the first few parameters. Myblock = {$ > $ $: $1}//Fifth Apple recommended Myblock = {(A, B) Int in Retu RN a > B? A:B}

Getting Started with Swift 2

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.