How do I make an enum Decodable in Swift?

It’s pretty easy, just use String or Int raw values which are implicitly assigned. enum PostType: Int, Codable { case image, blob } image is encoded to 0 and blob to 1 Or enum PostType: String, Codable { case image, blob } image is encoded to “image” and blob to “blob” This is a simple … Read more

Swift: #warning equivalent

Edit As of Swift 4.2, language level support is available for both build warnings and errors. #warning(“Warning description”) #error(“Throws a build error”) Original Answer Quick, dirty, and oh so elegantly simple all at the same time. // Description of what you need to fix var FIX_ME__🛠🛠🛠: AnyObject Throws a warning that ‘FIX_ME__🛠🛠🛠’ was never used. … Read more

Is there a way to reset the app between tests in Swift XCTest UI?

You can add a “Run Script” phase to build phases in your test target to uninstall the app before running unit tests against it, unfortunately this is not between test cases, though. /usr/bin/xcrun simctl uninstall booted com.mycompany.bundleId Update Between tests, you can delete the app via the Springboard in the tearDown phase. Although, this does … Read more

Convert a Date (absolute time) to be sent/received across the network as Data in Swift?

You can send your Date converting it to Data (8-bytes floating point) and back to Date as follow: extension Numeric { var data: Data { var source = self return .init(bytes: &source, count: MemoryLayout<Self>.size) } init<D: DataProtocol>(_ data: D) { var value: Self = .zero let size = withUnsafeMutableBytes(of: &value, { data.copyBytes(to: $0)} ) assert(size … Read more

What happened to the UIView?() constructor in Swift 3.0?

The following does initialise x to nil, the brackets are entirely superfluous. var x: UIView? return x == nil Will return true Check out the developer docs for more information. https://developer.apple.com/library/content//documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html If you define an optional variable without providing a default value, the variable is automatically set to nil for you: var surveyAnswer: String? // … Read more

What is the cause of this type error?

The problem is that when you don’t explicitly supply the generic parameter type of ZipList when you refer to it, the compiler will try and infer it for you – which it doesn’t always get correct. As you’re already inside a ZipList<A> class, the compiler will try and infer ZipList to be ZipList<A> when you … Read more