AnyObjectConvertible 0.0.3

AnyObjectConvertible 0.0.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2016
SPMSupports SPM

Maintained by tarunon.



  • By
  • tarunon

AnyObjectConvertible

Convert your own struct/enum to AnyObject easily.

Sumally

In swift, struct or enum cannot convert to AnyObject

NSNotificationCenter.defaultCenter().postNotificationName("foo", object: YourOwnStruct()) // Fail

Of course, you may wrap value using Box<T>.

class Box<T> {
  let value: T
  init(value: T) {
    self.value = value
  }
}
NSNotificationCenter.defaultCenter().postNotificationName("foo", object: Box(value: YourOwnStruct())) // OK

But Box<T> unwrap is too lazy.

let value = (notification.object as? Box<YourOwnStruct>)?.value

You can cast your struct/enum directory if implement AnyObjectConvertible at that type.

extension YourOwnStruct: AnyObjectConvertible {}

NSNotificationCenter.defaultCenter().postNotificationName("foo", object: YourOwnStruct()) // OK
let value = notification.object as? YourOwnStruct

Installation

Use Carthage.

github "tarunon/AnyObjectConvertible"

License

MIT