Swift protocol (Protocol)

Source: Internet
Author: User

A protocol defines a set of standards for methods and attributes without specific implementation.

The Protocol can be implemented (or followed) by classes and struct ).

 

protocol SomeProtocol { // protocoldefinition goes here } struct         SomeStructure:            FirstProtocol, AnotherProtocol {// structure definition goes here}class  SomeClass:    SomeSuperclass,     FirstProtocol, AnotherProtocol { // class definitiongoeshere }

 

 

Attribute

 

1. Set and get accessors

 protocol SomeProtocol { var mustBeSettable:Int { get set }var doesNotNeedToBeSettable: Int { get } }

 

2. Static attributes

 

protocol AnotherProtocol { class var someTypeProperty: Int { get set } }

 

3. Read-Only

 

protocol FullyNamed {var fullName: String { get } }

 

Instance:

 

struct Person: FullyNamed { varfullName: String } letjohn= Person(fullName: "John Appleseed") class Starship: FullyNamed { varprefix: String? varname: Stringinit(name: String, prefix: String? = nil) { self.name = name self.prefix = prefix} varfullName: String { return (prefix ? prefix!+ " " :"")+ name } } varncc1701 = Starship(name: "Enterprise",prefix: "USS") 

Method

1. Definition Method

 protocol RandomNumberGenerator{func random() -> Double}

 

2. Define static methods

 

 protocolSomeProtocol { class func someTypeMethod()}

 

Instance:

 

protocol RandomNumberGenerator{ funcrandom() -> Double } class                   LinearCongruentialGenerator:RandomNumberGenerator {var lastRandom= 42.0let m = 139968.0let a = 3877.0 let c = 29573.0funcrandom() -> Double { lastRandom = ((lastRandom * a + c) %m) returnlastRandom / m } } let generator= LinearCongruentialGenerator() println("Here‘s       a        random         number:\(generator.random())") //    prints    "Here‘s     a     random       number:0.37464991998171" println("And another one: \(generator.random())") //prints "And another one: 0.729023776863283"

Use the Protocol as a Type

 protocol RandomNumberGenerator { func random() -> Double} class LinearCongruentialGenerator: RandomNumberGenerator { varlastRandom= 42.0 let m =139968.0let a = 3877.0 letc = 29573.0func random() -> Double {lastRandom = ((lastRandom * a + c) %m) return lastRandom / m}}class Dice { letsides: Intlet generator: RandomNumberGenerator init(sides: Int, generator: RandomNumberGenerator) { self.sides = sidesself.generator = generator}func roll() -> Int{return Int(generator.random() * Double(sides)) + 1}}vard6 = Dice(sides: 6, generator: LinearCongruentialGenerator())for_ in 1...5 {println("Randomdiceroll is \(d6.roll())")}


Swift discussion forum: http://www.cocoagame.net

Welcome to the swift technology exchange group: 362298485



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.