the dynamic features of the Objective-C runtime. […] The point is that these problems will need solving in a possible future world without the Objective-C runtime. The answers don’t have to be the same answers as Objective- C – but they need to be good answers.” Brent Simmons
method and return YES. It will then try sending the message again. } // 2 - (id)forwardingTargetForSelector:(SEL)aSelector{ // Return an object that can handle the selector. } // 3 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{ // You need to implement this for the creation of an NSInvocation. } - (void)forwardInvocation:(NSInvocation *)invocation { // Invoke the selector on a target of your choice. [invocation invokeWithTarget:target]; }
property to an existing class. - Figure out what a class can do. - Add a method at runtime. - Forward a method to another target. - Replace / exchange an implementation. Dynamism!
property to an existing class. - Figure out what a class can do. - Add a method at runtime. - Forward a method to another target. - Replace / exchange an implementation. - Bind UI to Data. Dynamism!
property to an existing class. - Figure out what a class can do. - Add a method at runtime. - Forward a method to another target. - Replace / exchange an implementation. - Bind UI to Data. Dynamism!
Objective-C runtime, it does not guarantee dynamic dispatch. The Swift compiler may still devirtualize or inline member access to optimize the performance of your code, bypassing the Objective-C runtime. When you mark a member declaration with the dynamic modifier, access to that member is always dynamically dispatched. Declarations marked with the dynamic… [are] implicitly marked with the @objc attribute.”
property to an existing class. - Add a method at runtime. - Figure out what a class can do. - Forward a method to another target. - Replace / exchange an implementation. - Bind UI to Data. Dynamism!
{ // A chance to add the instance method and return YES. It will then try sending the message again. } // 2 override func forwardingTarget(for aSelector: Selector!) -> Any? { // Return an object that can handle the selector. } // 3 - NSInvocation is not available in Swift Forwarding *NSObject
("test_URLStrings", test_URLStrings), ("test_fileURLWithPath_relativeToURL", test_fileURLWithPath_relativeToURL), ("test_fileURLWithPath", test_fileURLWithPath), ("test_fileURLWithPath_isDirectory", test_fileURLWithPath_isDirectory), // Other tests go here ] }()
absolutely essential. I don’t think that it is interesting to provide the “equivalent” features to Objective-C, but I do think it is important that Swift be able to solve the same sorts of problems (including your list of Responder Chain, NSUndoManager, KVC/ KVO/Bindings, …) in a fluent/expressive way, even if it works differently. Chris Lattner
Swift currently doesn’t offer good enough alternatives for most runtime functions. - This will likely improve with future versions of Swift. Conclusion