{"id":1783,"date":"2014-01-21T17:50:16","date_gmt":"2014-01-22T00:50:16","guid":{"rendered":"http:\/\/mattfife.com\/?p=1783"},"modified":"2014-01-21T17:50:16","modified_gmt":"2014-01-22T00:50:16","slug":"violating-your-classes-objective-c-with-method-swizzling","status":"publish","type":"post","link":"https:\/\/mattfife.com\/?p=1783","title":{"rendered":"Violating your classes Objective-C with method swizzling"},"content":{"rendered":"<p>Went to a iOS programmers meetup recently here in Portland and they did a talk on the C-runtime.\u00a0 I missed most of the talk due to other commitments, but the topic of <a href=\"http:\/\/h30499.www3.hp.com\/t5\/HP-Software-Developers-Blog\/Objective-C-Runtime-method-swizzling-in-a-nutshell\/ba-p\/6122133#.UtiCkLRjE7Q\" target=\"_blank\">method swizzling <\/a>came up.<\/p>\n<p>Basically, it allows you to change what class methods are called by a class &#8211; at runtime.\u00a0 In other words, you can start willy-nilly re-pointing your member functions at runtime by:<\/p>\n<ol>\n<li>Create your 2 or more classes as normal (class A, class B)<\/li>\n<li>Create some methods off each class you want to swizzle (A.thisFunc, B.thatFunc)<\/li>\n<li>Swap&#8217;em &#8211; using the runtime functions:\n<ol>\n<li>class_getInstanceMethod()<\/li>\n<li>class_addMethod()<\/li>\n<li>class_replaceMethod()<\/li>\n<li>method_exchangeImplementations()<\/li>\n<\/ol>\n<\/li>\n<li>Profit!<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>While this is obviously very, very dangerous and likely not something you&#8217;d do in production, it does create some interesting opportunities for testing and exercising record\/replay and persist\/restore code.<\/p>\n<p>Either way &#8211; interesting write-up.\u00a0 Perhaps the title of the talk should have been called &#8216;Stupid Objective-C tricks&#8217;. \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Went to a iOS programmers meetup recently here in Portland and they did a talk on the C-runtime.\u00a0 I missed most of the talk due to other commitments, but the topic of method swizzling came up. Basically, it allows you to change what class methods are called by a class &#8211; at runtime.\u00a0 In other words, you can start willy-nilly re-pointing your member functions at runtime by: Create your 2 or more classes as normal (class A, class B) Create&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/mattfife.com\/?p=1783\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,5],"tags":[],"class_list":["post-1783","post","type-post","status-publish","format-standard","hentry","category-technicalprogramming","category-technical"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4WECr-sL","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/1783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1783"}],"version-history":[{"count":1,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/1783\/revisions"}],"predecessor-version":[{"id":1784,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/1783\/revisions\/1784"}],"wp:attachment":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}