Using Autolayout Visual Format with Swift? - autolayout

I've been trying to use the Autolayout Visual Format Language in Swift, using NSLayoutConstraint.constraintsWithVisualFormat. Here's an example of some code that does nothing useful, but as far as I can tell should make the type checker happy:
let foo:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat(
format: "", options: 0, metrics: {}, views: {})
However, this triggers the compiler error:
"Cannot convert the expression's type '[AnyObject]!' to type 'String!'".
Before I assume this is a Radar-worthy bug, is there anything obvious I'm missing here? This happens even without the explicit casting of the variable name, or with other gratuitous downcasting using as. I can't see any reason why the compiler would be expecting any part of this to resolve to a String!.

this works for me with no error:
let bar:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat(
nil, options: NSLayoutFormatOptions(0), metrics: nil, views: nil)
update
the line above may not be compiled since the 1st and 4th parameters cannot be optionals anymore.
syntactically those have to be set, like e.g. this:
let bar:[AnyObject] = NSLayoutConstraint.constraintsWithVisualFormat("", options: NSLayoutFormatOptions(0), metrics: nil, views: ["": self.view])
update
(for Xcode 7, Swift 2.0)
the valid syntax now requests the parameters's name as well, like:
NSLayoutFormatOptions(rawValue: 0)
NOTE: this line of code shows the correct syntax only, the parameters itself won't guarantee the constraint will be correct or even valid!

The first gotcha here is that Swift Dictionary is not yet bridged with NSDictionary. To get this to work, you'll want to explicitly create a NSDictionary for each NSDictionary-typed parameters.
Also, as Spencer Hall points out, {} isn't a dictionary literal in Swift. The empty dictionary is written:
[:]
As of XCode 6 Beta 2, this solution allows you to create constraints with the visual format:
var viewBindingsDict: NSMutableDictionary = NSMutableDictionary()
viewBindingsDict.setValue(fooView, forKey: "fooView")
viewBindingsDict.setValue(barView, forKey: "barView")
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[fooView]-[barView]-|", options: nil, metrics: nil, views: viewBindingsDict))

Try this - remove the initial variable name (format:), use NSLayoutFormatOptions(0), and just pass nil for those optional NSDictionaries:
let foo:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat("", options: NSLayoutFormatOptions(0), metrics: nil, views: nil)

FYI:
if you use views with constraintWithVisualFormat - instead of wrapping with NSMutableDict
["myLabel": self.myLabel!]
and to be more specific
var constraints = [NSLayoutConstraint]()
NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[myLabel]-15-|",
options:NSLayoutFormatOptions.allZeros,
metrics: nil,
views: ["myLabel": self.myLabel!]).map {
constraints.append($0 as NSLayoutConstraint)
}

This works with Xcode 6.1.1:
extension NSView {
func addConstraints(constraintsVFL: String, views: [String: NSView], metrics: [NSObject: AnyObject]? = nil, options: NSLayoutFormatOptions = NSLayoutFormatOptions.allZeros) {
let mutableDict = (views as NSDictionary).mutableCopy() as NSMutableDictionary
let constraints = NSLayoutConstraint.constraintsWithVisualFormat(constraintsVFL, options: options, metrics: metrics, views: mutableDict)
self.addConstraints(constraints)
}
}
Then you can use calls like:
var views : [String: NSView] = ["box": self.box]
self.view.addConstraints("V:[box(100)]", views: views)
This works to add constraints. If you are using iOS, substitute UIView for NSView
You should probably check out
Cartography, which is a new approach, but quite awesome. It uses Autolayout under the hood.
SnapKit, which I haven't tried but is also a DSL autolayout framework

NSLayoutFormatOptions implements the OptionSetType protocol, which inherits from SetAlgebraType which inherits from ArrayLiteralConvertible, so you can initialise NSLayoutFormatOptions like this: [] or this: [.DirectionLeftToRight, .AlignAllTop]
So, you can create the layout constraints like this:
NSLayoutConstraint.constraintsWithVisualFormat("", options: [], metrics: nil, views: [:])

