AAA
An
Any
Accessory.
Any
Accessory?
An 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.
- Package.swift (Using Swift Package Manager)
import PackageDescription
let package = Package(
name: "AWESOME_APP",
dependencies: [
.Package(url: "https://github.com/minacle/AAA.git", majorVersion: 1),
]
)
- Cartfile (Using Carthage)
github "minacle/AAA" ~> 1.0
- Podfile (Using CocoaPods)
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.
- Just wrap anything with
AAA
. - Explore with index or key.
[]
to get a copy of the thing!
One more thing. It never throws error!
- If index or key was wrong, it returns
AAA
which containsnil
. - If so,
[]
returnsnil
.