Latest 0.0.3
Homepage https://github.com/tarunon/AnyObjectConvertible
License MIT
Platforms ios 8.0
Authors

Build Status
Carthage compatible
CocoaPods compatible

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

CocoaPods

You can use CocoaPods to install AnyObjectConvertible by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!
pod 'AnyObjectConvertible'

To get the full benefits import AnyObjectConvertible wherever you import UIKit

import UIKit
import AnyObjectConvertible

Carthage

Create a Cartfile that lists the framework and run carthage bootstrap. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/YourLibrary.framework to an iOS project.

github "tarunon/AnyObjectConvertible"

Manually

  1. Download and drop AnyObjectConvertible.swift in your project.
  2. Congratulations!

License

MIT

Latest podspec

{
    "name": "AnyObjectConvertible",
    "version": "0.0.3",
    "summary": "Convert your own struct/enum to AnyObject easily.",
    "description": "Convert your own struct/enum to AnyObject easily.nYou can cast your struct/enum directory if implement AnyObjectConvertible at that type.nn- extension YourOwnStruct: AnyObjectConvertible {}n- NSNotificationCenter.defaultCenter().postNotificationName("foo", object: YourOwnStruct()) // OKn- let value = notification.object as? YourOwnStruct",
    "homepage": "https://github.com/tarunon/AnyObjectConvertible",
    "license": "MIT",
    "authors": {
        "tarunon": "[email protected]"
    },
    "source": {
        "git": "https://github.com/tarunon/AnyObjectConvertible.git",
        "tag": "0.0.3"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "AnyObjectConvertible/*.swift"
}

Pin It on Pinterest

Share This