It slightly annoys me that I'm calling NSLayoutConstraint (singular) to generate constraintsWithVisualFormat... (plural), though I'm sure that's just me. In any case, I have these two top level functions:
snippet 1 (Swift 1.2)
#if os(iOS)
public typealias View = UIView
#elseif os(OSX)
public typealias View = NSView
#endif
public func NSLayoutConstraints(visualFormat: String, options: NSLayoutFormatOptions = .allZeros, views: View...) -> [NSLayoutConstraint] {
return NSLayoutConstraints(visualFormat, options: options, views: views)
}
public func NSLayoutConstraints(visualFormat: String, options: NSLayoutFormatOptions = .allZeros, views: [View] = []) -> [NSLayoutConstraint] {
if visualFormat.hasPrefix("B:") {
let h = NSLayoutConstraints("H\(dropFirst(visualFormat))", options: options, views: views)
let v = NSLayoutConstraints("V\(dropFirst(visualFormat))", options: options, views: views)
return h + v
}
var dict: [String:View] = [:]
for (i, v) in enumerate(views) {
dict["v\(i + 1)"] = v
}
let format = visualFormat.stringByReplacingOccurrencesOfString("[v]", withString: "[v1]")
return NSLayoutConstraint.constraintsWithVisualFormat(format, options: options, metrics: nil, views: dict) as! [NSLayoutConstraint]
}
Which can be used like so:
superView.addConstraints(NSLayoutConstraints("B:|[v]|", view))
In other words, views are auto-named "v1" to "v\(views.count)" (except the first view which can be also referred to as "v"). In addition, prefixing the format with "B:" will generate both the "H:" and "V:" constraints. The example line of code above therefore means, "make sure the view always fits the superView".
And with the following extensions:
snippet 2
public extension View {
// useMask of nil will not affect the views' translatesAutoresizingMaskIntoConstraints
public func addConstraints(visualFormat: String, options: NSLayoutFormatOptions = .allZeros, useMask: Bool? = false, views: View...) {
if let useMask = useMask {
for view in views {
#if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(useMask)
#elseif os(OSX)
view.translatesAutoresizingMaskIntoConstraints = useMask
#endif
}
}
addConstraints(NSLayoutConstraints(visualFormat, options: options, views: views))
}
public func addSubview(view: View, constraints: String, options: NSLayoutFormatOptions = .allZeros, useMask: Bool? = false) {
addSubview(view)
addConstraints(constraints, options: options, useMask: useMask, views: view)
}
}
We can do some common tasks much more elegantly, like adding a button at a standard offset from the bottom right corner:
superView.addSubview(button, constraints: "B:[v]-|")
For example, in an iOS playground:
import UIKit
import XCPlayground
// paste here `snippet 1` and `snippet 2`
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
XCPShowView("view", view)
view.backgroundColor = .orangeColor()
XCPShowView("view", view)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
button.setTitle("bottom right", forState: .Normal)
view.addSubview(button, constraints: "B:[v]-|")

You have to access to the struct NSLayoutFormatOptions.
Following works for me.
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("",
options:NSLayoutFormatOptions.AlignAllBaseline,
metrics: nil, views: nil))

// topLayoutGuide constraint
var views: NSMutableDictionary = NSMutableDictionary()
views.setValue(taskNameField, forKey: "taskNameField")
views.setValue(self.topLayoutGuide, forKey: "topLayoutGuide")
let verticalConstraint = "V:[topLayoutGuide]-20-[taskNameField]"
let constraints:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat(verticalConstraint, options: NSLayoutFormatOptions(0), metrics: nil, views: views)
self.view.addConstraints(constraints)
// bottomLayoutGuide constraint
var views: NSMutableDictionary = NSMutableDictionary()
views.setValue(logoutButton, forKey: "logoutButton")
views.setValue(self.bottomLayoutGuide, forKey: "bottomLayoutGuide")
let verticalConstraint = "V:[logoutButton]-20-[bottomLayoutGuide]"
let constraints:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat(verticalConstraint, options: NSLayoutFormatOptions(0), metrics: nil, views: views)
self.view.addConstraints(constraints)

Related

Composing Text Providers with CLKTextProvider.localizableTextProvider(withStringsFileFormatKey:, textProviders:)

