SingleValueCodable
A simple exercise in leverage
The new Codable protocol is flexible enough to allow a different encoded representation from the in-memory representation which is a nice property to have in a serialization mechanism. Today I'm going to build SingleValueCodable to automate that work when dealing with RawRepresentable types.
The Setup
I want to encode . . .
AMD System V ABI Reference
An ABI reference for quick lookup. If you crash or just want to step through disassembly, check the contents of the registers to see where parameters are being passed.
| Arg | Register | Notes |
|---|---|---|
| 1 | RDI |
Usually self or address to hold return value |
| 2 | RSI |
Usually _cmd but may . . . |
MemoryLayout
Size & Stride
A quick word of warning to those of you using UnsafePointer and MemoryLayout.
- The
sizeof a type tells you how many bytes it takes to hold that type in memory. - The
strideof a type tells you how far apart each instance of the type is in memory.
If you are doing pointer arithmetic on UnsafePointer then Swift . . .
Swift REPL Targets
Have you seen the message error: repl.swift:2:31: error: '<type>' is only available on OS X <version> or newer? In current releases of Xcode, the Swift REPL uses a hard-coded 10.9 target instead of the current OS.
The solution is fairly easy:
xcrun swift -target x86_64-apple-macosx10.12
. . .
Improving Optionals
All problems can be solved by adding another protocol
Optionals are great. I've tracked down too many bugs to Objective-C's magical "messages to nil return nil"; I have no interest in going back.
But sometimes there are things you want to do with optionals or when optionals are of a specific type. Here are some of my favorites:
isNilOrEmpty
Sometimes . . .
Quick: Easy DispatchTime
Expressin' me some literals
Here's a quick bonus post. I find using DispatchTime a bit less convenient than it should be. In a GUI application I almost always just want to specify a TimeInterval, aka number of whole or fractional seconds.
Thankfully Swift extensions can grant my heart's desire:
extension DispatchTime: ExpressibleByIntegerLiteral { . . .Mixing Initializers
init? init! init throws
Today is a really simple post but something people struggle with, often in the context of extensions or interacting with RawRepresentable enums.
Can't delegate to failable initializer
Let's say you have a type with a failable initializer. Now you want to extend that type to support deserializing from JSON. You throw . . .