Ivars
@dynamicMemberLookup
public struct Ivars<IvarType>
A wrapper around an object for accessing Objective-C instance variables of
type IvarType on it.
This type supports dynamic member lookup, which means that ivars can be accessed as if they were members of the type. Note that if an ivar with the given name does not exist, the dynamic member access will result in a crash.
To access a pointer to the ivar instead, use
Ivars.withIvar(_:_:).To fail gracefully if the ivar does not exist, use
Ivars.subscript(safelyAccessing:).To access an ivar with a name that clashes with an actual member on this type, it is possible to use
Ivars.subscript(dynamicMember:)directly.To access a weak ivar, pass
.weakas the second argument to theIvarsinitializer. TheIvarTypemust correspond to an optional of a class (e.g.Ivars<NSString?>).
Example
let object = MyObject(foo: 5)
Ivars<Int>(object)._foo // 5
Ivars(object)._foo = 7
print(object.foo) // 7
-
Construct a new instance of
Ivarsfor accessing Objective-C instance variables onobject.Declaration
Swift
public init(_ object: AnyObject)Parameters
objectThe object on which ivars are to be accessed.
-
Access an Objective-C instance variable on the object.
It is safe to assume that
bodywill be called on all execution paths.Warning
Thepointerargument should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
public func withIvar<Result>( _ name: String, _ body: (_ pointer: UnsafeMutablePointer<IvarType>?) throws -> Result ) rethrows -> ResultParameters
nameThe name of the instance variable to access.
bodyA block which receives a pointer to the ivar, within which it may be read or written to.
pointerThe pointer to the ivar, or
nilifobjectdoes not have an ivar with the providedname.Return Value
The value returned from
body. -
Access an Objective-C instance variable on the object, failing gracefully if the instance variable does not exist.
Note
If the setter is passed a value of
nil, it will do nothing.Declaration
Swift
public subscript(safelyAccessing ivarName: String) -> IvarType? { get nonmutating set }Parameters
ivarNameThe name of the instance variable to access.
Return Value
The value of the instance variable, or
nilif there is no ivar with the given name. -
Access an Objective-C instance variable on the object.
Precondition
The ivar
ivarNamemust be present on the object. If an ivar with the given name is not present, using this subscript will result in a crash.To fail gracefully, use
Ivars.subscript(safelyAccessing:)orIvars.withIvar(_:_:).Declaration
Swift
public subscript(dynamicMember ivarName: String) -> IvarType { get nonmutating set }Parameters
ivarNameThe name of the instance variable to access.
-
Construct a new instance of
Ivarsfor accessing weak Objective-C instance variables onobject.This initializer requires that
IvarTypebe an optional object type.For example, to access a weak string ivar
_fooonobj, you could useIvars<NSString?>(obj, .weak)._foo.Declaration
Swift
public init(_ object: AnyObject, _ storage: WeakStorage)Parameters
objectThe object on which ivars are to be accessed.
storageSet this to
WeakStorage.weak. Indicates that the instance variable is weak.
View on GitHub
Install in Dash