{"id":4982,"date":"2022-03-29T17:13:24","date_gmt":"2022-03-29T09:13:24","guid":{"rendered":"https:\/\/1989lifejourney.com\/?p=4982"},"modified":"2022-03-29T17:13:57","modified_gmt":"2022-03-29T09:13:57","slug":"difference-between-value-type-and-a-reference-type-in-ios-swift%ef%bf%bc","status":"publish","type":"post","link":"https:\/\/1989lifejourney.com\/?p=4982","title":{"rendered":"Difference between value type and a reference type in iOS swift\ufffc"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>\u4ee5\u4e0b\u8cc7\u6599\u7686\u53c3\u8003\u81ea<a href=\"https:\/\/abhimuralidharan.medium.com\/difference-between-value-type-and-a-reference-type-in-ios-swift-18cb5145ad7a\">Ref1<\/a>\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Definition of Value type and Reference type<\/li><li>When do we choose a Value Type over a Reference Type?<\/li><li>How Reference and Value types are stored in memory ?<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Definition of Value type and Reference type:<\/h2>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Value type definition:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7576Value type\u88abassign\u7d66\u8b8a\u6578\u6216\u50b3\u905e\u7d66function\u6642\uff0c\u5c31\u6703\u628a\u65e2\u6709\u7684instance\u8907\u88fd\u4e00\u4efd\u51fa\u53bb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note: <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>IOS primitive collection types such as array, set, dictionary<\/strong>\u63a1\u7528<strong>copy-on-write<\/strong>\u6a5f\u5236\uff0c\u6240\u4ee5\u53ea\u6709\u628a\u524d\u8ff0collection types\u55ae\u7d14assign\u7d66\u8b8a\u6578\u6216\u50b3\u905e\u7d66function\uff0c\u4f46<strong>\u672a\u5c0d\u300c\u503c\u300d\u505a\u4fee\u6539\u7684\u8a71\uff0c\u5247\u65b0\u7684\u8b8a\u6578\u9084\u662f\u6307\u5230\u540c\u4e00\u500bmemory address<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:16px;\"><code>struct MyStruct {\n    var tmp: Int\n}\nvar a = MyStruct(tmp: 1)\nvar b = a\nprint(MemoryAddress(of: &amp;a)) \/\/0x0000000100008398\nprint(MemoryAddress(of: &amp;b)) \/\/0x00000001000083a0\n\nvar arr1 = Array(repeating: 1, count: 9)\nvar arr2 = arr1\nprint(MemoryAddress(of: &amp;arr1)) \/\/0x00000001028047a0\nprint(MemoryAddress(of: &amp;arr2)) \/\/0x00000001028047a0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Reference type definition:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7576Reference type\u88abassign\u7d66\u8b8a\u6578\u6216\u50b3\u905e\u7d66function\u6642\uff0c\u53ea\u6703\u628areference copy\u4e00\u4efd\u7d66\u65b0\u8b8a\u6578\u6216function\uff0c\u6240\u4ee5\u65b0\u8b8a\u6578\u4ecd\u7136\u6307\u5230\u540c\u4e00\u500bobject\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:16px;\"><code>class MyClass {\n    var tmp: Int\n    init(_ tmp: Int) {\n        self.tmp = tmp\n    }\n}\n\nvar c = MyClass(1)\nvar d = c\nprint(MemoryAddress(of: c)) \/\/0x000000010062b930\nprint(MemoryAddress(of: d)) \/\/0x000000010062b930<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When do we choose a Value Type over a Reference Type?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528Value type\u6642\u6a5f:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7528\u300c == \u300d \u53bb\u6bd4\u8f03\u5169\u500binstance data\u6bd4\u8f03\u5408\u7406\u6642\uff08p.s. double equal operator (aka ==)\u662f\u7528\u4f86\u6bd4\u8f03value\uff09<\/li><li>\u5e0c\u671b\u65b0\u7684\u8b8a\u6578\u64c1\u6709\u7368\u7acb\u7684state<\/li><li>Data\u6703\u5728multiple threads\u4e2d\u4f7f\u7528\uff0c\u4e0d\u9700\u8981\u64d4\u5fc3\u5728\u76ee\u524dthread\u4e0a\u7684data\u6703\u88ab\u5225\u7684thread\u4fee\u6539<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528Reference type\u6642\u6a5f:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7528\u300c === \u300d\u53bb\u6bd4\u8f03\u5169\u500binstance\u6bd4\u8f03\u5408\u7406\u6642 (p.s. ===\u662f\u6839\u64damemory address\u4f86\u6bd4\u8f03\u5169\u500bobjects\u662f\u5426\u76f8\u540c)<\/li><li>\u4f60\u5e0c\u671b\u7522\u751f\u4e00\u500b\u5206\u4eab\u4e14\u53ef\u4fee\u6539\u7684state<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Reference and Value types are stored in memory ?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Value Type<\/strong>\u00a0\u2014 \u5b58\u5728stack\u4e0a<\/li><li><strong>Reference Type<\/strong>\u00a0\u2014 \u5b58\u5728heap\u4e0a<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note: <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reference type\u7684\u300c<strong>reference<\/strong>\u300d\u662f<strong>\u5b58\u5728stack\u4e0a<\/strong>\uff0c\u800c<strong>object instance<\/strong>\u662f<strong>\u5b58\u5728heap\u4e0a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u88dc\u5145:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The way to prinit struct\/class memory address <strong>[\u53c3\u8003\u81ea<a href=\"https:\/\/stackoverflow.com\/questions\/24058906\/printing-a-variable-memory-address-in-swift\">Ref3<\/a>]<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:15px;\"><code>struct MemoryAddress&lt;T&gt;: CustomStringConvertible {\n\n    let intValue: Int\n\n    var description: String {\n        let length = 2 + 2 * MemoryLayout&lt;UnsafeRawPointer&gt;.size\n        return String(format: \"%0\\(length)p\", intValue)\n    }\n\n    \/\/ for structures\n    init(of structPointer: UnsafePointer&lt;T&gt;) {\n        intValue = Int(bitPattern: structPointer)\n    }\n}\n\nextension MemoryAddress where T: AnyObject {\n\n    \/\/ for classes\n    init(of classInstance: T) {\n        intValue = unsafeBitCast(classInstance, to: Int.self)\n        \/\/ or      Int(bitPattern: Unmanaged&lt;T&gt;.passUnretained(classInstance).toOpaque())\n    }\n}<\/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<ol class=\"wp-block-list\"><li><a href=\"https:\/\/abhimuralidharan.medium.com\/difference-between-value-type-and-a-reference-type-in-ios-swift-18cb5145ad7a\">https:\/\/abhimuralidharan.medium.com\/difference-between-value-type-and-a-reference-type-in-ios-swift-18cb5145ad7a<\/a><\/li><li><a href=\"https:\/\/juejin.cn\/post\/6844903828274282503\">https:\/\/juejin.cn\/post\/6844903828274282503<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/24058906\/printing-a-variable-memory-address-in-swift\">https:\/\/stackoverflow.com\/questions\/24058906\/printing-a-variable-memory-address-in-swift<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u8cc7\u6599\u7686\u53c3\u8003\u81eaRef1\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406: Definition of Value type and R [&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-4982","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-1im","jetpack-related-posts":[{"id":4037,"url":"https:\/\/1989lifejourney.com\/?p=4037","url_meta":{"origin":4982,"position":0},"title":"Bluetooth Communication(IOS Core Bluetooth)","author":"Kevin","date":"8 1 \u6708, 2022","format":false,"excerpt":"IOS Core Bluetooth\u662fbased on BLE(Bluetooth low ener\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":9022,"url":"https:\/\/1989lifejourney.com\/?p=9022","url_meta":{"origin":4982,"position":1},"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":[]},{"id":4087,"url":"https:\/\/1989lifejourney.com\/?p=4087","url_meta":{"origin":4982,"position":2},"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":3917,"url":"https:\/\/1989lifejourney.com\/?p=3917","url_meta":{"origin":4982,"position":3},"title":"Objective-C\u548cSwift\u7684\u5171\u540c\u9ede\u8207\u4e0d\u540c\u9ede","author":"Kevin","date":"8 12 \u6708, 2021","format":false,"excerpt":"Objective-C\u548cSwift\u7684\u5171\u540c\u9ede\u8207\u4e0d\u540c\u9ede \u4ee5\u4e0b\u8cc7\u6599\u4f86\u81ea[ref]\u7db2\u9801\u5167\u5bb9\uff0c\u9019\u88e1\u7d14\u7cb9\u505a\u91cd\u9ede\u6574\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":4899,"url":"https:\/\/1989lifejourney.com\/?p=4899","url_meta":{"origin":4982,"position":4},"title":"AnyObject v.s. Any in Swift","author":"Kevin","date":"24 3 \u6708, 2022","format":false,"excerpt":"AnyObject AnyObject can represent an instance of a\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":4982,"position":5},"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":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4982","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=4982"}],"version-history":[{"count":18,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4982\/revisions"}],"predecessor-version":[{"id":5003,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4982\/revisions\/5003"}],"wp:attachment":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4982"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fpost_folder&post=4982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}