Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tarunon/AnyObjectConvertible
Convert your own struct/enum to AnyObject easily.
https://github.com/tarunon/AnyObjectConvertible
Last synced: about 1 month ago
JSON representation
Convert your own struct/enum to AnyObject easily.
- Host: GitHub
- URL: https://github.com/tarunon/AnyObjectConvertible
- Owner: tarunon
- License: mit
- Archived: true
- Created: 2016-01-27T09:20:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-16T16:21:17.000Z (over 8 years ago)
- Last Synced: 2024-12-01T16:04:49.211Z (about 1 month ago)
- Language: Swift
- Size: 17.6 KB
- Stars: 61
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - AnyObjectConvertible - Convert your own struct/enum to AnyObject easily. (Data Structures / Algorithms / Getting Started)
- awesome-ios-star - AnyObjectConvertible - Convert your own struct/enum to AnyObject easily. (Data Structures / Algorithms / Getting Started)
- fucking-awesome-ios - AnyObjectConvertible - Convert your own struct/enum to AnyObject easily. (Data Structures / Algorithms / Getting Started)
- fucking-awesome-ios - AnyObjectConvertible - Convert your own struct/enum to AnyObject easily. (Data Structures / Algorithms / Getting Started)
README
# AnyObjectConvertible
[![Build Status](https://travis-ci.org/tarunon/AnyObjectConvertible.svg?branch=master)](https://travis-ci.org/tarunon/AnyObjectConvertible)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![CocoaPods compatible](https://img.shields.io/cocoapods/v/AnyObjectConvertible.svg?style=flat)](https://cocoapods.org/pods/AnyObjectConvertible)Convert your own struct/enum to AnyObject easily.
## Sumally
We can cast everything as AnyObject at Swift3.0 🎉
So, we would not need this library no longerIn swift, struct or enum cannot convert to AnyObject
```swift
NSNotificationCenter.defaultCenter().postNotificationName("foo", object: YourOwnStruct()) // Fail
```
Of course, you may wrap value using Box\.
```swift
class Box {
let value: T
init(value: T) {
self.value = value
}
}
NSNotificationCenter.defaultCenter().postNotificationName("foo", object: Box(value: YourOwnStruct())) // OK
```But Box\ unwrap is too lazy.
```swift
let value = (notification.object as? Box)?.value
```You can cast your struct/enum directory if implement AnyObjectConvertible at that type.
```swift
extension YourOwnStruct: AnyObjectConvertible {}NSNotificationCenter.defaultCenter().postNotificationName("foo", object: YourOwnStruct()) // OK
let value = notification.object as? YourOwnStruct
```## Installation
#### CocoaPods
You can use [CocoaPods](http://cocoapods.org/) to install `AnyObjectConvertible` by adding it to your `Podfile`:```ruby
platform :ios, '8.0'
use_frameworks!
pod 'AnyObjectConvertible'
```To get the full benefits import `AnyObjectConvertible` wherever you import UIKit
``` swift
import UIKit
import AnyObjectConvertible
```
#### Carthage
Create a `Cartfile` that lists the framework and run `carthage bootstrap`. Follow the [instructions](https://github.com/Carthage/Carthage#if-youre-building-for-ios) 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