{"id":4899,"date":"2022-03-24T23:55:05","date_gmt":"2022-03-24T15:55:05","guid":{"rendered":"https:\/\/1989lifejourney.com\/?p=4899"},"modified":"2022-03-24T23:55:05","modified_gmt":"2022-03-24T15:55:05","slug":"anyobject-v-s-any-in-swift","status":"publish","type":"post","link":"https:\/\/1989lifejourney.com\/?p=4899","title":{"rendered":"AnyObject v.s. Any in Swift"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">AnyObject<\/h2>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">AnyObject can represent an instance of any class type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1)<\/strong><br>AnyObject can be used as the concrete type for an instance of any <strong>class, class type, or class-only protocol<\/strong>.<br><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TestB {}\nvar tmp1: AnyObject = TestB()\nvar tmp2: AnyObject = TestB.self\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2)<\/strong><br>AnyObject can also be used as the concrete type for <strong>an instance of a type that bridges to an Objective-C class.<\/strong> Many value types in Swift bridge to Objective-C counterparts, like String and Int.<br><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var tmp: AnyObject = \"This is a string\" as NSString\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Any<\/h2>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">Any can represent an instance of any type at all, including function types, optional types.<br><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var closure: () -&gt; Void = {}\nvar tmp: Any = closure()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ref:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/medium.com\/@mimicatcodes\/any-vs-anyobject-in-swift-3-b1a8d3a02e00\">https:\/\/medium.com\/@mimicatcodes\/any-vs-anyobject-in-swift-3-b1a8d3a02e00<br><\/a><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/anyobject\">https:\/\/developer.apple.com\/documentation\/swift\/anyobject<br><\/a><a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/TypeCasting.html\">https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/TypeCasting.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AnyObject AnyObject can represent an instance of any cl [&hellip;]<\/p>\n","protected":false},"author":139426785,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_postgate_lock_override":"inherit","_postgate_lock_mode":"inherit","_postgate_preview_mode":"inherit","_postgate_preview_limit":0,"_postgate_product_ids":[],"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","jetpack_seo_llms_txt_always_include":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_wpcom_ai_launchpad_first_post":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","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},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[37324612,1],"tags":[],"post_folder":[],"class_list":["post-4899","post","type-post","status-publish","format-standard","hentry","category-ios-development","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paR5vw-1h1","jetpack-related-posts":[{"id":4982,"url":"https:\/\/1989lifejourney.com\/?p=4982","url_meta":{"origin":4899,"position":0},"title":"Difference between value type and a reference type in iOS swift\ufffc","author":"Kevin","date":"29 3 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u8cc7\u6599\u7686\u53c3\u8003\u81eaRef1\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406: Definition of Value type \u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":276,"url":"https:\/\/1989lifejourney.com\/?p=276","url_meta":{"origin":4899,"position":1},"title":"AVFoundation Programming Guide &#8211; Swift &#8211; Using Assets","author":"Kevin","date":"25 12 \u6708, 2019","format":false,"excerpt":"Using Assets Assets can come from a file or from m\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4009,"url":"https:\/\/1989lifejourney.com\/?p=4009","url_meta":{"origin":4899,"position":2},"title":"Static vs Dynamic Dispatch in Swift","author":"Kevin","date":"6 1 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u5167\u5bb9\u53c3\u8003\u81ea[Ref]\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u6458\u8981\u6574\u7406: Method Dispatch\u5b9a\u7fa9: Method\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4087,"url":"https:\/\/1989lifejourney.com\/?p=4087","url_meta":{"origin":4899,"position":3},"title":"Swift Initialization","author":"Kevin","date":"25 1 \u6708, 2022","format":false,"excerpt":"DefinitionRelationships between designated and con\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/docs.swift.org\/swift-book\/_images\/initializerDelegation01_2x.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/docs.swift.org\/swift-book\/_images\/initializerDelegation01_2x.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/docs.swift.org\/swift-book\/_images\/initializerDelegation01_2x.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":7878,"url":"https:\/\/1989lifejourney.com\/?p=7878","url_meta":{"origin":4899,"position":4},"title":"ChatGPT\u6e2c\u8a66_23 (Write the comment which style is Apple&#8217;s documentation comment style)","author":"Kevin","date":"12 4 \u6708, 2023","format":false,"excerpt":"\u554f\u984c: Write the comment for below Swift functions wh\u2026","rel":"","context":"\u5728\u300cChatGPT\u300d\u4e2d","block_context":{"text":"ChatGPT","link":"https:\/\/1989lifejourney.com\/?cat=760577830"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9022,"url":"https:\/\/1989lifejourney.com\/?p=9022","url_meta":{"origin":4899,"position":5},"title":"IOS Swift Opaque type","author":"Kevin","date":"7 11 \u6708, 2023","format":false,"excerpt":"Prerequisite The Problem That Opaque Types Solve O\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/users\/139426785"}],"replies":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4899"}],"version-history":[{"count":4,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4899\/revisions"}],"predecessor-version":[{"id":4903,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4899\/revisions\/4903"}],"wp:attachment":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4899"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fpost_folder&post=4899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}