Swift Codable with dynamic keys

Assuming you left out the { and } that would surround this block and are required for this to be valid JSON, the following is the simplest solution to getting things parsed, you really don’t need to deal with CodingKey at all since your names match the keys in the JSON, so the synthesized CodingKey … Read more

Encode/Decode Array of Types conforming to protocol with JSONEncoder

The reason why your first example doesn’t compile (and your second crashes) is because protocols don’t conform to themselves – Tag is not a type that conforms to Codable, therefore neither is [Tag]. Therefore Article doesn’t get an auto-generated Codable conformance, as not all of its properties conform to Codable. Encoding and decoding only the … Read more

Swift 4 JSON Codable – value returned is sometimes an object, others an array

You might encapsulate the ambiguity of the result using an Enum with Associated Values (String and Array in this case), for example: enum MetadataType: Codable { case array([String]) case string(String) init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() do { self = try .array(container.decode(Array.self)) } catch DecodingError.typeMismatch { do { self = try … Read more

navigation bar rightbaritem image-button bug iOS 11

Reason The problem appears because from ios 11 UIBarButtonItem uses autolayout instead of dealing with frames. Solution You should add width constraint for this image-button if you use Xcode 9. button.widthAnchor.constraint(equalToConstant: 32.0).isActive = true button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true PS button is not UIBarButtonItem, it is UIButton inside UIBarButtonItem. You should set constraint not for UIBarButtonItem, … Read more

coca pod Chart not appearing (Swift4)

Try this one it’s is Working (Swift 4 Code). import UIKit import Charts class RootViewController: UIViewController { @IBOutlet weak var lineChartView: BarChartView! var days: [String]! override func viewDidLoad() { super.viewDidLoad() days = [“Monday”,”Tuesday”,”Wednesday”,”Thursday”] let task = [1.0,4.0,3.0,5.0] setChart(dataPoints: days, values: task) } func setChart(dataPoints : [String], values : [Double]){ lineChartView.noDataText = “Nothining to display” var … Read more

Difference between flatMap and compactMap in Swift

The Swift standard library defines 3 overloads for flatMap function: Sequence.flatMap<S>(_: (Element) -> S) -> [S.Element] Optional.flatMap<U>(_: (Wrapped) -> U?) -> U? Sequence.flatMap<U>(_: (Element) -> U?) -> [U] The last overload function can be misused in two ways: Consider the following struct and array: struct Person { var age: Int var name: String } let … Read more

What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?

There’s a subtle, but important difference between these two lines of code: // Exhibit 1 foo = try container.decode(Int?.self, forKey: .foo) // Exhibit 2 foo = try container.decodeIfPresent(Int.self, forKey: .foo) Exhibit 1 will parse: { “foo”: null, “bar”: “something” } but not: { “bar”: “something” } while exhibit 2 will happily parse both. So in … Read more