Are there any official examples of setting up a complication using localizableTextProvider(withStringsFileFormatKey:, textProviders:)? I can get the text provider to populate when producing a SampleTemplate, but whenever I try to generate a template using getTimelineEntries the text provider generated by localizableTextProvider the result is always blank, no text.
Example (only supporting .utilitarianLarge):
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: #escaping (CLKComplicationTimelineEntry?) -> Void) {
// Call the handler with the current timeline entry
let template = CLKComplicationTemplateUtilitarianLargeFlat()
template.textProvider = CLKTextProvider.localizableTextProvider(
withStringsFileFormatKey: "testComplication",
textProviders: [
CLKSimpleTextProvider(text: "Hello"),
CLKSimpleTextProvider(text: "World")
]
)
handler(CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template))
}
and sampleTemplate as
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: #escaping (CLKComplicationTemplate?) -> Void) {
// This method will be called once per supported complication, and the results will be cached
switch complication.family {
case .utilitarianLarge:
let template = CLKComplicationTemplateUtilitarianLargeFlat()
template.textProvider = CLKTextProvider.localizableTextProvider(
withStringsFileFormatKey: "testComplication",
textProviders: [
CLKSimpleTextProvider(text: "Hi"),
CLKSimpleTextProvider(text: "World")
]
)
handler(template)
default:
handler(nil)
}
}
with ckcomplication.strings as
"testComplication" = "%# %#";
The sample template will always display the text "Hi World", whereas the result from getCurrentTimelineEntry will always display an empty complication.
Has anyone had luck composing text providers in this way?
It appears this API is just broken in Swift (as of 4.2). I worked around this with an Objective C category. Blatantly stolen from here
CLKTextProvider+NNNCompoundTextProviding.h
#interface CLKTextProvider (NNNCompoundTextProviding)
+ (nonnull CLKTextProvider *)nnn_textProviderByJoiningProvider:(nonnull CLKTextProvider *)provider1 andProvider:(nonnull CLKTextProvider *)provider2 withString:(nullable NSString *)joinString;
#end
CLKTextProvider+NNNCompoundTextProviding.m
#implementation CLKTextProvider (NNNCompoundTextProviding)
+ (nonnull CLKTextProvider *)nnn_textProviderByJoiningProvider:(nonnull CLKTextProvider *)provider1 andProvider:(nonnull CLKTextProvider *)provider2 withString:(nullable NSString *)joinString
{
NSString *textProviderToken = #"%#";
NSString *formatString;
if (joinString != nil) {
formatString = [NSString stringWithFormat:#"%#%#%#",
textProviderToken,
joinString,
textProviderToken];
}
else {
formatString = [NSString stringWithFormat:#"%#%#",
textProviderToken,
textProviderToken];
}
return [self textProviderWithFormat:formatString, provider1, provider2];
}
#end

Encode a [String: Encodable] dictionary into JSON using JSONEncoder in Swift 4

I am just curious how can I encode a dictionary with String key and Encodable value into JSON.
For example:
let dict: [String: Encodable] = [
"Int": 1,
"Double": 3.14,
"Bool": false,
"String": "test"
]
The keys in this dict are all of type String, but the type of the values vary.
However, all of these types are allowed in JSON.
I am wondering if there is a way to use JSONEncoder in Swift 4 to encode this dict into JSON Data.
I do understand there are other ways without using JSONEncoder to achieve this, but I am just wondering if JSONEncoder is capable of managing this.
The Dictionary do have a func encode(to encoder: Encoder) throws in an extension, but that only applies for constraint Key: Encodable, Key: Hashable, Value: Encodable, whereas for our dict, it needs constraint Key: Encodable, Key: Hashable, Value == Encodable.
Having a struct for this will be sufficient to use JSONEncoder,
struct Test: Encodable {
let int = 1
let double = 3.14
let bool = false
let string = "test"
}
However, I am interested to know if the it can be done without specifying the concrete type but just the Encodable protocol.
Just figured out a way to achieve this with a wrapper:
struct EncodableWrapper: Encodable {
let wrapped: Encodable
func encode(to encoder: Encoder) throws {
try self.wrapped.encode(to: encoder)
}
}
let dict: [String: Encodable] = [
"Int": 1,
"Double": 3.14,
"Bool": false,
"String": "test"
]
let wrappedDict = dict.mapValues(EncodableWrapper.init(wrapped:))
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try! jsonEncoder.encode(wrappedDict)
let json = String(decoding: jsonData, as: UTF8.self)
print(json)
And here is the result:
{
"Double" : 3.1400000000000001,
"String" : "test",
"Bool" : false,
"Int" : 1
}
I am still not happy with it. If there are any other approaches, I am more than happy to see it.
Thanks!
Edit 1 Moving the wrapper into an extension of JSONEncoder:
extension JSONEncoder {
private struct EncodableWrapper: Encodable {
let wrapped: Encodable
func encode(to encoder: Encoder) throws {
try self.wrapped.encode(to: encoder)
}
}
func encode<Key: Encodable>(_ dictionary: [Key: Encodable]) throws -> Data {
let wrappedDict = dictionary.mapValues(EncodableWrapper.init(wrapped:))
return try self.encode(wrappedDict)
}
}
let dict: [String: Encodable] = [
"Int": 1,
"Double": 3.14,
"Bool": false,
"String": "test"
]
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try! jsonEncoder.encode(dict)
let json = String(decoding: jsonData, as: UTF8.self)
print(json)
Result:
{
"Int" : 1,
"Double" : 3.1400000000000001,
"Bool" : false,
"String" : "test"
}
Edit 2: Take customized strategies into account as per #Hamish 's comments
private extension Encodable {
func encode(to container: inout SingleValueEncodingContainer) throws {
try container.encode(self)
}
}
extension JSONEncoder {
private struct EncodableWrapper: Encodable {
let wrapped: Encodable
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try self.wrapped.encode(to: &container)
}
}
func encode<Key: Encodable>(_ dictionary: [Key: Encodable]) throws -> Data {
let wrappedDict = dictionary.mapValues(EncodableWrapper.init(wrapped:))
return try self.encode(wrappedDict)
}
}
You would need a wrapper since with Encodable protocol to know which item is which to be able to encode it easier.
I suggest Use an enum named JSONValue which has 5 to 6 cases for all Int, String, Double, Array, Dictionary cases. then you can write JSONs in a type-safe way.
This link will help too.
This is how I use it:
indirect enum JSONValue {
case string(String)
case int(Int)
case double(Double)
case bool(Bool)
case object([String: JSONValue])
case array([JSONValue])
case encoded(Encodable)
}
And then make JSONValue: Encodable and write encoding code for each case.

