Successfully reported this slideshow.
Your SlideShare is downloading. ×

Method Swizzling with Objective-C

Ad

Disclaimer
This talk is mostly for educational purposes.
⚠

Ad

A bit on the Swift runtime.
It’s turtles, all the way down.
• Apple platforms utilise the Objective-C runtime, Swift runs ...

Ad

OOP
We made Alan Kay very sad (Please watch! https://www.youtube.com/watch?v=NdSD07U5uBs)
• OOP was originally designed to...

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Loading in …3
×

Check these out next

1 of 15 Ad
1 of 15 Ad
Advertisement

More Related Content

Advertisement

Method Swizzling with Objective-C

  1. 1. Disclaimer This talk is mostly for educational purposes. ⚠
  2. 2. A bit on the Swift runtime. It’s turtles, all the way down. • Apple platforms utilise the Objective-C runtime, Swift runs on top of this (iOS 7, macOS 10.11+). • Linux (and other platforms) don’t use this runtime. They ship with a “corelibs” library that creates a runtime for Swift, but it is much smaller and doesn’t support Objective-C. • So only on Apple platforms can you inter-op Swift & Objective-C. • But that also means Swift also has access to the same runtime environment as Objective-C! (Through ObjectiveC - A swift library present on Apple systems). • Yay! (uh-oh)
  3. 3. OOP We made Alan Kay very sad (Please watch! https://www.youtube.com/watch?v=NdSD07U5uBs) • OOP was originally designed to mimic natural biological systems. • You have an object and it has properties. How do you find those properties? You talk to the object. And if it understands you, it will respond. • Objective-C is closer to the original design of OOP when compared to Java, C+ +, Pure Swift. • objc_msgSend(id, SEL, …args) -> id* is truly dynamic. • * This is no longer the signature due to ABI, but it sort of is as it’s implemented in assembly. -> https://www.mikeash.com/pyblog/ objc_msgsends-new-prototype.html
  4. 4. A Smalltalk object can do exactly three things: • Hold state (references to other objects). • Receive a message from itself or another object. • In the course of processing a message, send messages to itself or another object. The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate. Alan Kay has commented that despite the attention given to objects, messaging is the most important concept in Smalltalk: "The big idea is 'messaging'—that is what the kernel of Smalltalk/Squeak is all about Objectively speaking
  5. 5. Cells vs Gears Fluidity vs Rigidity
  6. 6. objc_msgSend(id, SEL, …args) A thingy A message It’s caveats objc_msgSend(self, viewDidAppear, [animated:true]); Hello Mr.ViewController. Could you appear please? And when you do, could you animate?
  7. 7. Monkey Patching 🐒 • The Objective-C runtime allows you to rebind the way an object will respond to messages. • This is known as Swizzling. • Apparently no one remembers where that name came from.
  8. 8. I’m going to say uh-oh again. The possibilities are frightening. • ViewControllers that actually call viewDidDisappear when you call viewDidAppear. • Additions turning to subtraction. • All of your data getting slurped up on applicationDidLaunch…Hello Google and Facebook SDKs! • Facebook Swizzles didMoveToWindow, setDelegate. • https://github.com/facebook/facebook-ios-sdk/search? q=FBSDKSwizzler&type=code • Google Swizzles viewDidAppear, viewDidDisappear: • https://github.com/firebase/firebase-ios-sdk/search?q=swizzleClass
  9. 9. AOP No, not Air Observation Post. Aspect orientated programming! • Non business logic (non functional requirements) are concerns that need “weaved” into your application. • Common use cases; Logging, Security, Profiling, Transaction Management, Analytics. • Really useful for adding an “aspect” of functionality to your app. i.e your app doesn’t have logging at the moment, but you’ve got 600 screens and you want to add it to all of them. • Solves a few problems; • Reduces boilerplate. • Keeps unrelated code separated from business logic. • Dynamic application of function. Turn aspect on/off based on flags. • Allows you to open closed interfaces (uh-oh x3).
  10. 10. Demo
  11. 11. Further reading An evolutionary dead end, but an interesting one all the same. • Anything by Alan Kay or Joe Armstrong on OOP • https://medium.com/chmcore/a-short-history-of-objective-c-aff9d2bde8dd • Akka Actors show how message passing works even in a networked system. • https://doc.akka.io/docs/akka/current/typed/actors.html#akka-actors • https://www.mikeash.com/pyblog/objc_msgsends-new-prototype.html • https://indiestack.com/2019/10/casting-objective-c-message-sends/

×