{"id":44215,"date":"2024-07-25T14:24:45","date_gmt":"2024-07-25T18:24:45","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=44215"},"modified":"2024-07-25T14:24:45","modified_gmt":"2024-07-25T18:24:45","slug":"swifts-anyobject","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/07\/25\/swifts-anyobject\/","title":{"rendered":"Swift&rsquo;s AnyObject"},"content":{"rendered":"<p><a href=\"https:\/\/belkadan.com\/blog\/2024\/07\/AnyObject\/\">Jordan Rose<\/a>:<\/p>\n<blockquote cite=\"https:\/\/belkadan.com\/blog\/2024\/07\/AnyObject\/\">\n<p>You can also use AnyObject as a constraint on protocols: <code>protocol MyDelegate: AnyObject<\/code>. Now the implementers are known to have reference semantics, and with <code>T: MyDelegate<\/code> you can have weak references to T, as before. You can even have weak references to <code>any MyDelegate<\/code>, allowing swapping between delegates of different types.<\/p>\n\n<p>What you might run into, though, is that <code>any MyDelegate<\/code> is not itself AnyObject.<\/p>\n<p>[&#8230;]<\/p>\n<p>Because it carries more information than just a single object reference: it also has a &ldquo;witness table&rdquo; pointer, the run-time representation of a protocol conformance.<\/p>\n<p>[&#8230;]<\/p>\n<p>But wait, Objective-C never had this problem! The <code>id &lt;MyDelegate&gt;<\/code> type doesn&rsquo;t take up more than a single-object-reference to store! But that&rsquo;s because ObjC protocols aren&rsquo;t represented as tables of methods; they&rsquo;re just promises that the implementing class <em>has<\/em> methods with particular names.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/03\/17\/equality-in-swift-nsobject-subclasses-and-existentials\/\">Equality in Swift: NSObject, Subclasses, and Existentials<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/04\/28\/swift-generics-2-existentials-boogaloo\/\">Swift Generics 2: Existentials Boogaloo<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/11\/17\/does-swift-call-the-protocol-extension-or-subclass-implementation\/\">Does Swift Call the Protocol Extension or Subclass Implementation?<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/02\/28\/swift-protocols-wishlist\/\">Swift Protocols Wish List<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/09\/01\/swift-protocols\/\">Swift Protocols<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Jordan Rose: You can also use AnyObject as a constraint on protocols: protocol MyDelegate: AnyObject. Now the implementers are known to have reference semantics, and with T: MyDelegate you can have weak references to T, as before. You can even have weak references to any MyDelegate, allowing swapping between delegates of different types. What you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-07-25T18:24:50Z","apple_news_api_id":"b1692600-1aa1-4746-bf2e-d5043315600c","apple_news_api_modified_at":"2024-07-25T18:24:50Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AsWkmABqhR0a_LtUEMxVgDA","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":[46,54,71,901],"class_list":["post-44215","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44215","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=44215"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44215\/revisions"}],"predecessor-version":[{"id":44216,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44215\/revisions\/44216"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=44215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=44215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=44215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}