Bind constraints in Input control of SAPUI5

My rest service expose me a group of fields: each filed has a value and a list of attributes: enabled, maxLength (in case of string), minLength (in case of string), decimals (number of decimal digits - in case of float).
In OpenUi5 I have:
value and enabled are properties of Input control Link (Good!! I can bind properties with model contains the attributes)
maxLength and decimals are optionsof String type and Float type (Link) but I can't bind options with a model :-/
minLength I can't find a property/option
I would like map (bind) each attribute with component so that automatically the library control for me without writing more code.
there is a property called maxLength for Input Control.
So the only problem I see is binding minLength and decimals for which there is little bit effort is needed.
Solution
Create your own input control by extending the existing Input
Control.How to achieve it?
Sample Code Structure:
jQuery.sap.require("sap.m.Input");
jQuery.sap.declare("sap.m.ComplexInput");
sap.m.Input.extend("sap.m.ComplexInput", {
metadata: {
properties: {
minLength: {
type: "int"
},
decimals: {
type: "int"
},
events: {
//define your own events like checkMinLength,checkDecimals
}
},
onInit: function () {
//on init do something
},
onAfterRendering: function () {
//called after instance has been rendered (it's in the DOM)
},
_somePrivateMethod: function () {
/*do someting...*/
},
somePublicMethod: function () {
/*do someting...*/
},
}
});
sap.m.ComplexInput.prototype.exit = function () {
/* release resources that are not released by the SAPUI5 framework */
//do something
};
Adding CustomData and using wherever you want to.
Then you can access custom data in validation process or on liveChange or so..
Bind the other properties to the value of customData
var input = new sap.m.Input({
value: '{value}',
enabled: '{enabled}',
maxLength: '{maxLength}',
customData: [
new sap.ui.core.CustomData({
key: 'minLength',
value: '{minLength}'
}),
new sap.ui.core.CustomData({
key: 'decimals ',
value: '{decimals}'
})
],
change: function(oEvent) {
var src = oEvent.getSource();
var minLen = src.getCustomData()[0].getValue();
var decimals = src.getCustomData()[1].getValue();
if (src.getValue() && src.getValue().length > minLen) {
src.setValueState('Success');
} else {
src.setValueState('Error');
}
}
});

How to access deeply nested dictionaries in Swift

