• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Type-Safe Associated Objects in Swift

Type-Safe Associated Objects in Swift

Version Platforms Languages License Carthage compatible CocoaPods

TSAO is an implementation of type-safe associated objects in Swift. Objective-C associated objects are useful, but they are also untyped; every associated object is only known to be id at compile-time and clients must either test the class at runtime or rely on it being the expected type.

Swift allows us to do better. We can associate the value type with the key used to reference the value, and this lets us provide a strongly-typed value at compile-time with no runtime overheadยน. What's more, it allows us to store value types as associated objects, not just object types, by transparently boxing the value (although this involves a heap allocation). We can also invert the normal way associated objects work and present this type-safe adaptor using the semantics of a global map from AnyObject to ValueType.

It's also possible to specify the association policy. For all values, atomic/nonatomic retain is supported. For class values, assign is also supported. And for NSCopying values, atomic/nonatomic copy is supported.

To properly use this library, the AssocMap values you create should be static or global values (they should live for the lifetime of the program). You aren't required to follow this rule, but any AssocMaps you discard will end up leaking an object (this is the only way to ensure safety without a runtime penalty).

ยน It does require a type-check, but the optimizer should in theory be able to remove this check.

Usage example

import TSAO

// create a new map that stores the value type Int
// note how this is a global value, so it lives for the whole program
let intMap = AssocMap<Int>()

// fetch the associated object from `obj` using `intMap`
func lookup_int_object(obj: AnyObject) -> Int? {
    // The subscript getter returns a value of type `Int?` so no casting is necessary
    return intMap[obj]
}

// set the associated object for `intMap` on `obj`
func set_int_object(obj: AnyObject, val: Int?) {
    // The subscript setter takes an `Int?` directly, trying to pass
    // a value of any other type would be a compile-time error
    intMap[obj] = val
}

// This map stores values of type NSString with the nonatomic copy policy
let strMap = AssocMap<NSString>(copyAtomic: false)

// fetch the associated object from `obj` using `strMap`
func lookup_str_object(obj: AnyObject) -> NSString? {
    // The subscrip getter returns a value of type `NSString?`
    return strMap[obj]
}

// set the associated object for `strMap` on `obj`
func set_str_object(obj: AnyObject, val: NSString?) {
    // The subscript setter takes an `NSString?` directly, trying to pass
    // an `Int?` like we did with `intMap` would be a compile-time error
    strMap[obj] = val
}

More Repositories

1

nix-env.fish

Nix environment setup for the fish shell
Shell
279
star
2

feedparser

A Cocoa RSS/Atom parser for Mac OS X and the iPhone
Objective-C
269
star
3

rust-lua

Safe Rust bindings to Lua 5.1
Rust
127
star
4

vim-swift

Swift filetype plugin for Vim
Vim Script
121
star
5

NotificationWatcher

A Mac OS X application that monitors all workspace and distributed notifications
Objective-C
118
star
6

Tomorrowland

Lightweight Promises for Swift & Obj-C
Swift
117
star
7

textmate-bundles

git-svn clone of the TextMate Bundles repo
Ruby
89
star
8

alfred-rs

Helper library for writing Alfred workflows in Rust
Rust
47
star
9

dcpu16

A cycle-accurate implementation of @notch's dcpu-16 in Go
Go
41
star
10

git-scripts

A collection of scripts for use with git
Shell
36
star
11

FontLabel

A project that includes a module for drawing arbitrary .ttf fonts for iPhone.
Objective-C
33
star
12

osx-plist

Ruby osx/plist extension for reading/writing property lists
Ruby
29
star
13

go-osx-plist

CoreFoundation Property List support for Go
Go
29
star
14

emojienabler

iPhone app to enable Emoji on non-jailbroken iPhones
Objective-C
24
star
15

Xcode-File-Templates

A collection of file templates for Xcode
Objective-C
17
star
16

fmdb

Git mirror of the flycode fmdb svn repo
Objective-C
15
star
17

six

A fork of CyBot (http://repo.or.cz/w/six.git)
Ruby
15
star
18

projecteuler-ocaml

Solutions to the Project Euler problems in OCaml
OCaml
12
star
19

.vim

My ~/.vim directory
Vim Script
12
star
20

TinEye-for-Safari

A Safari Extension to search for images on TinEye
JavaScript
10
star
21

GoogleImageSearch-for-Safari

A Safari Extension to search for images on Google Image Search
JavaScript
10
star
22

swift-demangle.alfredworkflow

Alfred Workflow for demangling Swift symbols
Rust
9
star
23

flip-text.alfredworkflow

Alfred workflow for flipping text upside-down
Rust
9
star
24

swift-deque

A double-ended queue collection for Swift
Swift
8
star
25

amatch

String matching extension for Ruby (fork of svn repo)
C
8
star
26

fzf-git.fish

Fish shell plugin for fzf git bindings
Shell
8
star
27

Newzbin-SABnzbd.safariextz

Safari extension for adding better integration between Newzbin and SABnzbd+
JavaScript
7
star
28

slack-groupme-gateway

Really hacky gateway between Slack and GroupMe
CoffeeScript
6
star
29

rust-irclib

IRC library for Rust
Rust
6
star
30

alfred-install-workflow

Helper script for installing workflows from development sources
Shell
5
star
31

goirc

Go library for implementing IRC clients/bots
Go
5
star
32

Ponify-Safari

Port of Ponify for Safari
JavaScript
5
star
33

call_with_locals

Ruby library to add Proc#call_with_locals
Ruby
4
star
34

OglafSecrets

Safari Extension to reveal secret messages in Oglaf comics
JavaScript
4
star
35

rust-ircbot

Simple IRC bot written in Rust
Rust
4
star
36

projecteuler-haskell

Project Euler problems solved with Haskell
Haskell
3
star
37

dcpu16.go

Placeholder to redirect to dcpu16
3
star
38

typo-wordpress

Conversion script from Typo to WordPress
Ruby
3
star
39

vim-swift-extra

All the non-syntax-coloring bits from lilyball/vim-swift
Vim Script
3
star
40

decafbland-limechat

The DecafBland theme for LimeChat
3
star
41

yubnubsearch

YubNubSearch is a SIMBL plugin that turns Safari's Google search field into a YubNub search field
Perl
3
star
42

My-Reddit-Ponies

Port of My Reddit Ponies to Safari
Go
3
star
43

nnwstyle

NetNewsWire Stylesheets repository (rails app)
Ruby
2
star
44

My-Little-Spoilers

Safari Extension port of the My Little Spoilers userscript
JavaScript
2
star
45

openri

Quickly open documentation for your Gems in your browser
Ruby
2
star
46

MLP-Easy-Emotes

Port of MLP Easy Emotes to Safari
Go
2
star
47

voidbot

IRC bot written in Go based on kballard/goirc
Go
2
star
48

dcbot

Direct Connect bot written in Ruby
Ruby
2
star
49

alt-q.fish

Fish plugin for alt-q and alt-g keybindings
Shell
1
star
50

alfred-unicode-info

Alfred workflow for providing information about Unicode characters
Rust
1
star
51

go-alfred

Go package for Alfred 2 workflow script filters
Go
1
star
52

storyboard-color-value.alfredworkflow

Alfred workflow to convert Xcode storyboard XML colors into hex colors
Rust
1
star
53

rust-termbox

A Rust wrapper for the termbox library.
Rust
1
star