Custom Swift Encoder/Decoder for the Strings Resource Format

A bit late to the party here but I feel this might be helpful/informative to others given the question high vote count. (But I won’t really get into the actual usefulness of such code in practice—please check the comments above for that.) Unfortunately, given the coding stack flexibility and type-safeness, implementing a new encoding and … Read more

Declarations in extensions cannot override yet error in Swift 4

It will work if you make the base implementation @objc. See Hamish’s answer for a detailed explanation about the internals. Overriding methods declared in extensions is a bit tricky to do correctly. Objective-C supports it, but it’s not absolutely safe. Swift aims to do it better. The proposal is not completed yet. Current version of … Read more

Codable enum with default case in Swift 4

You can extend your Codable Type and assign a default value in case of failure: enum Type: String { case text, image, document, profile, sign, inputDate = “input_date”, inputText = “input_text” , inputNumber = “input_number”, inputOption = “input_option”, unknown } extension Type: Codable { public init(from decoder: Decoder) throws { self = try Type(rawValue: decoder.singleValueContainer().decode(RawValue.self)) … Read more

Using codable with value that is sometimes an Int and other times a String

struct GeneralProduct: Codable { var price: Double? var id: String? var name: String? private enum CodingKeys: String, CodingKey { case price = “p”, id = “i”, name = “n” } init(price: Double? = nil, id: String? = nil, name: String? = nil) { self.price = price self.id = id self.name = name } init(from decoder: … Read more

Unable to access Swift 4 class from Objective-C: “Property not found on object of type”

The rules for exposing Swift code to Objective-C have changed in Swift 4. Try this instead: @objc var foobar = true As an optimization, @objc inference have been reduced in Swift 4. For instance, a property within an NSObject-derived class, such as your TestViewController, will no longer infer @objc by default (as it did in … Read more

‘subscript’ is unavailable: cannot subscript String with a CountableClosedRange, see the documentation comment for discussion

If you want to use subscripts on Strings like “palindrome”[1..<3] and “palindrome”[1…3], use these extensions. Swift 4 extension String { subscript (bounds: CountableClosedRange<Int>) -> String { let start = index(startIndex, offsetBy: bounds.lowerBound) let end = index(startIndex, offsetBy: bounds.upperBound) return String(self[start…end]) } subscript (bounds: CountableRange<Int>) -> String { let start = index(startIndex, offsetBy: bounds.lowerBound) let end … Read more

Encode nil value as null with JSONEncoder

Yes, but you’ll have to write your own encode(to:) implementation, you can’t use the auto-generated one. struct Foo: Codable { var string: String? = nil var number: Int = 1 func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(number, forKey: .number) try container.encode(string, forKey: .string) } } Encoding an optional directly … Read more

Module compiled with Swift 4.0 cannot be imported in Swift 4.0.1

Update: For release versions of Xcode: This error (and similar errors involving Swift 4.1, 4.2, etc.) will occur when opening a project with Xcode 9.1, 9.2, 9.3, 9.4, 10, etc. that uses frameworks that were built with earlier Xcode tools. To fix the issue, update and rebuild your frameworks using Carthage ( carthage update –platform … Read more