swift4
Make UIColor Codable
If you care only about the 4 color components this is a simple solution using a wrapper struct struct Color : Codable { var red : CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, alpha: CGFloat = 0.0 var uiColor : UIColor { return UIColor(red: red, green: green, blue: blue, alpha: alpha) … Read more
Swift 4 ,must be used from main thread only warning
You’re making this call on a background queue. To fix, try something like… public var context: NSManagedObjectContext DispatchQueue.main.async { var appDelegate = UIApplication.shared.delegate as! AppDelegate context = appDelegate.persistentContainer.viewContext } Although this is a pretty bad way to do this… you’re using your App Delegate as a global variable (which we all know is bad!) You should … 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
Decoding a JSON without keys in Swift 4
If the structure stays the same, you can use this Decodable approach. First create a decodable Model like this: struct MyModel: Decodable { let firstString: String let stringArray: [String] init(from decoder: Decoder) throws { var container = try decoder.unkeyedContainer() firstString = try container.decode(String.self) stringArray = try container.decode([String].self) } } Or if you really want to … Read more
Swift 4 ‘substring(from:)’ is deprecated: Please use String slicing subscript with a ‘partial range from’ operator
Follow the below example to fix this warning: Supporting examples for Swift 3, 4 and 5. let testStr = “Test Teja” let finalStr = testStr.substring(to: index) // Swift 3 let finalStr = String(testStr[..<index]) // Swift 4 let finalStr = testStr.substring(from: index) // Swift 3 let finalStr = String(testStr[index…]) // Swift 4 //Swift 3 let finalStr … Read more
Can’t import packages using Swift 4 Package Manager
Turns out I had to also include the dependencies into the .target of the Package.swift: .target(named: “sampleproject”, dependencies: [“Kitura”, “Alamofire”]) and build the project again.
How to conform UIImage to Codable?
Properly the easiest way is to just make the property Data instead of UIImage like this: public struct SomeImage: Codable { public let photo: Data public init(photo: UIImage) { self.photo = photo.pngData()! } } Deserialize the image: UIImage(data: instanceOfSomeImage.photo)!
Closure tuple does not support destructuring in Xcode 9 Swift 4
Let’s start with the definition of flatMap for a dictionary which is the following: func flatMap(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult] You see that the transform closure takes only one parameter of type Element where Element is just a typealias for a tuple: public typealias Element = (key: Key, value: Value) So … Read more