I have a pretty complex data structure in my app, which I need to manipulate. I am trying to keep track of how many types of bugs a player has in thier garden. There are ten types of bugs, each with ten patterns, each pattern having ten colors. So there are 1000 unique bugs possible, and I want to track how many of each of these types the player has. The nested dictionary looks like:
var colorsDict: [String : Int]
var patternsDict: [String : Any] // [String : colorsDict]
var bugsDict: [String : Any] // [String : patternsDict]
I do not get any errors or complaints with this syntax.
When I want to increment the player's bug collection though, doing this:
bugs["ladybug"]["spotted"]["red"]++
I get this error: String is not convertible to 'DictionaryIndex< String, Any >' with the error's carrot under the first string.
Another similar post suggested using "as Any?" in the code, but the OP of that post only had a dictionary one deep so could do that easily with: dict["string"] as Any? ...
I am not sure how to do this with a multilevel dictionary. Any help would be appreciated.
When working with dictionaries you have to remember that a key might not exist in the dictionary. For this reason, dictionaries always return optionals. So each time you access the dictionary by key you have to unwrap at each level as follows:
bugsDict["ladybug"]!["spotted"]!["red"]!++
I presume you know about optionals, but just to be clear, use the exclamation mark if you are 100% sure the key exists in the dictionary, otherwise it's better to use the question mark:
bugsDict["ladybug"]?["spotted"]?["red"]?++
Addendum: This is the code I used for testing in playground:
var colorsDict = [String : Int]()
var patternsDict = [String : [String : Int]] ()
var bugsDict = [String : [String : [String : Int]]] ()
colorsDict["red"] = 1
patternsDict["spotted"] = colorsDict
bugsDict["ladybug"] = patternsDict
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 1
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 2
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 3
bugsDict["ladybug"]!["spotted"]!["red"]! // Prints 4
Another option: You could try calling dict.value( forKeyPath: "ladybug.spotted.red" )!
So I just tried this with Swift 5:
import Foundation
var d = [ "ladybug" : [ "spotted" : [ "red" : 123 ] ] ] as [String:Any]
(d as NSDictionary).value(forKeyPath: "ladybug.spotted.red")
and it works, but this is probably the best way:
d["ladybug"]?["spotted"]?["red"]
I had the same issue, where I wanted to get boolValue nested in dictionary.
{
"Level1": {
"leve2": {
"code": 0,
"boolValue": 1
}
}
}
I tried a lot of solution but those didn't worked for me as i was missing type casting. So I used following code to get the boolValue from json, where json is a nested dictionary of type [String:Any].
let boolValue = ((json["level1"]
as? [String: Any])?["level2"]
as? [String: Any])?["boolValue"] as? Bool
My primary use case was reading ad-hoc values from a deep dictionary. None of the answers given worked for me in my Swift 3.1 project, so I went looking and found Ole Begemann's excellent extension for Swift dictionaries, with a detailed explanation on how it works.
I've made a Github gist with the Swift file I made for using it, and I welcome feedback.
To use it, you can add the Keypath.swift into your project, and then you can simply use a keyPath subscript syntax on any [String:Any] dictionary as follows.
Considering you have a JSON object like so:
{
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
stored in a dictionary var dict:[String:Any]. You could use the following syntax to get to the various depths of the object.
if let name = data[keyPath:"name"] as? String{
// name has "John"
}
if let age = data[keyPath:"age"] as? Int{
// age has 30
}
if let car1 = data[keyPath:"cars.car1"] as? String{
// car1 has "Ford"
}
Note that the extension supports writing into nested dictionaries as well, but I haven't yet used this.
I still haven't found a way to access arrays within dictionary objects using this, but it's a start! I'm looking for a JSON Pointer implementation for Swift but haven't found one, yet.
If it's only about retrieval (not manipulation) then here's a Dictionary extension for Swift 3 (code ready for pasting into Xcode playground) :
//extension
extension Dictionary where Key: Hashable, Value: Any {
func getValue(forKeyPath components : Array<Any>) -> Any? {
var comps = components;
let key = comps.remove(at: 0)
if let k = key as? Key {
if(comps.count == 0) {
return self[k]
}
if let v = self[k] as? Dictionary<AnyHashable,Any> {
return v.getValue(forKeyPath : comps)
}
}
return nil
}
}
//read json
let json = "{\"a\":{\"b\":\"bla\"},\"val\":10}" //
if let parsed = try JSONSerialization.jsonObject(with: json.data(using: .utf8)!, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<AnyHashable,Any>
{
parsed.getValue(forKeyPath: ["a","b"]) //-> "bla"
parsed.getValue(forKeyPath: ["val"]) //-> 10
}
//dictionary with different key types
let test : Dictionary<AnyHashable,Any> = ["a" : ["b" : ["c" : "bla"]], 0 : [ 1 : [ 2 : "bla"]], "four" : [ 5 : "bla"]]
test.getValue(forKeyPath: ["a","b","c"]) //-> "bla"
test.getValue(forKeyPath: ["a","b"]) //-> ["c": "bla"]
test.getValue(forKeyPath: [0,1,2]) //-> "bla"
test.getValue(forKeyPath: ["four",5]) //-> "bla"
test.getValue(forKeyPath: ["a","b","d"]) //-> nil
//dictionary with strings as keys
let test2 = ["one" : [ "two" : "three"]]
test2.getValue(forKeyPath: ["one","two"]) //-> "three"
Unfortunately none of these methods worked for me, so I built my own to use a simple string path like "element0.element1.element256.element1", etc. Hope this save a time for others. (just use a dots between name of elements in string)
Json example:
{
"control": {
"type": "Button",
"name": "Save",
"ui": {
"scale": 0.5,
"padding": {
"top": 24,
"bottom": 32
}
}
}
}
Step 1, convert json String to Dictionary
static func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
Step 2, helper to get a nested objects
//path example: "control.ui.scale"
static func getDictValue(dict:[String: Any], path:String)->Any?{
let arr = path.components(separatedBy: ".")
if(arr.count == 1){
return dict[String(arr[0])]
}
else if (arr.count > 1){
let p = arr[1...arr.count-1].joined(separator: ".")
let d = dict[String(arr[0])] as? [String: Any]
if (d != nil){
return getDictValue(dict:d!, path:p)
}
}
return nil
}
Step 3, use helper
let controlScale = getDictValue(dict:dict, path: "control.ui.scale") as! Double?
print(controlScale)
let controlName = getDictValue(dict:dict, path: "control.name") as! String?
print(controlName)
Returns
0.5
Save
The Swift 4 default: subscript for Dictionaries makes makes updating values in nested Dictionaries much more concise.
Get and Set a default value rather than dealing with optionals:
var dict = [String : [String : String]]()
dict["deep", default: [:]]["nested"] = "dictionary"
print(dict)
// ["deep": ["nested": "dictionary"]]
https://swift.org/blog/dictionary-and-set-improvements/
You can use this extension:
extension Dictionary {
/// - Description
/// - The function will return a value on given keypath
/// - if Dictionary is ["team": ["name": "KNR"]] the to fetch team name pass keypath: team.name
/// - If you will pass "team" in keypath it will return team object
/// - Parameter keyPath: keys joined using '.' such as "key1.key2.key3"
func valueForKeyPath <T> (_ keyPath: String) -> T? {
let array = keyPath.components(separatedBy: ".")
return value(array, self) as? T
}
/// - Description:"
/// - The function will return a value on given keypath. It keep calling recursively until reach to the keypath. Here are few sample:
/// - if Dictionary is ["team": ["name": "KNR"]] the to fetch team name pass keypath: team.name
/// - If you will pass "team" in keypath it will return team object
/// - Parameters:
/// - keys: array of keys in a keypath
/// - dictionary: The dictionary in which value need to find
private func value(_ keys: [String], _ dictionary: Any?) -> Any? {
guard let dictionary = dictionary as? [String: Any], !keys.isEmpty else {
return nil
}
if keys.count == 1 {
return dictionary[keys[0]]
}
return value(Array(keys.suffix(keys.count - 1)), dictionary[keys[0]])
}
}
Usage:
let dictionary = ["values" : ["intValue": 3]]
let value: Int = dictionary.valueForKeyPath("values.intValue")
You can use the following syntax on Swift 3/4:
if let name = data["name"] as? String {
// name has "John"
}
if let age = data["age"] as? Int {
// age has 30
}
if let car = data["cars"] as? [String:AnyObject],
let car1 = car["car1"] as? String {
// car1 has "Ford"
}
Yet another approach using various overloaded Dictionary subscript implementations:
let dict = makeDictionary(fromJSONString:
"""
{
"control": {
"type": "Button",
"name": "Save",
"ui": {
"scale": 0.5,
"padding": {
"top": 24,
"bottom": 32
}
}
}
}
""")!
dict[Int.self, ["control", "ui", "padding", "top"]] // 1
dict[Int.self, "control", "ui", "padding", "top"] // 2
dict[Int.self, "control.ui.padding.top"] // 3
And the actual implementations:
extension Dictionary {
// 1
subscript<T>(_ type: T.Type, _ pathKeys: [Key]) -> T? {
precondition(pathKeys.count > 0)
if pathKeys.count == 1 {
return self[pathKeys[0]] as? T
}
// Drill down to the innermost dictionary accessible through next-to-last key
var dict: [Key: Value]? = self
for currentKey in pathKeys.dropLast() {
dict = dict?[currentKey] as? [Key: Value]
if dict == nil {
return nil
}
}
return dict?[pathKeys.last!] as? T
}
// 2. Calls 1
subscript<T>(_ type: T.Type, _ pathKeys: Key...) -> T? {
return self[type, pathKeys]
}
}
extension Dictionary where Key == String {
// 3. Calls 1
subscript<T>(_ type: T.Type, _ keyPath: String) -> T? {
return self[type, keyPath.components(separatedBy: ".")]
}
}
func makeDictionary(fromJSONString jsonString: String) -> [String: Any]? {
guard let data = jsonString.data(using: .utf8)
else { return nil}
let ret = try? JSONSerialization.jsonObject(with: data, options: [])
return ret as? [String: Any]
}
Yet another Dictionary extension
public extension Dictionary where Key: Hashable, Value: Any {
subscript(keyPath path: String) -> Value? {
self[keyPath: path.components(separatedBy: ".").compactMap { $0 as? Key }]
}
private subscript(keyPath keys: [Key]) -> Value? {
var keys = keys
switch keys.first {
case .some(let key) where keys.count == 1:
return self[key]
case .some(let key) where keys.count > 1:
keys.removeFirst()
return (self[key] as? Dictionary<Key, Value>)?[keyPath: keys]
default:
return nil
}
}
}
Test code:
let dict: [String: Any] = [
"user": [
"name": "Giorgio",
"surname": "Baldazzi"
]
]
let keyPath = "user.name"
print(String(describing: dict[keyPath: keyPath]))

Computed property in Ember based on async data

I'm trying to use a computed property based on the values from an async, hasMany model property, but cannot get it to display in my view.
MyApp.Foo = DS.Model.extend({
title: DS.attr('string'),
peeps: DS.hasMany('peep', { async: true });
});
MyApp.Peep = DS.Model.extend({
name: DS.attr('string'),
email: DS.attr('string')
});
MyApp.Foo.FIXTURES = [
{ id: 1, title: 'nice', peeps: [1,2] }
];
MyApp.Peep.FIXTURES = [
{ id: 1, name: 'mypeep', email: 'peep#example.com' },
{ id: 2, name: 'mypeep2', email: 'peep2#example.com' }
];
MyApp.FooController = EmberObjectController.extend({
showPeeps: function() {
// This one works for this test data.
// return [{name: 'baz', email: 'bar'}];
var peepList = this.get('content.peeps.[]').then(function(c) {
// This one does not work, even for this test data.
return {name: 'baz', email: 'bar'}];
});
}.property('content.peeps.[]');
});
In my view, something along the lines of:
{#each peep in controller.showPeeps}}{{peep.name}}{{/each}}
I can see all the data in the "then()" using console.log(), and as it indicates in the code comments, it works if I take the return out of the "then()" - but then the real data is empty because it is returned as async. If I try to make it non-async, I get
Uncaught TypeError: Cannot call method 'resolve' of undefined
I've tried many variants of the computed property code (using #each, using model.peeps - all of which correctly show the data in console.log(), but not in the view. In the view, it is always undefined unless I just return dummy data outside of the then() - which displays correctly)
What am I missing?
Don't treat the hasMany relationship as a promise, treat it as an array. That's the whole point of DS.PromiseArray. If you just want the users, don't even bother with the computed property, just use peeps in your template. But, if you need to convert the data somehow, use map.
showPeeps: function() {
return this.get('peeps').map(function(peep) {
return { name: peep.name, email: peep.email };
});
}.property('peeps.#each')
Also, don't watch the [] property. That only updates when an item is added or removed from the array. Your array contents aren't changing, the contents of the contents are changing. You should watch the #each property instead. You also don't need to add [] to the end of the property name, and you don't need to prefix the property with content..

Resources