ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - lilyball/swift-tsao: Type-Safe Associated Objects in Swift
Type-Safe Associated Objects in Swift. Contribute to lilyball/swift-tsao development by creating an account on GitHub.
Visit Site

GitHub - lilyball/swift-tsao: Type-Safe Associated Objects in Swift

GitHub - lilyball/swift-tsao: 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 Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory