Created
October 23, 2016 02:08
-
-
Save misbell/90a39719a45cf456398854627f672d09 to your computer and use it in GitHub Desktop.
Functions return [String:Any] and [AnyHashable:Any] , consumed in for-in loop, String conversions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrote this to test out how Arrays of Dictionaries are handled | |
// was surprised to see they become tuples in a for-in | |
// also some unintuitive string conversions | |
import Cocoa | |
func c() -> [String:Any] { | |
print ( "ok" ) | |
var keys = ["automaticDashSubstitutionEnabled": false, "automaticDataDetectionEnabled": false, "automaticQuoteSubstitutionEnabled": false, "automaticSpellingCorrectionEnabled": false, "automaticTextReplacementEnabled": false, "continuousSpellCheckingEnabled": false, "enabledTextCheckingTypes": "abc", "grammarCheckingEnabled": false] as [String : Any]? | |
return keys! | |
} | |
// x is a [String:Array] | |
let x = c() | |
// but d is a tuple | |
for d in c() { | |
// it comes in as a TUPLE | |
let dict = d.0 as String | |
} | |
func f() -> [AnyHashable:Any] { | |
print ( "ok" ) | |
let keys = ["automaticDashSubstitutionEnabled": false, "automaticDataDetectionEnabled": false, "automaticQuoteSubstitutionEnabled": false, "automaticSpellingCorrectionEnabled": false, "automaticTextReplacementEnabled": false, "continuousSpellCheckingEnabled": false, "enabledTextCheckingTypes": "abc", "grammarCheckingEnabled": false] as [String : Any]? | |
return keys! | |
} | |
var y = f() | |
for e in f() { | |
// it comes in as a TUPLE | |
let dict = e.0 as! String | |
} | |
func g() -> [AnyHashable:Any] { | |
print ( "ok" ) | |
let keys : [AnyHashable:Any]? = ["automaticDashSubstitutionEnabled": false, "automaticDataDetectionEnabled": false, "automaticQuoteSubstitutionEnabled": false, "automaticSpellingCorrectionEnabled": false, "automaticTextReplacementEnabled": false, "continuousSpellCheckingEnabled": false, "enabledTextCheckingTypes": "abc", "grammarCheckingEnabled": false] | |
return keys! | |
} | |
var j = g() | |
for k in g() { | |
// it comes in as a TUPLE | |
let dict = k.0 //AnyHashable not convertible to String | |
} | |
// this gives us what we need | |
// g returns [AnyHashable: Any] | |
// from stackoverflow | |
var m = g() as NSDictionary as! [String: AnyObject] as! [String : NSObject] | |
for s in m { | |
// it comes in as a TUPLE | |
let key = s.0 | |
print ("key \(key)") | |
} | |
//=================== | |
var n = g() as NSDictionary as! [String: AnyObject] | |
for s in n { | |
// it comes in as a TUPLE | |
let key = s.0 | |
print ("key \(key)") | |
} | |
//=================== | |
var o = g() as NSDictionary | |
print ("o is \(o)") | |
for s in o { | |
// it comes in as a TUPLE | |
let key = s.0 | |
print ("key \(key)") | |
} | |
//=================== | |
var p = g() as Dictionary | |
for s in p { | |
// it comes in as a TUPLE | |
let key = s.0 | |
print ("key \(key)") | |
} | |
//=================== | |
var q = g() as Dictionary as! [String: AnyObject] | |
for s in q { | |
// it comes in as a TUPLE | |
let key = s.0 | |
print ("key \(key)") | |
} | |
//==================== | |
var t = g() as Dictionary | |
for s in t { | |
// it comes in as a TUPLE | |
let key = s.0 | |
print ("key \(key)") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment