Any when decoding JSON with Codable?

I ended up having to implement my own class to encode/decode Any values. It’s not pretty, but it seems to work: class JSONAny: Codable { public let value: Any static func decodingError(forCodingPath codingPath: [CodingKey]) -> DecodingError { let context = DecodingError.Context(codingPath: codingPath, debugDescription: “Cannot decode JSONAny”) return DecodingError.typeMismatch(JSONAny.self, context) } static func encodingError(forValue value: Any, … Read more

Converting from Swift string to const char*

You should be able to pass a String directly to a C function expecting const char * and it will be automatically converted to a null-terminated UTF-8 string: let string = “string” let node = artnet_new(string, 1) See Interacting with C APIs for more information. Here is the relevant excerpt: When a function is declared … Read more

Using Codable on a dynamic type/object

Since you linked to my answer to another question, I will expand that one to answer yours. Truth is, all keys are known at runtime if you know where to look: struct GenericCodingKeys: CodingKey { var intValue: Int? var stringValue: String init?(intValue: Int) { self.intValue = intValue; self.stringValue = “\(intValue)” } init?(stringValue: String) { self.stringValue … Read more

HMAC SHA256 in Swift 4

If you target iOS 13.0+ or macOS 10.15+, use Apple’s CryptoKit import CryptoKit let secretString = “my-secret” let key = SymmetricKey(data: Data(secretString.utf8)) let string = “An apple a day keeps anyone away, if you throw it hard enough” let signature = HMAC<SHA256>.authenticationCode(for: Data(string.utf8), using: key) print(Data(signature).map { String(format: “%02hhx”, $0) }.joined()) // 1c161b971ab68e7acdb0b45cca7ae92d574613b77fca4bc7d5c4effab89dab67

Failed to load optimized model – GoogleMaps SDK IOS

If you have already double-checked the basic setup for the “google maps ios-sdk” with an APIkey for your app’s bundle identifier here and still have the same problem then probably you have not enabled the google maps API. Go to your app-project’s dashboard on https://console.developers.google.com and click the “ENABLE APIS AND SERVICES”. There, under the … Read more

Swift 4 Conversion error – NSAttributedStringKey: Any

Why you got this error Previously, your attributes is defined as [String: Any], where the key comes from NSAttributedStringKey as a string or NSAttributedString.Key in Swift 4.2 During the migration, the compiler tries to keep the [String: Any] type. However, NSAttributedStringKey becomes a struct in swift 4. So the compiler tries to change that to … Read more

How to asynchronous load image from a web-server in UICollectionView using NSCache

Try this one it’s Working code (Swift 4). func NKPlaceholderImage(image:UIImage?, imageView:UIImageView?,imgUrl:String,compate:@escaping (UIImage?) -> Void){ if image != nil && imageView != nil { imageView!.image = image! } var urlcatch = imgUrl.replacingOccurrences(of: “https://stackoverflow.com/”, with: “#”) let documentpath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] urlcatch = documentpath + “https://stackoverflow.com/” + “\(urlcatch)” let image = UIImage(contentsOfFile:urlcatch) if image != nil … Read more