AAA

1.0.7

An Any Accessory.
minacle/AAA

What's New

phase17

2017-04-17T10:11:10Z

Build environment:

  • Xcode 8.3.1 (8E1000a)
  • Apple Swift version 3.1 (swiftlang-802.0.51 clang-802.0.41)

Spotlight:

  • Swift 3.1

Enhancement:

  • Support OSX 10.9

AAA

release Travis license Carthage compatible platform

An Any Accessory.

An Any Accessory?

Well.. it sounds great. So, what is it?

You probably encountered at least once the situation that upcasted [Any], [AnyHashable: Any] or anything as Any. If so, you wrote as follows:

func tion(_ anAny: Any) -> Int {
    var anArray: [Int]?
    var aDictionary: [String: Any]?
    var result = 0
    if anAny is [Int] {
        anArray = anAny as! [Int]
    }
    else if anAny is [String: Int] {
        aDictionary = anAny as! [String: Int]
    }
    if let anArray = anArray {
        result = ((anArray[3] as? [Int])?[9] as? [Int])?[27]? ?? 0
    }
    else if let aDictionary = aDictionary {
        result = (aDictionary["3"] as? [String: [String: Int]])?["9"]?["27"] ?? 0
    }
    return result
}

No, stop! Please have mercy on me!

Okay. I'll show some mercy to you.

import PackageDescription

let package = Package(
    name: "AWESOME_APP",
    dependencies: [
        .Package(url: "https://github.com/minacle/AAA.git", majorVersion: 1),
    ]
)
github "minacle/AAA" ~> 1.0
target 'AWESOME_APP' do
  use_frameworks!
  pod 'AAA', '~> 1.0'
end

...Now, we have AAA. Let's try it!

import AAA

func tion(_ anAny: Any) -> Int {
    let a = AAA(anAny)
    return (a[3][9][27][] ?? a["3"]["9"]["27"][]) as? Int ?? 0
}

Wait, what? Are these really same code? What a beautiful!

So, how to use it?

It's super simple.

  1. Just wrap anything with AAA.
  2. Explore with index or key.
  3. [] to get a copy of the thing!

One more thing. It never throws error!

  • If index or key was wrong, it returns AAA which contains nil.
  • If so, [] returns nil.

Description

  • Swift Tools 3.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 01:00:34 GMT-0900 (Hawaii-Aleutian Daylight Time)