Swift Chapter III: Classes, enumerations, structs

Source: Internet
Author: User


Let's start by creating a class and declaring properties and methods inside.

 class BaseUser {    var userName:String =  "SWH"      var password:String =  "123"         // There is a construction method, which is similar to the way we rewrite the Init method, and then we can add some parameters     //but when we declare the class, we can just put parentheses after the class name. Oh      init (username:string, password:string)  {         self.username = username        self.password =  password    }        var name:string {     //The following are two definitions of the Set method         set  (value)  {             self.userName = value             println ("Method set")          }         set {//             self.userName = newValue//             println ("Method set")//        }         get {             println ("Method get")              return self.username        }    }         func tostring ()-> string {         return  "Username:\ (self.username): \ (Self.password)"      }}


At this point we call the corresponding property and method

Declare and initialize var user = Baseuser (username: "Kutian", Password: "123456") println (User.tostring ()) User.Name = "Kutian" println ("\ (User.Name) \ (User.password)")


Next, we continue to create a class to inherit the previous class

Inheritance, not the difference between what you think.

Class Adminuser:baseuser {var managermodel:string = "Furnace stone legend" override Func toString (), String {Retu RN "Furnace stone legend"}}var AdminUser = AdminUser (username: "SwH", Password: "123") println (Adminuser.tostring ())


Okay, next we create the enumeration

We can assign a value directly, or we can get a way to get it back to the new value.

 enum Suit:String {    case Spades =  "Spades", hearts  =  "Red Peach", diamonds =  "block", clubs =  "Plum"     func  ToString ()-> string {        switch self {         case . spades:            return  "Spades"          case . hearts:            return  "Red Peach"          case . diamonds:            return  "Block"          case . clubs:            return  "Plum Blossom"          default :            return  "Ghost card"          }    }}


And then invoke the enumeration that involves some of the methods

There's a new rawvalue in there! is to turn any nil into normal.

var suit = suit.heartsprintln (suit.rawvalue) Let Tempsuit = Suit (rawValue: "Plum") println (tempsuit!. RawValue)


We can also create a new class and declare that a property is an enumeration type

Okay, let's create a structure.

struct Hero {static var Isgirl:bool = False Var rank:rank var name:compasspointtype func toString () String {return ' \ (name): \ (Rank.rawvalue) '}}


In fact, it is similar to the class, but it does not involve the transfer of the value of the address OH

And then we'll write an agreement.

Protocol Simpleprotocol {var value:string {get} func toString () String mutating func Update ()} protocol Baseprotocol {func equal ()-Bool}class superclass {var name:string = "SwH"}


Next, create a class to follow and implement this Protocol

Class Subclss:superclass, Simpleprotocol, Baseprotocol {var value:string = "Defalut" func toString (), Stri        ng {return value} Func update () {value = "new value"} func equal (), Bool { Return False}}


And then, let's write another extension.

Class Rect {var width:int = var Height:int = 100}extension Rect:baseprotocol {func area ()-Int { Return width * height} func equal () Bool {return height = = width}} extension Int {func to String () Strings {return "you guessed"}}

Classes and protocols are better understood, no different from o-c.


OK, so much more.


This article is from the "Neusoft iOS Alumni Group Technology Blog" blog, please be sure to keep this source http://neusoftios.blog.51cto.com/9977509/1673824

Swift Chapter III: Classes, enumerations, structs

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.