K1: protocol及 protocol的extension
public protocol RXTestProtocol {
var value: String { get }
var valueImpByExtension: String { get }
var valueImpByExtensionAndObject: String { get }
func test()
func testImpByExtension()
func testImpByExtensionAndObject()
}
public extension RXTestProtocol {
// 在protocol的extension中,函数需要有实现
public func testInExtension() {
NSLog("testInExtension, value:%@", self.value)
NSLog("testInExtension, valueImpByExtension:%@", self.valueImpByExtension)
NSLog("testInExtension, valueImpByExtensionAndObject:%@", self.valueImpByExtensionAndObject)
}
public var valueImpByExtension: String {
return "valueImpByExtension"
}
public var valueImpByExtensionAndObject: String {
return "valueImpByExtensionAndObject"
}
public func testImpByExtension() {
NSLog("testImpByExtension:%@", self.valueImpByExtension)
}
public func testImpByExtensionAndObject() {
NSLog("testImpByExtensionAndObject:%@", self.valueImpByExtensionAndObject)
}
}
class RXTestProtocolObject: NSObject, RXTestProtocol {
var value: String
var valueImpByExtensionAndObject: String
func test() {
NSLog("test in object, value:%@", self.value)
NSLog("test in object, valueImpByExtension:%@", self.valueImpByExtension)
NSLog("test in object, valueImpByExtensionAndObject:%@", self.valueImpByExtensionAndObject)
}
func testImpByExtensionAndObject() {
NSLog("test in testImpByExtensionAndObject, valueImpByExtensionAndObject:%@", self.valueImpByExtensionAndObject)
}
override init() {
self.value = "abc"
self.valueImpByExtensionAndObject = "abc object"
}
}
RXTestProtocolViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let obj = RXTestProtocolObject()
obj.test()
obj.testInExtension()
obj.testImpByExtension()
obj.testImpByExtensionAndObject()
self.view.backgroundColor = UIColor.white
}
输出:
2018-08-27 19:51:58.974901+0800 RXSwiftDemo[8372:3788328] test in object, value:abc
2018-08-27 19:51:58.974950+0800 RXSwiftDemo[8372:3788328] test in object, valueImpByExtension:valueImpByExtension
2018-08-27 19:51:58.974965+0800 RXSwiftDemo[8372:3788328] test in object, valueImpByExtensionAndObject:abc object
2018-08-27 19:51:58.974977+0800 RXSwiftDemo[8372:3788328] testInExtension, value:abc
2018-08-27 19:51:58.974989+0800 RXSwiftDemo[8372:3788328] testInExtension, valueImpByExtension:valueImpByExtension
2018-08-27 19:51:58.975000+0800 RXSwiftDemo[8372:3788328] testInExtension, valueImpByExtensionAndObject:abc object
2018-08-27 19:51:58.975012+0800 RXSwiftDemo[8372:3788328] testImpByExtension:valueImpByExtension
2018-08-27 19:51:58.975023+0800 RXSwiftDemo[8372:3788328] test in testImpByExtensionAndObject, valueImpByExtensionAndObject:abc object
由上述我们可以得知:
extension可以理解是实现了protocol(半实现,不必要全部实现),其他object在实现的protocol的时候会覆盖extension
K2: associatetype
public protocol RXATTestProtocol {
associatedtype DataType
var value1: DataType { get }
var value2: DataType { get }
}
class RXATTestProtocolObject: NSObject, RXATTestProtocol {
// 输入value1的时候,自动代码提示为 RXATTestProtocolObject.DataType
var value1: String
// 在输入value2的时候,自动代码提示为 String
var value2: String
override init() {
self.value1 = "abc"
self.value2 = "aaaa"
}
}
K3: 泛型以及模板
public class RXTemplateTestObject <T> {
public let value: T
public init(_ value: T) {
self.value = value
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let obj1: RXTemplateTestObject = RXTemplateTestObject<Int>(1)
let obj2: RXTemplateTestObject = RXTemplateTestObject<String>("abc")
NSLog("obj1:%zd, obj2:%@", obj1.value, obj2.value)
}
K4: 函数内部定义一个函数
OC 与 swift区别
OC:1.定义一个block 2. 在外部定义一个函数
swift:1.定义一个func 2. 定义一个block?
网友评论