Code-free
Code-free

Take Delight in Small Joys

Extensions everywhere

Russ Bishop
November 08, 2018

This is a small post about a small joy. I really enjoy how natural extensions can feel in Swift.

I consider it quite unfortunate that UnsafeMutableRawBufferPointer.baseAddress is optional. It makes that type so ugly to use in practice. I also dislike having to specify alignment on allocation; a sensible default is Int.bitWidth / 8 on . . .

Read More

Fixed-sized Arrays

Who doesn't like a tuple with 1024 elements?

Russ Bishop
October 30, 2018

Let's say we want to statfs() a mount point to determine which BSD device name it belongs to. In english, that means discover that /Volumes/MyDisk comes from /dev/disk6s2.

struct statfs fsinfo;
if (statfs(path, &fsinfo) != 0) {
 //error
}

The equivalent Swift code, along with a POSIX error helper:

func  . . .

Read More

SingleValueCodable

A simple exercise in leverage

Russ Bishop
October 24, 2017

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 . . .

Read More

AMD System V ABI Reference

Russ Bishop
July 18, 2017

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 . . .

Read More

MemoryLayout

Size & Stride

Russ Bishop
June 16, 2017

A quick word of warning to those of you using UnsafePointer and MemoryLayout.

  • The size of a type tells you how many bytes it takes to hold that type in memory.
  • The stride of 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 . . .

Read More

Swift REPL Targets

Russ Bishop
January 22, 2017

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

. . .

Read More

Improving Optionals

All problems can be solved by adding another protocol

Russ Bishop
January 19, 2017

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 . . .

Read More

Archive
   Subscribe by email and never miss a post.