Property

@propertyWrapper
public struct Property<T>

A property wrapper which allows ClassHook types to add new properties to their hooked class.

This type is an ergonomic wrapper around Objective-C associated objects. The type of the associated object is the generic argument T.

If you are declaring a property on a ClassHook, you likely want to use this. Note that this property only works on types which conform to ClassHook.

Important

All properties with this attribute must have a default value.

Attributes

This property wrapper gives you the ability to specify attributes on the property, which are similar to those used by Objective-C’s @property. For a description of each attribute, see Property.Assign, Property.Atomicity, and Property.RetainOrCopy.

The default attributes are atomic and retain. Specifying assign will replace both defaults, and specifying an atomicity and/or retain policy will only override the respective default.

Example

@objcMembers class Person: NSObject {
    dynamic func sayHello() -> String {
        "hello"
    }
}

class PersonHook: ClassHook<Person> {
    @Property(.nonatomic) var x = 0

    func sayHello() -> String {
        x += 1
        return "Hi! I've been called \(x) time(s)"
    }
}

// later...

let alice = Person()
alice.sayHello() // Hi! I've been called 1 time(s)
alice.sayHello() // Hi! I've been called 2 time(s)

let bob = Person()
bob.sayHello() // Hi! I've been called 1 time(s)
bob.sayHello() // Hi! I've been called 2 time(s)
  • An enumeration indicating that the property should use the assign attribute.

    See more

    Declaration

    Swift

    public enum Assign
  • An enumeration containing attributes which allow specifying the atomicity of the association.

    See more

    Declaration

    Swift

    public enum Atomicity
  • An enumeration containing attributes which determine whether the property is retained or copied.

    See more

    Declaration

    Swift

    public enum RetainOrCopy