1
hewigovens 2014-07-02 08:42:35 +08:00 via Android
听上去是redis swift client?
|
2
notcome OP @hewigovens 是的,封装了 Hiredis,已经被 C 指针玩死了 Swift 把 char[] 当 tuple 处理你造吗……
|
3
notcome OP @hewigovens 目标是用 Swift 写服务器端代码,我想类似模板引擎这样的东西反正也能用 JavaScriptCore 解决(虽然不兼容 CommonJS 但比重写好多了……吧!),于是就去试试。
没有类型系统的日子我已经受!够!了! |
4
notcome OP 不过说实话,发 iDev 不合适……不过估计这里的人对 Swift 最熟了。
|
5
notcome OP class EEProto {
var A: Int var B: Double typealias EventChangeA = (Int, Int) -> Void typealias EventChangeB = (Double, Double) -> Void typealias EventChanged = () -> Void struct RegisteredEvent { var eventChangeA: EventChangeA[] var eventChangeB: EventChangeB[] var eventChanged: EventChanged[] } enum EventType { case changeA(EventChangeA[]) case changeB(EventChangeB[]) case changed(EventChanged[]) } var registeredEvents: RegisteredEvent init (A: Int, B: Double) { self.A = A self.B = B registeredEvents = RegisteredEvent(eventChangeA: [], eventChangeB: [], eventChanged: []) } func on (event: EventType) { switch event { case .changeA(let events): registeredEvents.eventChangeA += events case .changeB(let events): registeredEvents.eventChangeB += events case .changed(let events): registeredEvents.eventChanged += events default: assert("unhandled event type | check your code") break } } func resetEvents (eventType: EventType) { switch eventType { case .changeA: registeredEvents.eventChangeA = [] case .changeB: registeredEvents.eventChangeA = [] case .changed: registeredEvents.eventChangeA = [] default: assert("unhandled event type | check your code") break } } func setA (newA: Int) { let oldA = A A = newA for cb in registeredEvents.eventChangeA { cb(oldA, newA) } for cb in registeredEvents.eventChanged { cb() } } func setB (newB: Double) { let oldB = B B = newB for cb in registeredEvents.eventChangeB { cb(oldB, newB) } for cb in registeredEvents.eventChanged { cb() } } } var inst = EEProto(A: 10, B: 5.5) inst.on(EEProto.EventType.changeA([{ println("from \($0) to \($1)") }])) inst.on(EEProto.EventType.changeB([{ println("from \($0) to \($1)") }])) inst.on(EEProto.EventType.changed([{ println("value changed") }])) inst.setA(10) inst.setB(3.14) 好冗余………………丑爆了。 |
6
notcome OP |