Utilities
- 
                  
                  A wrapper around an object for accessing Objective-C instance variables of type IvarTypeon 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 the- Ivarsinitializer. The- IvarTypemust correspond to an optional of a class (e.g.- Ivars<NSString?>).
 Example
 See morelet object = MyObject(foo: 5) Ivars<Int>(object)._foo // 5 Ivars(object)._foo = 7 print(object.foo) // 7DeclarationSwift @dynamicMemberLookup public struct Ivars<IvarType>
- 
                  
                  A wrapper enabling ergonomic dynamic Objective-C lookup. This type can be used as syntactic sugar to access ObjC classes and protocols by name, and also to call undeclared methods on them in a relatively type-safe manner. This type is inspired by mhdhejazi/Dynamic. Obtaining an InstanceUse Dynamic.TypeNameto get an instance ofDynamic. Accessing classes/protocols with dots in their name can be done in the same way: e.g.Dynamic.Foo.Bar.You can also get an instance of Dynamicby passing a string, e.g.Dynamic("Foo.Bar"), or even by passing an existing class:Dynamic(NSString.self).Getting the Underlying TypeIf the type name corresponds to an Objective-C class, it is possible to retrieve an AnyClassfrom the type using theclassproperty. For example,Dynamic.TypeName.classwill return the class named “TypeName”. To cast to a more specific superclass type, useas(type:), for exampleDynamic.PrivateViewType.as(type: UIView.self).If the type name corresponds to an Objective-C protocol, the protocolproperty returns the type as aProtocol.Calling Undeclared Instance MethodsThe first step to calling an undeclared method on the type (one that is not known to the Swift compiler) is to declare an “interface” with the method. An interface is simply an Objective-C protocol. Say you wanted to call -[MyClass someInstanceMethod], wheresomeInstanceMethodis not exposed to Swift. First, declare the interface:@objc protocol MyInterfaceA { func someInstanceMethod() }Then, use Dynamic.convert(_:to:)to “cast” the object to that interface:let obj = MyClass() let converted = Dynamic.convert(obj, to: MyInterfaceA.self)Swift sees convertedas a type conforming toMyInterface, and so it is now possible to directly call the method on it:converted.someInstanceMethod()If MyClassconforms toNSObject, a way to do this with even more syntactic sugar is to useNSObject.as(interface:):MyClass().as(interface: MyInterfaceA.self).someInstanceMethod()Calling Undeclared Class MethodsCalling class methods is similar to calling instance methods. Say you wanted to call +[MyClass someClassMethod]where neither the class nor the method is publicly exposed to Swift. First, declare an interface:@objc protocol MyInterfaceB { func someClassMethod() }Note that the method is declared as an instance method in the protocol, even though it is actually a class method. Next, obtain an instance of Dynamiccorresponding to the type, and useDynamic.as(interface:)to cast it to the interface.let converted = Dynamic.MyClass.as(interface: MyInterfaceB.self)Following this, it is possible to call methods on the type which have been declared on the interface: converted.someClassMethod()As with instance methods, class methods too have syntactic sugar available via an extension on NSObject. IfMyClasswas accessible in Swift and conformed toNSObject, the private method could be called as follows:MyClass.as(interface: MyInterfaceB.self).someClassMethod()Calling InitializersIn order to call a private initializer using Dynamic, declare an interface with the initializer method (escaping`init`with backticks if required). Then callalloc(interface:)on the class or an instance ofDynamiccorresponding to it, passing in the interface. Finally, call the initializer method on the returned object.For example, initializing MyClassusing-[MyClass initWithString:]would look like this:@objc protocol MyInterfaceC { func initWithString(_ string: String) } let obj = Dynamic.MyClass .alloc(interface: MyInterfaceC.self) .initWithString("hello")Since See moreMyClassconforms toNSObject, it is again possible to utilize additional syntactic sugar, in this case replacingDynamic.MyClasswith simplyMyClass.DeclarationSwift @dynamicMemberLookup public struct Dynamic
- 
                  
                  DeclarationSwift extension NSObject
 View on GitHub
            View on GitHub
           Install in Dash
            Install in Dash
           Utilities  Reference
      Utilities  Reference