//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
class Person {
let name: String
init(name: String) { self.name = name }
var classes: Classess?
deinit { print("\(name) is being deinitialized") }
}
class Classess {
let no: String
init(no: String) { self.no = no }
weak var member: Person?
deinit { print("Apartment \(no) is being deinitialized") }
}
let p1 = Person.init(name: "zhanshan")
let c1 = Classess.init(no: "111")
p1.classes = c1
c1.member = p1
print(p1.classes ?? "")
print(c1.member ?? "")
extension Double {
var km:Double { return self*888}
}
let d1:Double = 2
d1.km
//
protocol SomeProtocol {
var value1: String { get set }
var value2: String { get }
static var value3:String { get }
//异变方法
mutating func getName()
}
//
protocol TwoProtocol {
}
//多承载
protocol AllProtocol: SomeProtocol, TwoProtocol {
//定义属性别名
associatedtype DefineType
var weight:DefineType { get }
}
//结构体继承协议
struct Toprotocol:SomeProtocol {
mutating internal func getName() {
self.value1 = "edit"
}
internal var value1: String
internal var value2: String
internal static var value3: String = "value3"
var value4:String
}
var struct1 = Toprotocol.init(value1: "value1", value2: "value2", value4: "value4")
var struct2:Toprotocol = struct1
struct2.value2 = "2"
//定义别名类型
struct MobilePhone:AllProtocol {
typealias DefineType = Double
internal var weight: Double
mutating internal func getName() {
value2 = "changed"
}
internal static var value3: String = ""
internal var value2: String
internal var value1: String
}
let mobile1 = MobilePhone.DefineType()
var mobile2 = MobilePhone.init(weight: 25, value2: "value2", value1: "value1")
mobile2.getName()
//Swift标准库协议
//1, 比较相关的协议
struct Student1:Equatable {
var math:Int
var english:Int
static func == (lhs: Student1, rhs: Student1) -> Bool {
return lhs.english == rhs.english && lhs.math == rhs.math
}
}
//Student遵守Equatable
let s1 = Student1.init(math: 20, english: 30)
let s2 = Student1.init(math: 20, english: 40)
let isTrue1 = s1 == s2
//2, 自定义比较协议
public protocol ZZNCompareable {
static func < (lhs:Self,rhs:Self) ->Bool
}
struct Student2:ZZNCompareable {
var studentName1:String
var studentName2:String
//重载 < 运算符
static func < (lhs:Student2,rhs:Student2) ->Bool {
return lhs.studentName1 == rhs.studentName2
}
//重载 <= 运算符
static func <= (lhs:Student2,rhs:Student2) ->Bool {
return lhs.studentName1 == rhs.studentName2
}
//重载 > 运算符
static func > (lhs:Student2,rhs:Student2) ->Bool {
return lhs.studentName1 == rhs.studentName2
}
//重载 >= 运算符
static func >= (lhs:Student2,rhs:Student2) ->Bool {
return lhs.studentName1 == rhs.studentName2
}
}
let ss2 = Student2.init(studentName1: "zz", studentName2: "zz")
let ss22 = Student2.init(studentName1: "zz", studentName2: "zw")
ss2 < ss22
//
struct Student3: Comparable {
var math: Int
var english: Int
//重载 < 运算符
static func < (lhs:Student3,rhs:Student3) ->Bool {
return (lhs.math + lhs.english) < (rhs.math + rhs.english)
}
//重载 <= 运算符
static func <= (lhs:Student3,rhs:Student3) ->Bool {
return (lhs.math + lhs.english) <= (rhs.math + rhs.english)
}
//重载 > 运算符
static func > (lhs:Student3,rhs:Student3) ->Bool {
return (lhs.math + lhs.english) > (rhs.math + rhs.english)
}
//重载 >= 运算符
static func >= (lhs:Student3,rhs:Student3) ->Bool {
return (lhs.math + lhs.english) >= (rhs.math + rhs.english)
}
//重载 == 运算符
static func == (lhs:Student3,rhs:Student3) ->Bool {
return (lhs.math + lhs.english) == (rhs.math + rhs.english)
}
}
let s3_left = Student3(math: 80, english: 60)
let s3_right = Student3(math: 70, english: 90)
s3_left > s3_right
//-----
//枚举继承协议
protocol NetServiceProtocol {
var netName:String { get }//只需实现get
mutating func change()//外部修改enum 枚举值
}
enum NetServiceType:NetServiceProtocol {
case union(String)
case mobile(String)
case telecom(String)
mutating func change() {
self = .mobile("mobile")
}
var netName: String {
get {
return NetServiceType.mobile("mobile").netName
}
}
}
网友评论