Extension and Protocol
Extension
It’s kind of Category for Objective-C??
Sample
I tried NSString contains I did in Objective-C
StringUtils.swift
import Foundation
extension String {
func contains(parts : String) -> Bool {
return self.rangeOfString(parts) != nil;
}
}
Test
let str : String = "abcdefg"
let target : String = "ddd"
let target2 : String = "def"
println(str.contains(target))
println(str.contains(target2))
Result
false true
Protocol
Protocol seems to be same as Objective-C.
Is like Java interface.
Example(ProtocolTest.swift)
import Foundation
protocol TestProtocol {
var desc: String { get }
func test()
}
class ImplTestProtocol : TestProtocol {
var desc : String = "Test"
func test() {
println(desc)
}
}
Test
let impl = ImplTestProtocol() impl.test()
