Swift - Protocols and Extensions

The Swift Programming Language中的代码加上部分EXPERIMENT

import UIKit

protocol ExampleProtocol {
    var simpleDescription: String { get }
    mutating func adjust()
}

class SimpleClass: ExampleProtocol {
    var simpleDescription: String = "A very simple class."
    var anotherProperty: Int = 69105
    func adjust() {
        simpleDescription += "  Now 100% adjusted."
    }
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription

struct SimpleStructure: ExampleProtocol {
    var simpleDescription: String = "A simple structure"
    mutating func adjust() {
        simpleDescription += " (adjusted)"
    }
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription

enum SimpleEnum: ExampleProtocol {
    case SIMPLE(String)
    var simpleDescription: String {
        get {
            switch self {
            case let .SIMPLE(str):
                return str
            }
        }
    }
    mutating func adjust() {
        switch self {
        case let .SIMPLE(str):
            self = .SIMPLE(str + " (adjusted)")
        }
    }
}
var c = SimpleEnum.SIMPLE("A simple enum")
c.simpleDescription
c.adjust()
c.simpleDescription

extension Int: ExampleProtocol {
    var simpleDescription: String {
        return "The number \(self)"
    }
    mutating func adjust() {
        self += 42
    }
}
var d = 7
d.simpleDescription
d.adjust()

let protocolValue: ExampleProtocol = a
protocolValue.simpleDescription
时间: 2024-08-06 03:45:03

Swift - Protocols and Extensions的相关文章

Swift 扩展(Extensions)(十四)

可以为类添加新功能 UIViewController extension UIViewController { // 为 UIViewController 添加的新功能写到这里 } Double extension Double { var km: Double { return self * 1_000.0 } var m : Double { return self } var cm: Double { return self / 100.0 } var mm: Double { retur

Swift学习:扩展(Extensions)

扩展就是为一个已有的类.结构体.枚举类型或者协议类型添加新功能.这包括在没有权限获取原始源代码的情况下扩展类型的能力(即 逆向建模).扩展和 Objective-C 的分类类似.(与 Objective-C 不同的是,Swift 的扩展没有名字.) swift 中的扩展可以: 添加计算型属性和计算型类型属性 定义实例方法和类型方法 提供新的构造器 定义下标 定义和使用新的嵌套类型 使一个已有类型符合某个协议 在 swift 中,你甚至可以对协议进行扩展,提供协议要求的实现,或者添加额外的功能,从

Swift -- enum 继承 protocol

原文地址链接:http://blog.csdn.net/duanyipeng/article/details/32338575 Apple官方文档:The Swift Programming LanguageProtocols and Extensions一节的小节练习,要求自行定义一个enumeration枚举类型,并且遵循ExampleProtocol协议: protocol ExampleProtocol { var simpleDescription: String { get } mu

Swift 初见

Swift 初见 本页内容包括: 简单值(Simple Values) 控制流(Control Flow) 函数和闭包(Functions and Closures) 对象和类(Objects and Classes) 枚举和结构体(Enumerations and Structures) 协议和扩展(Protocols and Extensions) 泛型(Generics) 通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”.在 Swift 中,可以用一行代码实

The Swift Programming Language IOS8 快捷编程语言

This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programming interfaces described herein for use on Apple-branded products. This info

Swift中文教程(七)--协议,扩展和泛型

Protocols and Extensions 协议(接口)和扩展 Swift使用关键字protocol声明一个协议(接口): 类(classes),枚举(enumerations)和结构(structs)都可采用协议(protocol): 1 class SimpleClass: ExampleProtocol { 2 var simpleDescription: String = "A very simple class." 3 var anotherProperty: Int

寒城攻略:Listo 教你 25 天学会 Swift 语言 - 02 Swift Tour

import Foundation //*********************************************************************************************** //1.Hello world //_______________________________________________________________________________________________ //输出 "Hello, world&q

The Swift Programming Language 中文版---Swift 初见

Swift 初见 本页内容包括: 简单值(Simple Values) 控制流(Control Flow) 函数和闭包(Functions and Closures) 对象和类(Objects and Classes) 枚举和结构体(Enumerations and Structures) 接口和扩展(Protocols and Extensions) 泛型(Generics) 通常来说,编程语言教程中的第一个程序应该在屏幕上打印"Hello, world".在 Swift 中,可以用

"Swift"编程语言

来自英文文档.百度翻译以及自己没过4级的渣渣英语功底,为了自己以后看起来方便 About Swift 关于"海燕" IMPORTANT 重要 This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programm