@@ -30,13 +30,18 @@ extension NSObject {
3030/// Methods are hooked via replacing the implementation, instead of the usual exchange.
3131/// Supports both swizzling classes and individual objects.
3232final public class Interpose {
33+
3334 /// Stores swizzle hooks and executes them at once.
3435 public let `class` : AnyClass
3536 /// Lists all hooks for the current interpose class object.
3637 public private( set) var hooks : [ AnyHook ] = [ ]
3738
3839 /// If Interposing is object-based, this is set.
39- public let object : AnyObject ?
40+ public var object : AnyObject ? {
41+ objectContainer? . object
42+ }
43+
44+ internal let objectContainer : AnyObjectContainer ?
4045
4146 // Checks if a object is posing as a different class
4247 // via implementing 'class' and returning something else.
@@ -58,7 +63,7 @@ final public class Interpose {
5863 /// If `builder` is present, `apply()` is automatically called.
5964 public init ( _ `class`: AnyClass , builder: ( ( Interpose ) throws -> Void ) ? = nil ) throws {
6065 self . class = `class`
61- self . object = nil
66+ self . objectContainer = nil
6267
6368 // Only apply if a builder is present
6469 if let builder = builder {
@@ -67,9 +72,16 @@ final public class Interpose {
6772 }
6873
6974 /// Initialize with a single object to interpose.
70- public init ( _ object: NSObject , builder: ( ( Interpose ) throws -> Void ) ? = nil ) throws {
71- self . object = object
72- self . class = type ( of: object)
75+ public convenience init ( _ object: NSObject , builder: ( ( Interpose ) throws -> Void ) ? = nil ) throws {
76+ try self . init ( . strong( object) , builder: builder)
77+ }
78+
79+ /// Initialize with a single object to interpose.
80+ public init ( _ objectContainer: AnyObjectContainer , builder: ( ( Interpose ) throws -> Void ) ? = nil ) throws {
81+ self . objectContainer = objectContainer
82+ self . class = type ( of: objectContainer. object)
83+
84+ let object = objectContainer. object
7385
7486 if let actualClass = checkObjectPosingAsDifferentClass ( object) {
7587 if isKVORuntimeGeneratedClass ( actualClass) {
@@ -106,8 +118,8 @@ final public class Interpose {
106118 _ implementation: ( TypedHook < MethodSignature , HookSignature > ) -> HookSignature ? ) throws -> TypedHook < MethodSignature , HookSignature > {
107119
108120 var hook : TypedHook < MethodSignature , HookSignature >
109- if let object = self . object {
110- hook = try ObjectHook ( object : object , selector: selector, implementation: implementation)
121+ if let objectContainer = self . objectContainer {
122+ hook = try ObjectHook ( objectContainer : objectContainer , selector: selector, implementation: implementation)
111123 } else {
112124 hook = try ClassHook ( class: `class`, selector: selector, implementation: implementation)
113125 }
0 commit comments