swift Swift中面向协议的编程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift中面向协议的编程相关的知识,希望对你有一定的参考价值。
import Foundation
// ErrorType
public enum OSAScriptError: ErrorType {
case InitializingFromSource(source: String?)
case InitializingFromContents(error: NSDictionary)
case Executing(error: NSDictionary)
case Unknown
}
// Protocol
public protocol OSAScriptProperty {
var script : NSAppleScript { get set }
}
public protocol OSAScriptInitialize: OSAScriptProperty {
init()
init(contentsOfPath: String) throws
}
public protocol OSAScriptFunction: OSAScriptProperty {
func run() throws -> NSAppleEventDescriptor
}
public protocol OSAScriptProtocol: OSAScriptInitialize, OSAScriptFunction {}
// Protocol Extension
extension OSAScriptInitialize {
init(contentsOfPath path: String) throws {
self.init()
let url = NSURL(fileURLWithPath: path)
var err : NSDictionary?
guard let script = NSAppleScript(contentsOfURL: url, error: &err) else {
if let err = err {
throw OSAScriptError.InitializingFromContents(error: err)
} else {
throw OSAScriptError.Unknown
}
}
self.script = script
}
}
extension OSAScriptFunction {
func run() throws -> NSAppleEventDescriptor {
var err : NSDictionary?
let result = self.script.executeAndReturnError(&err)
if let err = err {
throw OSAScriptError.Executing(error: err)
}
return result
}
}
// ValueType
struct JavaScript: OSAScriptProtocol {
var script = NSAppleScript()
}
struct AppleScript: OSAScriptProtocol {
var script = NSAppleScript()
}
extension AppleScript {
init(source: String) throws {
guard let script = NSAppleScript(source: source) else {
throw OSAScriptError.InitializingFromSource(source: source)
}
self.script = script
}
}
// Main
_ = try? JavaScript(contentsOfPath: "/path/to/script/javascript.scptd").run()
_ = try? AppleScript(source: "say \"hello again\"").run()
以上是关于swift Swift中面向协议的编程的主要内容,如果未能解决你的问题,请参考以下文章
Swift 学习笔记(面向协议编程)
WWDC Swift 会话中面向协议的编程
Swift -POP( 面向协议编程)与OOP
Swift中面向协议的编程
什么是 Swift 中的面向协议编程?它带来了什么附加价值?
什么是 Swift 中的面向协议编程?它带来了什么附加价值?