{"id":29107,"date":"2020-06-01T16:13:31","date_gmt":"2020-06-01T20:13:31","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=29107"},"modified":"2020-06-01T16:13:31","modified_gmt":"2020-06-01T20:13:31","slug":"interposekit","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/06\/01\/interposekit\/","title":{"rendered":"InterposeKit"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/steipete\/InterposeKit\">Peter Steinberger<\/a> (<a href=\"https:\/\/twitter.com\/steipete\/status\/1267112519111491585\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/steipete\/InterposeKit\"><p>InterposeKit is a modern library to swizzle elegantly in Swift. It is <a href=\"http:\/\/interposekit.com\/\">well-documented<\/a>, <a href=\"https:\/\/github.com\/jsteipete\/InterposeKit\/actions?query=workflow%3ASwiftPM\">tested<\/a>, written in &ldquo;pure&rdquo; Swift 5.2 and works on <code>@objc dynamic<\/code> Swift functions or Objective-C instance methods. The Inspiration for InterposeKit was <a href=\"https:\/\/steipete.com\/posts\/mac-catalyst-crash-hunt\/\">a race condition in Mac Catalyst<\/a>, which required tricky swizzling to fix, I also wrote up  <a href=\"https:\/\/steipete.com\/posts\/interposekit\/\">implementation thoughts on my blog<\/a>.<\/p><p>Instead of <a href=\"https:\/\/nshipster.com\/method-swizzling\/\">adding new methods and exchanging implementations<\/a> based on <a href=\"https:\/\/developer.apple.com\/documentation\/objectivec\/1418769-method_exchangeimplementations\"><code>method_exchangeImplementations<\/code><\/a>, this library replaces the implementation directly using <a href=\"https:\/\/developer.apple.com\/documentation\/objectivec\/1418677-class_replacemethod\"><code>class_replaceMethod<\/code><\/a>. This avoids some of <a href=\"https:\/\/pspdfkit.com\/blog\/2019\/swizzling-in-swift\/\">the usual problems with swizzling<\/a>.<\/p><p>You can call the original implementation and add code before, instead or after a method call.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/steipete.com\/posts\/interposekit\/\">Peter Steinberger<\/a>:<\/p>\n<blockquote cite=\"https:\/\/steipete.com\/posts\/interposekit\/\"><p><code>imp_implementationWithBlock<\/code> has <a href=\"https:\/\/github.com\/steipete\/InterposeKit\/blob\/85f6c2dcc465811048cac0b31c4edc8bb71d4268\/Sources\/InterposeKit\/InterposeKit.swift#L130\">no way to undo<\/a> or deregister the IMP; so once you submitted a block that captures state, you have a permanent memory leak? Oh well.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Peter Steinberger (tweet): InterposeKit is a modern library to swizzle elegantly in Swift. It is well-documented, tested, written in &ldquo;pure&rdquo; Swift 5.2 and works on @objc dynamic Swift functions or Objective-C instance methods. The Inspiration for InterposeKit was a race condition in Mac Catalyst, which required tricky swizzling to fix, I also wrote up implementation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2020-06-01T20:13:34Z","apple_news_api_id":"55eb09af-c5ef-42bd-b792-4994797b2519","apple_news_api_modified_at":"2020-06-01T20:13:35Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AVesJr8XvQr23kkmUeXslGQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[1621,31,1667,30,1666,571,54,760,74,71,901],"class_list":["post-29107","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-marzipan","tag-ios","tag-ios-13","tag-mac","tag-macos-10-15","tag-memory-management","tag-objective-c","tag-objective-c-runtime","tag-opensource","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=29107"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29107\/revisions"}],"predecessor-version":[{"id":29108,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29107\/revisions\/29108"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=29107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=29107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=29107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}