Swift / iOS: How to assign value for OrderedDictionary variable? -
i want assign value ordereddictionary
(https://github.com/lukaskubanek/ordereddictionary) , way, it's ok.
var ordereddictionary: ordereddictionary<string, int> = ["a": 1, "b": 2, "c": 3, "d": 4]
but this, doesn't work.
// json data let abc:[string:anyobject] = ["a": [1], "b": [2], "c": [3], "d": [4]] var od: ordereddictionary<string, anyobject> = abc
or
var od: ordereddictionary<string, anyobject> = abc as! ordereddictionary<string, anyobject>
why? what's different there? , how assign value it?
i think there more different issues.
first of all, array not anyobject, have change to
var ordereddictionary2: ordereddictionary<string, any> = ["a": [1], "b": [2], "c": [3], "d": [4]]
the second issue when trying assign dictionary directly, dictionaryliteralconvertible called (ordereddictionary has extension implement protocol), when trying assign dictionary temporary variable, imho assign operator should overloaded:
func = <t,u>(inout left: ordereddictionary<t,u>, right: [t: u])
and last issue mentioned martin r. when regular dictionary initialised, @ point information order lost.
update
example initialization temporary variable:
let array = [("a", 1), ("b", 2), ("c", 3)] let orderedarray: ordereddictionary<string, int> = ordereddictionary(elements: array)
or
let array = [("a", [1]), ("b", [2]), ("c", [3])] let orderedarray: ordereddictionary<string, [int]> = ordereddictionary(elements: array)
notice
json format:
an object unordered collection of 0 or more name/value pairs, name string , value string, number, boolean, null, object, or array.
an array ordered sequence of 0 or more values.
the terms "object" , "array" come conventions of javascript.
Comments
Post a Comment