tag:blogger.com,1999:blog-14753458088513428122024-03-18T15:18:51.473+05:30 iOS interview questionsLatest iOS Interview QuestionsManan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.comBlogger226125tag:blogger.com,1999:blog-1475345808851342812.post-86496263093832867982023-11-26T16:26:00.002+05:302023-11-26T16:26:38.747+05:30Difference between assign and retain in iOS<div dir="ltr" style="text-align: left;" trbidi="on"><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-variant-ligatures: normal; margin: 0px 0px 1.25em; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;"><span style="font-family: verdana; font-size: medium;">In the context of memory management in programming, particularly in Objective-C (though less relevant in modern Swift), "assign" and "retain" are related to the property attributes used in the declaration of object properties.</span></p><ol style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; counter-reset: list-number 0; display: flex; flex-direction: column; font-variant-ligatures: normal; list-style: none; margin: 1.25em 0px; orphans: 2; padding: 0px; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; counter-increment: list-number 1; display: block; margin-bottom: 0px; margin-top: 0px; min-height: 28px; padding-left: 0.375em;"><p style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; margin: 0px;"><strong style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"><span style="font-family: verdana; font-size: medium;">Assign:</span></strong></p><ul style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: flex; flex-direction: column; list-style: outside; margin: 0px 0px 0px 1rem; padding: 0px;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;"><br /></span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">In the context of Objective-C, the <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">assign</code> attribute is used for simple assignments and is often associated with primitive types or non-Objective-C objects.</span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">When you declare a property with the <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">assign</code> attribute, you're saying that the property will just take the value assigned to it without affecting the reference counting of the object.</span></li></ul><pre style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; line-height: 1.71429; margin-bottom: 0.5rem; margin-top: 0.25rem; overflow-x: auto; padding: 0px;"><div class="bg-black rounded-md" style="--tw-bg-opacity: 1; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; background-color: rgba(0,0,0,var(--tw-bg-opacity)); border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"></div></pre><ul style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: flex; flex-direction: column; list-style: outside; margin: 0px 0px 0px 1rem; padding: 0px;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;"><br /></span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">@property (assign) NSInteger someInteger;</span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;"><br /></span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">For objects, assigning does not increase the retain count, so if the assigned object is deallocated elsewhere, you might end up with a dangling pointer.</span></li></ul></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; counter-increment: list-number 1; display: block; margin-bottom: 0px; margin-top: 0px; min-height: 28px; padding-left: 0.375em;"><p style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; margin: 0px;"><strong style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"><span style="font-family: verdana; font-size: medium;"><br /></span></strong></p><p style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; margin: 0px;"><strong style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"><span style="font-family: verdana; font-size: medium;">Retain:</span></strong></p><ul style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: flex; flex-direction: column; list-style: outside; margin: 0px 0px 0px 1rem; padding: 0px;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;"><br /></span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">The <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">retain</code> attribute, on the other hand, is used when you want to claim ownership of an object. It increases the retain count of the object, indicating that you want to keep a reference to it.</span></li></ul><pre style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; line-height: 1.71429; margin-bottom: 0.5rem; margin-top: 0.25rem; overflow-x: auto; padding: 0px;"><div class="bg-black rounded-md" style="--tw-bg-opacity: 1; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; background-color: rgba(0,0,0,var(--tw-bg-opacity)); border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"></div></pre><div><span style="font-family: verdana; font-size: medium;"> </span></div><div><span style="font-family: verdana; font-size: medium;"> @property (retain) NSString *name;
</span></div><div><span style="font-family: verdana; font-size: medium;"><br /></span></div><ul style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: flex; flex-direction: column; list-style: outside; margin: 0px 0px 0px 1rem; padding: 0px;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;"><br /></span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">When using <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">retain</code>, it's your responsibility to release the object when you're done with it. This is crucial to avoid memory leaks.</span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;"><br /></span></li></ul><pre style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; line-height: 1.71429; margin-bottom: 0.5rem; margin-top: 0.25rem; overflow-x: auto; padding: 0px;"><div class="bg-black rounded-md" style="--tw-bg-opacity: 1; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; background-color: rgba(0,0,0,var(--tw-bg-opacity)); border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"></div></pre><ul style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: flex; flex-direction: column; list-style: outside; margin: 0px 0px 0px 1rem; padding: 0px;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">[name release];</span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;"><br /></span></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><span style="font-family: verdana; font-size: medium;">In modern Objective-C and Swift, Apple introduced Automatic Reference Counting (ARC), which essentially automates the reference counting process. In Swift, you generally use <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">strong</code> instead of <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">retain</code>, and the memory management is handled by the ARC.</span></li></ul></li></ol><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-variant-ligatures: normal; margin: 1.25em 0px 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;"><span style="font-family: verdana; font-size: medium;">In summary, the key difference is that <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">assign</code> is used for non-object types and doesn't affect the reference counting of objects, while <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">retain</code> (or <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">strong</code> in Swift) is used for objects, and it does increase the retain count, indicating ownership. With ARC in Swift, the need to manually specify <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">assign</code> or <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-weight: 600;">retain</code> has diminished, as ARC takes care of many memory management tasks automatically.AI.</span></p>
<b><br /></b></div>
Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-67217286940570507282023-11-26T16:03:00.037+05:302023-11-26T16:14:02.249+05:30Dependency injection in Swift<p> <span style="font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; orphans: 2; white-space: pre-wrap; widows: 2;">Dependency injection (DI) is a design pattern in software development that promotes the separation of concerns by allowing components to depend on abstractions rather than concrete implementations. In Swift, dependency injection is commonly used to achieve loosely coupled and easily testable code.</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRuol5upiQBYKE15S0rwEEQgPrKMM0GQkgFxCbSWJIy7Q1RAwDAXQfAw3ZyraJBHQeYD_DywZqAE_ITDC7s2Lfcw67b_TPsNmzOv5PYN-8f5BNCl-feBZkSDxIK_U_AgiFnMx4tvjoMgWhXBnsTuWIzoLurFCNeV_6jwQL5I2s5xWQ0HWczNZ5LpAjgL4/s1280/1*GkEtWDiMYQnEMPmOVOMWaA.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Dependency injection in Swift" border="0" data-original-height="720" data-original-width="1280" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRuol5upiQBYKE15S0rwEEQgPrKMM0GQkgFxCbSWJIy7Q1RAwDAXQfAw3ZyraJBHQeYD_DywZqAE_ITDC7s2Lfcw67b_TPsNmzOv5PYN-8f5BNCl-feBZkSDxIK_U_AgiFnMx4tvjoMgWhXBnsTuWIzoLurFCNeV_6jwQL5I2s5xWQ0HWczNZ5LpAjgL4/w400-h225/1*GkEtWDiMYQnEMPmOVOMWaA.jpg" title="Dependency injection in Swift" width="400" /></a></div><span style="font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; orphans: 2; white-space: pre-wrap; widows: 2;"><br /></span><p></p><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; margin: 1.25em 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;">There are three main types of dependency injection:</p><ol style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; counter-reset: list-number 0; display: flex; flex-direction: column; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; list-style: none; margin: 1.25em 0px; orphans: 2; padding: 0px; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; counter-increment: list-number 1; display: block; margin-bottom: 0px; margin-top: 0px; min-height: 28px; padding-left: 0.375em;"><p style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; margin: 0px;"><strong style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;">Constructor Injection:</strong> Dependencies are injected through the class's initializer.</p></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; counter-increment: list-number 1; display: block; margin-bottom: 0px; margin-top: 0px; min-height: 28px; padding-left: 0.375em;"><p style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; margin: 0px;"><strong style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;">Method Injection:</strong> Dependencies are injected through methods of the class.</p></li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; counter-increment: list-number 1; display: block; margin-bottom: 0px; margin-top: 0px; min-height: 28px; padding-left: 0.375em;"><p style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; margin: 0px;"><strong style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;">Property Injection:</strong> Dependencies are injected through properties of the class.</p></li></ol><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; margin: 1.25em 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;">Let's look at an example using constructor injection in Swift:</p><pre style="border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; color: #374151; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-variant-ligatures: normal; line-height: 1.71429; margin-bottom: 0px; margin-top: 0px; orphans: 2; overflow-x: auto; padding: 0px; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; widows: 2;"><div class="bg-black rounded-md" style="--tw-bg-opacity: 1; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; background-color: rgba(0,0,0,var(--tw-bg-opacity)); border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"></div></pre><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; margin: 1.25em 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;">// Protocol defining the dependency
protocol DataService {
func fetchData() -> String
}
// Concrete implementation of the DataService protocol
class RemoteDataService: DataService {
func fetchData() -> String {
return "Data from remote service"
}
}
// Class that depends on DataService through constructor injection
class DataManager {
let dataService: DataService
init(dataService: DataService) {
self.dataService = dataService
}
func processData() -> String {
let data = dataService.fetchData()
return "Processed: \(data)"
}
}
// Example of using the DataManager with dependency injection
let remoteDataService = RemoteDataService()
let dataManager = DataManager(dataService: remoteDataService)
let result = dataManager.processData()
print(result) // Output: Processed: Data from remote service
</p><div><br /></div><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; margin: 1.25em 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;">In this example:</p><ul style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: flex; flex-direction: column; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; list-style: outside; margin: 1.25em 0px; orphans: 2; padding: 0px; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600; margin-bottom: 1.25em; margin-top: 1.25em;">DataService</code> is a protocol that defines the contract for fetching data.</li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600; margin-top: 1.25em;">RemoteDataService</code> is a concrete implementation of <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600; margin-bottom: 1.25em;">DataService</code>.</li><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; display: block; margin: 0px; min-height: 28px; padding-left: 0.375em;"><code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600; margin-top: 1.25em;">DataManager</code> is a class that depends on <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600; margin-bottom: 1.25em;">DataService</code> through its initializer.</li></ul><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; margin: 1.25em 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;">This setup allows you to easily switch the implementation of <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600;">DataService</code> without modifying <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600;">DataManager</code>. For example, you could create a <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600;">LocalDataService</code> implementing <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600;">DataService</code> and use it with <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600;">DataManager</code> without changing the <code style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-weight: 600;">DataManager</code> class.</p><pre style="border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box; color: #374151; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace !important; font-size: 0.875em; font-variant-ligatures: normal; line-height: 1.71429; margin-bottom: 0px; margin-top: 0px; orphans: 2; overflow-x: auto; padding: 0px; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; widows: 2;"><div class="bg-black rounded-md" style="--tw-bg-opacity: 1; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; background-color: rgba(0,0,0,var(--tw-bg-opacity)); border-radius: 0.375rem; border: 0px solid rgb(217, 217, 227); box-sizing: border-box;"></div></pre><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; margin: 1.25em 0px 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;">class LocalDataService: DataService {
func fetchData() -> String {
return "Data from local storage"
}
}
let localDataService = LocalDataService()
let dataManagerWithLocalData = DataManager(dataService: localDataService)
let resultLocal = dataManagerWithLocalData.processData()
print(resultLocal) // Output: Processed: Data from local storage
</p><div><br /></div><p style="border: 0px solid rgb(217, 217, 227); box-sizing: border-box; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px; font-variant-ligatures: normal; margin: 1.25em 0px 0px; orphans: 2; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; white-space: pre-wrap; widows: 2;">This flexibility is especially useful for testing, as you can easily substitute real implementations with mock implementations for testing purposes.AI.</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-15304195426751533012023-11-26T15:51:00.004+05:302023-11-26T15:51:29.570+05:30Difference between App Store Binary Rejection and Metadata rejection<div><span style="font-size: medium;">When submitting an application to the App Store, developers may encounter two types of rejections: App Store Binary Rejection and Metadata Rejection. Understanding the difference between these rejections is essential for iOS developers using Xcode to maximize their chances of successfully getting their apps approved.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><div style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6d9U51s171KU1M7HBvTGHPPHQKm5yepj1cetjE6n1XElhvnIQYP3ApEt0ZlP8PU76LzJpbfn-jZ4tZ8U2tUtKScWRxSVahaB3Pvt7usr5-kLm9D2WXfS7mOVBzPkdWD9Q7erb61179Y91pyxquflkVk2iqSz1cEnnFwSc5Hh9Vj10A40rJfPmLuiSYVw/s714/Screenshot%202023-11-26%20at%203.49.30%20PM.png" imageanchor="1"><img alt="Difference between App Store Binary Rejection and Metadata rejection" border="0" data-original-height="690" data-original-width="714" height="386" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6d9U51s171KU1M7HBvTGHPPHQKm5yepj1cetjE6n1XElhvnIQYP3ApEt0ZlP8PU76LzJpbfn-jZ4tZ8U2tUtKScWRxSVahaB3Pvt7usr5-kLm9D2WXfS7mOVBzPkdWD9Q7erb61179Y91pyxquflkVk2iqSz1cEnnFwSc5Hh9Vj10A40rJfPmLuiSYVw/w400-h386/Screenshot%202023-11-26%20at%203.49.30%20PM.png" title="Difference between App Store Binary Rejection and Metadata rejection" width="400" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div></span></div><div><span style="font-size: medium;">App Store Binary Rejection refers to the rejection of the compiled binary code that forms the application. This rejection can stem from numerous reasons, such as memory leaks or violations of Apple's App Store Review Guidelines. Memory leaks occur when an app fails to release dynamically allocated memory, resulting in wasted memory resources and potential crashes. To mitigate memory leaks, developers must carefully use memory allocation and deallocation mechanisms provided by iOS and Xcode, such as ARC (Automatic Reference Counting) or manual memory management.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;">On the other hand, Metadata Rejection deals with issues related to the app's information and presentation on the App Store. It primarily focuses on the textual and visual components, including the app's name, description, screenshots, keywords, and other metadata. Common reasons for metadata rejection can include misleading app descriptions or inappropriate screenshots that do not accurately represent the actual app functionalities.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;">To avoid both types of rejections, it is imperative for developers to pay attention to detail during the development and submission process. After addressing potential memory leaks by employing proper memory management practices, developers must also ensure that the app's metadata accurately reflects its functionalities and adheres to Apple's guidelines. By debugging their code thoroughly using instruments provided by Xcode, developers can identify and resolve any memory leaks, while adhering to Apple's App Store Review Guidelines can prevent metadata rejection.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;">In conclusion, developers using Xcode to create iOS apps must be aware of the distinction between App Store Binary Rejection and Metadata Rejection. By addressing memory leaks, adhering to Apple's guidelines, and carefully preparing app metadata, developers can increase the chances of getting their apps approved on the App.AI.</span></div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-37330206806576937082023-11-26T15:41:00.004+05:302023-11-26T15:41:49.737+05:30Difference between normal function and function ending throw<p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">Function ending with throw can throw error but normal function can not throw error it can just return value.</span></p><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">Lets take example :</span><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">1. Custom error enum</span><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><div class="p1" style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;"><span style="color: black;"><span class="s1">enum</span> ErrorsToThrow: <span class="s2">Error</span> {</span></div><div class="p1" style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;"><span style="color: black;"> <span class="s1">case</span> fileNotFound</span></div><div class="p1" style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;"><span style="color: black;"> <span class="s1">case</span> fileNotReadable</span></div><div class="p1" style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;"><span style="color: black;"> <span class="s1">case</span> fileSizeIsTooHigh</span></div><div class="p1" style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;"><span style="color: black;">}</span><br /><br />2. Make function which can throw error<br /><br /><div class="p1"><span class="s1">func</span> readFiles(path:<span class="s2">String</span>) <span class="s1">throws</span> -><span class="s2">String</span> {</div><div class="p1"> <span class="s1">if</span> path == <span class="s3">""</span> {</div><div class="p2"><span class="s4"> </span><span class="s1">throw</span><span class="s4"> </span>ErrorsToThrow<span class="s4">.</span>fileNotFound</div><div class="p1"> }</div><div class="p3"><span class="s4"> </span><span class="s1">return</span><span class="s4"> </span>"Data from file"</div><br /><div class="p1"> }</div><div class="p1"><br />3. Calling readFiles function</div><div class="p1"> do<span class="s1"> {</span></div><div class="p2"> <span class="s2">let</span> dataFromString = <span class="s2">try</span>? <span class="s3">readFiles</span>(path: <span class="s4">""</span>)</div><div class="p2"> <span class="s5">print</span>(dataFromString)</div><div class="p2"> } <span class="s2">catch</span> ErrorsToThrow.<span class="s3">fileNotFound</span> {</div><div class="p3"><span class="s1"> </span><span class="s5">print</span><span class="s1">(</span>"error generated1"<span class="s1">)</span></div><div class="p2"> } <span class="s2">catch</span> ErrorsToThrow.<span class="s3">fileNotReadable</span> {</div><div class="p3"><span class="s1"> </span><span class="s5">print</span><span class="s1">(</span>"error generated2"<span class="s1">)</span></div><div class="p2"> } <span class="s2">catch</span> ErrorsToThrow.<span class="s3">fileSizeIsTooHigh</span> {</div><div class="p3"><span class="s1"> </span><span class="s5">print</span><span class="s1">(</span>"error generated3"<span class="s1">)</span></div><div class="p2"> } <span class="s2">catch</span> {</div><div class="p2"> <span class="s5">print</span>(<span class="s4">"error"</span>)</div><div class="p1"></div><div class="p2"> }</div></div><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">Now, let's analysis.</span><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">1. If in #2, function is not ending with throws can not throw error. But our function can throw error.</span><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">2. In #3, we have used try?, so if readFiles function return nil instead of throwing error. So in our case print(dataFromString) statement executed and it will nil means printing nil.</span><div class="google-auto-placed ap_container" style="caret-color: rgb(51, 51, 51); clear: both; color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px; height: auto; text-align: center; width: 618px;"><ins class="adsbygoogle adsbygoogle-noablate" data-ad-client="ca-pub-6801405920639793" data-ad-format="auto" data-ad-status="unfilled" data-adsbygoogle-status="done" style="display: block; height: 0px; margin: auto;"><div aria-label="Advertisement" id="aswift_3_host" style="border: medium; display: inline-block; height: 0px; margin: 0px; opacity: 0; overflow: hidden; padding: 0px; position: relative; visibility: visible; width: 618px;" tabindex="0" title="Advertisement"><iframe allowtransparency="true" data-google-container-id="a!4" data-google-query-id="CJWc7f6d4YIDFSHZFgUdtXgORg" data-load-complete="true" frameborder="0" height="0" hspace="0" id="aswift_3" marginheight="0" marginwidth="0" name="aswift_3" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" scrolling="no" src="https://googleads.g.doubleclick.net/pagead/ads?gdpr=0&client=ca-pub-6801405920639793&output=html&h=280&adk=186598372&adf=1647056134&pi=t.aa~a.3809098904~i.35~rp.1&w=618&fwrn=4&fwrnh=100&lmt=1700986385&num_ads=1&rafmt=1&armr=3&sem=mc&pwprc=9390334626&ad_type=text_image&format=618x280&url=https%3A%2F%2Fwww.iosiqa.com%2F2019%2F05%2Fdifference-between-normal-function-and.html&ea=0&host=ca-host-pub-1556223355139109&fwr=0&pra=3&rh=155&rw=618&rpe=1&resp_fmts=3&wgl=1&fa=27&dt=1700986897960&bpp=1&bdt=1104&idt=-M&shv=r20231109&mjsv=m202311140101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D07b2325f986b2ffd%3AT%3D1700925403%3ART%3D1700986897%3AS%3DALNI_MZVmisM_YeZeuPljRmQeaqCCHPFpw&gpic=UID%3D00000c9922f863cd%3AT%3D1700925403%3ART%3D1700986897%3AS%3DALNI_MYZlhOZcTgZtXitI0KfiHSZr6-imA&prev_fmts=0x0%2C620x280%2C618x280&nras=2&correlator=3277083315139&frm=20&pv=1&ga_vid=1315241949.1700925403&ga_sid=1700986897&ga_hid=967522927&ga_fc=1&u_tz=330&u_his=2&u_h=1120&u_w=1792&u_ah=1010&u_aw=1792&u_cd=24&u_sd=2&adx=522&ady=1637&biw=1792&bih=930&scr_x=0&scr_y=0&eid=44759876%2C44759927%2C44759837%2C31079492%2C42531514%2C42531706%2C44798934%2C31078301%2C31079756%2C44807764%2C44808148%2C44808285%2C44809057%2C318512602&oid=2&pvsid=2473310078275049&tmod=1486338366&uas=0&nvt=1&ref=https%3A%2F%2Fsearch.google.com%2F&fc=1408&brdim=0%2C25%2C0%2C25%2C1792%2C25%2C1792%2C1010%2C1792%2C930&vis=1&rsz=%7C%7Cs%7C&abl=NS&fu=128&bc=31&psd=W251bGwsbnVsbCxudWxsLDNd&ifi=4&uci=a!4&btvi=2&fsb=1&dtd=396" style="border-style: none; border-width: 0px; height: 0px; left: 0px; position: absolute; top: 0px; width: 618px;" vspace="0" width="618"></iframe></div></ins></div><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">3. If try! is written and if function throw error, then fatal error will be occured and program will be crashed as we ensure that error will not occur by putting ! .</span><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">4. So if we want to execute catch statement then we have to use only try . try always used with do-catch.</span><br style="caret-color: rgb(51, 51, 51); color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15.84px;" /><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">5. try? and try! does not need do-catch block.</span>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-35856674373983158982023-11-26T15:39:00.005+05:302023-11-26T15:39:53.871+05:30Write a program to define closure which accepts string and return integer.<p> <span style="background-color: white; color: #333333; font-size: 15.84px;">Write a closure that accepts string and return length of string(which is integer) :</span></p><p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;"><br /></span></p><p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">let simpleClosure:(String) -> (Int) = { name in </span></p><p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;"><span> return name.count</span><br /></span></p><p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">}</span></p><p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">let result = simpleClosure("Hello World")</span></p><p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; caret-color: rgb(51, 51, 51); color: #333333; font-size: 15.84px;">print(result)</span></p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-68707977794675341892023-11-26T15:16:00.001+05:302023-11-26T15:23:31.315+05:30Identifying Memory Leaks using the Xcode<div>Identifying Memory Leaks in Xcode with Memory Graph Debugger is a great way to make application leakage proof. When it comes to detecting memory leaks in iOS development using Xcode, there are a few steps you can follow. Memory leaks can lead to performance issues, crashes, and an overall degraded user experience. Therefore, it is crucial to address and resolve them.</div><div><br /></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy11NlHg5wIAqhUH7kpSioZFs0573NnV4oqPoyMy0-AjZeZ2sv_kkVs5YpAmMw24v5RlNd6CY1BpjYEOkqpHGs7-M-HxcXLMfjnjO-e88293aVmhdYGVaogmuz_JNPRFoO6d0YWYzTbLVR9i20TmqTgGMXsMIUNmwjArbmjB6Q6MMJyOM6AKrAWhAiGVk/s984/Screenshot%202023-11-26%20at%203.07.57%20PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Identifying Memory Leaks in Xcode with Memory Graph Debugger." border="0" data-original-height="502" data-original-width="984" height="204" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy11NlHg5wIAqhUH7kpSioZFs0573NnV4oqPoyMy0-AjZeZ2sv_kkVs5YpAmMw24v5RlNd6CY1BpjYEOkqpHGs7-M-HxcXLMfjnjO-e88293aVmhdYGVaogmuz_JNPRFoO6d0YWYzTbLVR9i20TmqTgGMXsMIUNmwjArbmjB6Q6MMJyOM6AKrAWhAiGVk/w400-h204/Screenshot%202023-11-26%20at%203.07.57%20PM.png" title="Identifying Memory Leaks in Xcode with Memory Graph Debugger." width="400" /></a></div><br /><div><br /><br /></div><div><br /></div><div>1. Enable the Memory Debugging Tools:</div><div> - Open your Xcode project and go to the "Product" menu.</div><div> - Select "Scheme" and then click on "Edit Scheme."</div><div> - In the left sidebar, select "Run" and then navigate to the "Diagnostics" tab.</div><div> - Enable the "Memory Management" option and close the scheme editor.</div><div><br /></div><div>2. Run the App with the Leaks Instrument:</div><div> - Build and run your iOS app in the Xcode simulator or on a physical device.</div><div> - Go to the "Product" menu and select "Profile."</div><div> - Choose the "Leaks" instrument.</div><div> - Observe the instrument while interacting with your app to detect any memory leaks.</div><div> - If any leaks are found, it will be indicated in the "Allocations List" and "Leaks List" panels.</div><div><br /></div><div>3. Analyze the Detected Leaks:</div><div> - Click on the leaks listed in the "Leaks List" panel to review them in detail.</div><div> - The "Allocations Summary" panel will show you the memory allocations and deallocations for each object.</div><div> - Analyze the call stack to identify the source of the leak.</div><div> - Pay attention to any objects that are not being deallocated when they should be.</div><div><br /></div><div>4. Diagnose the Memory Leak:</div><div> - Understanding the code responsible for the memory leak is key to finding a solution.</div><div> - Use the call stack information and Xcode's debugging tools to trace back to where the leaked object is created or retained.</div><div> - Review variables and references that may be keeping the object from being released.</div><div> - Common causes of memory leaks include strong reference cycles, forgotten timers, delegates not set as weak, or improper use of closures.</div><div><br /></div><div>5. Fix the Memory Leak:</div><div> - Once you've identified the source of the memory leak, take appropriate actions to fix it.</div><div> - Ensure that objects are properly deallocated by removing any unnecessary strong references.</div><div> - Consider using weak or unowned references where appropriate.</div><div> - Review your code for strong reference cycles and break them using capture lists or weak/unowned references.</div><div> - Validate that the memory leak is resolved by re-running the app and using the Leaks instrument again.</div><div><br /></div><div>By following these steps, you can effectively detect and fix memory leaks in your Xcode project. Regularly checking for and resolving memory leaks will result in a more stable and efficient iOS .</div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-40988322970567415812023-11-26T11:37:00.008+05:302023-11-26T15:42:55.274+05:30Explain collections in Swift<span style="font-family: verdana;">Following are different types of collections in Swift:</span><p></p><div><span style="font-family: verdana;">Array</span></div><div><span style="font-family: verdana;">Set</span></div><div><span style="font-family: verdana;">Dictionary</span></div><div><span style="font-family: verdana;"><br /></span></div><div><span style="font-family: verdana;">Let's understand one by one : </span></div><div class="eo ep eq er es" style="box-sizing: inherit; caret-color: rgba(0, 0, 0, 0.8); color: rgba(0, 0, 0, 0.8); overflow-wrap: break-word; word-break: break-word;"><div class="ab ca" style="box-sizing: inherit; display: flex; justify-content: center;"><div class="ch bg dx dy dz ea" style="box-sizing: inherit; margin: 0px 24px; max-width: 680px; min-width: 0px; width: 680px;"><h1 class="ms mt ev be mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np bj" data-selectable-paragraph="" id="f643" style="box-sizing: inherit; color: #242424; font-size: 24px; letter-spacing: -0.016em; line-height: 30px; margin: 1.95em 0px -0.28em;"><span style="font-family: inherit;">1. Array</span></h1><p class="pw-post-body-paragraph kt ku ev kv b kw nq ky kz la nr lc ld le ns lg lh li nt lk ll lm nu lo lp lq eo bj" data-selectable-paragraph="" id="4d21" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 0.94em 0px -0.46em; word-break: break-word;"></p><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: -0.003em;">Most common and Widely used data type. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">Ordered collection of similar types of data. </span></li></ul><p class="pw-post-body-paragraph kt ku ev kv b kw nq ky kz la nr lc ld le ns lg lh li nt lk ll lm nu lo lp lq eo bj" data-selectable-paragraph="" id="4d21" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 0.94em 0px -0.46em; word-break: break-word;"><strong class="kv ew" style="box-sizing: inherit; font-family: inherit; letter-spacing: -0.003em;">Declaration: </strong></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="b0cc" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var normalArray = [Int]() // </span><span face="-webkit-standard" style="font-size: 20px; letter-spacing: -0.06px; white-space: normal;">declaring an empty </span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="b0cc" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span face="-webkit-standard" style="font-size: 20px; letter-spacing: -0.06px; white-space: normal;"> Int array like this</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="f949" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"></p><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: -0.003em;">The array can be any type in swift. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">We can also declare with some initial values. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">Swift is a strongly typed language so we don’t have to tell the type when we declare an array but we can also specify the type</span></li></ul><p></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="da96" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var <span style="letter-spacing: -0.352px;">normal</span>Array = [2,3,4,5]<br style="box-sizing: inherit;" />var <span style="letter-spacing: -0.352px;">normal</span>Array: [Int] = [2,3,4,5]</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="c10d" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"><span style="font-family: inherit;">You also can declare an array with some repeated values or using one or more arrays</span></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="48c3" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var<strong class="od ew" style="box-sizing: inherit;"> </strong><span style="letter-spacing: -0.352px;">normal</span>Array = Array(repeating: 1, count: 3) //initialize <span style="letter-spacing: -0.352px;">normal</span>Array with three ones </span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="4e91" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var <span style="letter-spacing: -0.352px;">normal</span>Array = arrayOne + arrayTwo //initialize <span style="letter-spacing: -0.352px;">normal</span>Array with addign arrayOne & arrayTwo</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="b8d9" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Insert elements into an Array: </strong>We can insert an element at the end of the array or in a valid position.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="485c" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.append(12) //append element at the end of array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array += [100.0] //append at the end</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="bd9b" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.insert(12, at: 0) //insert element at 0 position. This position value should be n to n-1 if array have n number of elements.</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="ebe7" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Access Elements of Array: </strong>We can access the element of an array using a subscript <em class="ny" style="box-sizing: inherit;">s</em>yntax, passing the index value into it. Also swift provided a list of methods to get values and check different parameters.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="675c" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array[2] //return second element from array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.count // return the number of elements in array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.isEmpty // return false if array is empty<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.capacity // return the number of elements in array</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="7a33" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.first //get the first element from array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.last //get the last elements from array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.max() //get the max element from array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.min() //get the min element from array</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="36e0" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.sorted() // sort the array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.reversed() //reverse an array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.shuffled() //shuffle the elements of an array</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="1af4" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Replace elements of Array: </strong>Array<strong class="kv ew" style="box-sizing: inherit;"> </strong>value<strong class="kv ew" style="box-sizing: inherit;"> </strong>can set or replace in a specific index or for a range of the index.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="4b19" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array[2] = 1 // replace the value in second index of array</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="437f" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array[1...3] = [12, 23, 34] // set first to third position value as 12, 23 &34</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="0798" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Remove elements from Array: </strong>Array elements<strong class="kv ew" style="box-sizing: inherit;"> </strong>can remove from different ways & different positions.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="c7e3" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.removeAll() // remove allelement from array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.removeFirst() // remove first element from array<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Array.removeLast() // remove last element from array</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="aa7c" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.remove(at: 2) //remove second element from array</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="ac85" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.removeLast(1) // remove the second element from the end index of array</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="7567" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Array.removeFirst(2) // remove the thirdelement from the start index of the array</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="1a2d" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Iterate</strong> <strong class="kv ew" style="box-sizing: inherit;">over</strong> <strong class="kv ew" style="box-sizing: inherit;">the</strong> <strong class="kv ew" style="box-sizing: inherit;">array: </strong>Every element of an array can be traverse using loop</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="8ad4" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">for i in 0..<<span style="letter-spacing: -0.352px;">normal</span>Array.count{<br style="box-sizing: inherit;" /> print(<span style="letter-spacing: -0.352px;">normal</span>Array[i])<br style="box-sizing: inherit;" />}</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="554b" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><strong class="od ew" style="box-sizing: inherit;">or<br style="box-sizing: inherit;" /></strong>for item in <span style="letter-spacing: -0.352px;">normal</span>Array{<br style="box-sizing: inherit;" /> print(item)<br style="box-sizing: inherit;" />}</span></span></pre></div></div></div><div class="eo ep eq er es" style="box-sizing: inherit; caret-color: rgba(0, 0, 0, 0.8); color: rgba(0, 0, 0, 0.8); overflow-wrap: break-word; word-break: break-word;"><div class="ab ca" style="box-sizing: inherit; display: flex; justify-content: center;"><div class="ch bg dx dy dz ea" style="box-sizing: inherit; margin: 0px 24px; max-width: 680px; min-width: 0px; width: 680px;"><h1 class="ms mt ev be mu mv ov mx my mz ow nb nc nd ox nf ng nh oy nj nk nl oz nn no np bj" data-selectable-paragraph="" id="161a" style="box-sizing: inherit; color: #242424; font-size: 24px; letter-spacing: -0.016em; line-height: 30px; margin: 1.25em 0px -0.28em;"><span style="font-family: inherit;">2. Dictionary</span></h1><p class="pw-post-body-paragraph kt ku ev kv b kw nq ky kz la nr lc ld le ns lg lh li nt lk ll lm nu lo lp lq eo bj" data-selectable-paragraph="" id="cee0" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 0.94em 0px -0.46em; word-break: break-word;"></p><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: -0.003em;">Can hold multiple unordered data in key-value pairs. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">Each value is associated with a unique key and any value from the dictionary can access with the associated key with it.</span></li></ul><p></p><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="a202" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Declaration: </strong></span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="d1b3" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var <span style="letter-spacing: -0.352px;">normal</span>Dictionary = [Int: String]() </span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="d1b3" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><br /></span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="d1b3" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">// an empty "<span style="letter-spacing: -0.352px;">normal</span>Dictionary" with Int type key & </span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="d1b3" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">String type </span><span style="font-family: inherit; letter-spacing: -0.022em;">value</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="d026" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"></p><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: -0.003em;">The key-value pair of a dictionary can be any type in swift. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">We can also declare with some initial values. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">Swift is a strongly typed language so we don’t have to tell the type of key-value pair when we declare a dictionary but we can also specify the type</span></li></ul><p></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="11a0" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var <span style="letter-spacing: -0.352px;">normal</span>Dictionary = [1: "John", 2: "Doe"]<br style="box-sizing: inherit;" />var <span style="letter-spacing: -0.352px;">normal</span>Dictionary : [Int: String] = [1: "John", 2: "Doe"]</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="987e" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Insert elements into a Dictionary: </strong>We can insert a value into the dictionary using a key. If the key already exists it will replace the previous value for this key elsewhere it will add a new value.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="b6a1" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary[3] = "Mihalos" // Add a value "Mihalos" for key "3"</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="4294" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Access Elements of Dictionary: </strong>We can access the elements of a dictionary by using the key. Also swift provided a list of methods to get values and check different parameters.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="731f" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary[2] //return the value of second key-value pair in dictionary </span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="64f1" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.count // return the number of elements in dictionary</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="15fd" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.isEmpty // return false if dictionary is </span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="1aa7" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">empty<span style="letter-spacing: -0.352px;">normal</span>Dictionary.capacity // return the number of elements in dictionary</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="6da1" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.first //get the first element from dictionary</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="29b0" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.max { a, b in a.value < b.value} // return key-value pair for maximum value</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="373f" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.min() { a, b in a.value < b.value} // return key-value pair for minimumvalue</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="4e4a" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.sorted( by: { $0.0 < $1.0 }) // reurn an sorted array of dictionary</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="d454" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.reversed() //return key-value pair in reverse order</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="d57e" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary.shuffled() //shuffle the key-value pair of dictionary</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="93f7" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Replace elements from a Dictionary: </strong>A value for a key is simply replaced by putting a new value for that key. We also can use “updateValue” method to replace the value for any key. But Remember “updateValue” method returns the previous value as output.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="130f" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary[3] = "Mike"<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Dictionary.updateValue("Duke", forKey: 3)<br style="box-sizing: inherit;" /></span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="4c17" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Remove elements from Dictionary: </strong>Dictionary elements<strong class="kv ew" style="box-sizing: inherit;"> </strong>can be removed in a different way.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="5dd5" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Dictionary[3] = nil // remove key-value pair for key "3"<strong class="od ew" style="box-sizing: inherit;"><br style="box-sizing: inherit;" /></strong><span style="letter-spacing: -0.352px;">normal</span>Dictionary.removeAll() // remove all element from dictionary arraysimpleDictionary.removeAll(keepingCapacity: true) remove all elements with keeping the capacity or not</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="ad9c" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Iterate</strong> <strong class="kv ew" style="box-sizing: inherit;">over</strong> <strong class="kv ew" style="box-sizing: inherit;">the</strong> <strong class="kv ew" style="box-sizing: inherit;">Dictionary: </strong>Every key-value pairs of a dictionary can be traverse using loop</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="0a92" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">for (key, value) in <span style="letter-spacing: -0.352px;">normal</span>Dictionary {<br style="box-sizing: inherit;" /> print(key, value)<br style="box-sizing: inherit;" />}</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="f9dd" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"><span style="font-family: inherit;">Instead of key-value pair we also can access only key or value from dictionary like this</span></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="2562" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">for key in <span style="letter-spacing: -0.352px;">normal</span>Dictionary.keys{<br style="box-sizing: inherit;" /> print(key)<br style="box-sizing: inherit;" />}</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="aaa7" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">for value in <span style="letter-spacing: -0.352px;">normal</span>Dictionary.values{<br style="box-sizing: inherit;" /> print(value)<br style="box-sizing: inherit;" />}</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="63f6" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"><span style="font-family: inherit;">We also can get the keys and values of a dictionary as an array.</span></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="111f" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">let keys = [Int](<span style="letter-spacing: -0.352px;">normal</span>Dictionary.keys) // return all the keys as an array<br style="box-sizing: inherit;" />let values = [String](<span style="letter-spacing: -0.352px;">normal</span>Dictionary.values) //return all the values as an array</span></span></pre><h1 class="ms mt ev be mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np bj" data-selectable-paragraph="" id="ee1f" style="box-sizing: inherit; color: #242424; font-size: 24px; letter-spacing: -0.016em; line-height: 30px; margin: 1.95em 0px -0.28em;"><span style="font-family: inherit;">3. Set</span></h1><p class="pw-post-body-paragraph kt ku ev kv b kw nq ky kz la nr lc ld le ns lg lh li nt lk ll lm nu lo lp lq eo bj" data-selectable-paragraph="" id="5a92" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 0.94em 0px -0.46em; word-break: break-word;"></p><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: -0.003em;">Set is an unordered collection of the same type of data. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">The main characteristic of the set is, set can not have any duplicate elements.</span></li></ul><p></p><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="9285" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Declaration: </strong>You can declare an empty Int type set like this</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="7f4b" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var <span style="letter-spacing: -0.352px;">normal</span>Set = Set<Int>() // </span><span face="-webkit-standard" style="font-size: 20px; letter-spacing: -0.06px; white-space: normal;">an empty Int type set</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="bbc2" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"></p><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: -0.003em;">The set can be any type in swift. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">We can also declare with some initial values. </span></li><li><span style="font-family: inherit; letter-spacing: -0.003em;">Swift is a strongly typed language so we don’t have to tell the type when we declare a set but we can also specify the type</span></li></ul><p></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="1eda" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var <span style="letter-spacing: -0.352px;">normal</span>Set = Set([10, 20, 30, 40, 20])<br style="box-sizing: inherit;" />var<span style="letter-spacing: -0.352px;">normal</span>Set: Set<String> = ["John", "Doe", "Hi", "John"]</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="6844" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"><span style="font-family: inherit;">You also can declare a set with some repeated values but you will get a single value for those repeated value cause set didn’t store any duplicate element.</span></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="5a08" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">var <span style="letter-spacing: -0.352px;">normal</span>Set = Set(repeatElement(1, count: 3)) </span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="5a08" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">// Initialize set with three ones but set will have </span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="5a08" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">only one into it.</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="2801" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Insert elements into a set: </strong>We can insert an element at the end of the array or in a valid position.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="6c6a" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set.insert(12) // //append element at the end of set</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="58b4" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Access Elements of a set: </strong>Like array you cannot access set elements using subscript syntax but you can check an element is in the set or not. Also swift provided a list of methods to get values and check different parameters.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="10f2" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set.contains(2) // check an element is in the set or not<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.count // return the number of elements in set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.isEmpty // return false if set is </span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="10f2" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"> empty <span style="letter-spacing: -0.352px;">normal</span>Set.capacity </span></span><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="10f2" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"> // return the number of elements in set</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="d1eb" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set.first //get the first element from set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.max() //get the max element from set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.min() //get the min element from set</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="214c" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set.sorted() // sort the set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.reversed() // reverse set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.shuffled() // shuffle set elements</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="cb4a" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Remove elements: </strong>Set<strong class="kv ew" style="box-sizing: inherit;"> </strong>elements<strong class="kv ew" style="box-sizing: inherit;"> </strong>can remove from different way & from different positions also.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="d815" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set.removeFirst() // remove first element from set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.remove(1) // remove from a specific index</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="99e2" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set.removeAll() // remove all elements from set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set.removeAll(keepingCapacity: true) // remove all elements with keeping the capacity or not</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="71bb" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Iterate</strong> <strong class="kv ew" style="box-sizing: inherit;">over</strong> <strong class="kv ew" style="box-sizing: inherit;">the set: </strong>Every element of a set can be traverse using loop</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="1ee6" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">for data in <span style="letter-spacing: -0.352px;">normal</span>Set {<br style="box-sizing: inherit;" /> print(data)<br style="box-sizing: inherit;" />}</span></span></pre><p class="pw-post-body-paragraph kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq eo bj" data-selectable-paragraph="" id="ecad" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; margin: 2.14em 0px -0.46em; word-break: break-word;"><span style="font-family: inherit;">The set type didn’t have any order so if you want to traverse in any order you can do it by swift provided methods</span></p><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="269e" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">for data in <span style="letter-spacing: -0.352px;">normal</span>Set.sorted() {<br style="box-sizing: inherit;" /> print(data)<br style="box-sizing: inherit;" />}</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="24e7" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;">for data in <span style="letter-spacing: -0.352px;">normal</span>Set.reversed() {<br style="box-sizing: inherit;" /> print(data)<br style="box-sizing: inherit;" />}</span></span></pre><ul style="box-sizing: inherit; list-style: none; margin: 0px; padding: 0px;"><li class="kt ku ev kv b kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq nz oa ob bj" data-selectable-paragraph="" id="00ed" style="box-sizing: inherit; color: #242424; font-size: 20px; letter-spacing: -0.003em; line-height: 32px; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2.14em; padding-left: 0px;"><span style="font-family: inherit;"><strong class="kv ew" style="box-sizing: inherit;">Different set operations: </strong>You also can perform different set operations within two sets and each of those operations will create a new set.</span></li></ul><pre class="ma mb mc md me oc od oe of ax og bj" style="background: repeat rgb(242, 242, 242); border-radius: 0px; border: medium; box-sizing: inherit; color: #242424; margin-bottom: 0px; margin-top: 56px; overflow-x: auto; padding: 20px;"><span class="oh mt ev od b gr oi oj l ok ol" data-selectable-paragraph="" id="bfa1" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: -0.09em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set2.union(<span style="letter-spacing: -0.352px;">normal</span>Set3).sorted() // perform union opertion within <span style="letter-spacing: -0.352px;">normal</span>Set2 & <span style="letter-spacing: -0.352px;">normal</span>Set3<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set2.intersection(<span style="letter-spacing: -0.352px;">normal</span>Set3) // perform intersection within two set<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set2.subtracting(<span style="letter-spacing: -0.352px;">normal</span>Set3) // perform subtracting<br style="box-sizing: inherit;" /><span style="letter-spacing: -0.352px;">normal</span>Set2.symmetricDifference(<span style="letter-spacing: -0.352px;">normal</span>Set3) // perform symmetricDifference</span></span><span class="oh mt ev od b gr om oj l ok ol" data-selectable-paragraph="" id="4874" style="box-sizing: inherit; display: block; font-size: 16px; letter-spacing: -0.022em; line-height: 1.18; margin-bottom: -0.09em; margin-top: 0.91em; min-width: fit-content; white-space: pre-wrap;"><span style="font-family: inherit;"><span style="letter-spacing: -0.352px;">normal</span>Set2.isSubset(of: <span style="letter-spacing: -0.352px;">normal</span>Set3) // check one set is subset of snother or not <span style="box-sizing: inherit;"><br style="box-sizing: inherit;" /></span><span style="letter-spacing: -0.352px;">normal</span>Set2.isSuperset(of: <span style="letter-spacing: -0.352px;">normal</span>Set3) // check superset or not <span style="box-sizing: inherit;"><br style="box-sizing: inherit;" /></span><span style="letter-spacing: -0.352px;">normal</span>Set2.isDisjoint(with: <span style="letter-spacing: -0.352px;">normal</span>Set3) // check disjoint or not</span></span></pre></div></div></div><div><br /></div>
There are some more interview questions answers for collections in this site. <a href="https://www.iosiqa.com/search/label/Collections" target="_blank">Visit here</a>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-90776752509467596502023-11-26T10:59:00.008+05:302023-11-26T11:07:14.429+05:30Difference between Value type and Reference type<h2 id="value-types" style="box-sizing: inherit; color: #333333; letter-spacing: 0.1px; line-height: 38px; margin-bottom: 12px; margin-top: 6px; position: relative;"><span style="font-family: inherit;">Value Types</span></h2><p style="box-sizing: inherit; color: #444444; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit; font-size: 18px;">A <em style="box-sizing: inherit;">value type</em> instance is an independent instance and holds its data in its own memory allocation. There are a few different <em style="box-sizing: inherit;">value types</em>: </span><span style="background-color: #f8f8f8; font-family: monospace; font-size: 14px;">Struct, Enum, Tupple.</span></p><h4 id="struct" style="box-sizing: inherit; color: #333333; font-size: 18px; letter-spacing: 0.1px; line-height: 34px; margin: 0px 0px 2px; position: relative;"><span style="font-family: inherit;">Struct</span></h4><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">Let’s experiment with <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">struct</code> and prove that they’re <em style="box-sizing: inherit;">value types</em>:</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit; letter-spacing: 0.1px;">Add the following code to your playground:</span></p><pre style="background-color: #f8f8f8; border-color: rgba(222, 229, 231, 0.6); box-sizing: inherit; display: inline-block; font-size: 14px; letter-spacing: 0.1px; margin-bottom: 31.2px; margin-left: -18px; overflow-x: scroll; padding: 1.4em; width: 636px;"><code class="language-swift" style="box-sizing: inherit; line-height: 22px; padding: 0px; word-break: normal;"><span style="font-family: inherit;"><span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 1</span>
<span class="hljs-class" style="box-sizing: inherit;"><span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">struct</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: bold;">Car</span> </span>{
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">let</span> brand: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">String</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">var</span> model: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">String</span>
}
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 2</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">var</span> golf = <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Car</span>(brand: <span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Volkswagen"</span>, model: <span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Golf"</span>)
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 3</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">let</span> polo = golf
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 4</span>
golf.model = <span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Golf 2019"</span>
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 5</span>
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(golf)
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(polo)
</span></code></pre><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">In the code above, you will:</span></p><span style="color: #444444; font-family: inherit; font-size: 18px; letter-spacing: 0.1px;"><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Car</code><span style="font-family: inherit; letter-spacing: 0.1px;"> struct with </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">brand</code><span style="font-family: inherit; letter-spacing: 0.1px;"> and </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">model</code><span style="font-family: inherit; letter-spacing: 0.1px;"> properties.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a new instance of </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Car</code><span style="font-family: inherit; letter-spacing: 0.1px;"> named </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">golf</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a copy of the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">golf</code><span style="font-family: inherit; letter-spacing: 0.1px;"> i</span><span style="font-family: inherit; letter-spacing: 0.1px;">nstance, named </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">polo</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Change the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">golf.model</code><span style="font-family: inherit; letter-spacing: 0.1px;"> variabl</span><span style="font-family: inherit; letter-spacing: 0.1px;">e to </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Golf 2019</code></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Print the 2 different instan</span><span style="font-family: inherit; letter-spacing: 0.1px;">ces. The first </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">print</code><span style="font-family: inherit; letter-spacing: 0.1px;"> statement prints </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Car(brand: "Volkswagen", model: "Golf 2019")</code><span style="font-family: inherit; letter-spacing: 0.1px;"> in the Console. The second one prints </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Car(brand: "Volkswagen", model: "Golf")</code><span style="font-family: inherit; letter-spacing: 0.1px;">. Even if </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">polo</code><span style="font-family: inherit; letter-spacing: 0.1px;"> is a copy of </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">golf</code><span style="font-family: inherit; letter-spacing: 0.1px;">, the instances remain independent with their own unique data copies.</span></li></ul></span><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">With this simple playground, we’ve confirmed that <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">structs</code> are indeed <em style="box-sizing: inherit;">value types</em>.</span></p><h4 id="enum" style="box-sizing: inherit; color: #333333; font-size: 18px; letter-spacing: 0.1px; line-height: 34px; margin: 0px 0px 2px; position: relative;"><span style="font-family: inherit;">Enum</span></h4><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">To check that <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">enums</code> are <em style="box-sizing: inherit;">value types</em>, add this code to the playground:</span></p><pre style="background-color: #f8f8f8; border-color: rgba(222, 229, 231, 0.6); box-sizing: inherit; display: inline-block; font-size: 14px; letter-spacing: 0.1px; margin-bottom: 31.2px; margin-left: -18px; overflow-x: scroll; padding: 1.4em; width: 636px;"><code class="language-swift" style="box-sizing: inherit; line-height: 22px; padding: 0px; word-break: normal;"><span style="font-family: inherit;"><span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 1</span>
<span class="hljs-class" style="box-sizing: inherit;"><span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">enum</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: bold;">Language</span> </span>{
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">case</span> italian
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">case</span> english
}
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 2</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">var</span> italian = <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Language</span>.italian
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 3</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">let</span> english = italian
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 4</span>
italian = .english
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 5</span>
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(italian)
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(english)
</span></code></pre><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">In the code above, you will:</span></p><span style="color: #444444; font-family: inherit; font-size: 18px; letter-spacing: 0.1px;"><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Language</code><span style="font-family: inherit; letter-spacing: 0.1px;"> enum with </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">italian</code><span style="font-family: inherit; letter-spacing: 0.1px;"> and </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">english</code><span style="font-family: inherit; letter-spacing: 0.1px;"> cases.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a new instance of </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Language</code><span style="font-family: inherit; letter-spacing: 0.1px;"> for the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">italian</code><span style="font-family: inherit; letter-spacing: 0.1px;"> language.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a copy </span><span style="font-family: inherit; letter-spacing: 0.1px;">of the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">italian</code><span style="font-family: inherit; letter-spacing: 0.1px;"> instance, named </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">english</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Change t</span><span style="font-family: inherit; letter-spacing: 0.1px;">he </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">italian</code><span style="font-family: inherit; letter-spacing: 0.1px;"> instance to </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">english</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Print the </span><span style="font-family: inherit; letter-spacing: 0.1px;">two different instances. The first </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">print</code><span style="font-family: inherit; letter-spacing: 0.1px;"> statement prints </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">english</code><span style="font-family: inherit; letter-spacing: 0.1px;">, and the second one prints </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">italian</code><span style="font-family: inherit; letter-spacing: 0.1px;">. Even if </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">english</code><span style="font-family: inherit; letter-spacing: 0.1px;"> is a copy of </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">italian</code><span style="font-family: inherit; letter-spacing: 0.1px;">, the instances remain independent.</span></li></ul></span><h4 id="tuple" style="box-sizing: inherit; color: #333333; font-size: 18px; letter-spacing: 0.1px; line-height: 34px; margin: 0px 0px 2px; position: relative;"><span style="font-family: inherit;">Tuple</span></h4><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">The last <em style="box-sizing: inherit;">value type</em> that we'll explore is <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">tuple</code>. A tuple type is a comma-separated list of zero or more types, enclosed in parentheses. You can access its values using the dot (<code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">.</code>) notation followed by the index of the value.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">You can also name the elements in a tuple and use the names to access the different values.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">Add the following code to the playground:</span></p><pre style="background-color: #f8f8f8; border-color: rgba(222, 229, 231, 0.6); box-sizing: inherit; display: inline-block; font-size: 14px; letter-spacing: 0.1px; margin-bottom: 31.2px; margin-left: -18px; overflow-x: scroll; padding: 1.4em; width: 636px;"><code class="language-swift" style="box-sizing: inherit; line-height: 22px; padding: 0px; word-break: normal;"><span style="font-family: inherit;"><span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 1</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">var</span> ironMan = (<span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Tony"</span>, <span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Stark"</span>)
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 2</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">let</span> parent = ironMan
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 3</span>
ironMan.<span class="hljs-number" style="box-sizing: inherit; color: teal;">0</span> = <span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Alfred"</span>
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 4</span>
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(ironMan)
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(parent)
</span></code></pre><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">In the code above, you will:</span></p><span style="color: #444444; font-family: inherit; font-size: 18px; letter-spacing: 0.1px;"><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create an </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">ironMan</code><span style="font-family: inherit; letter-spacing: 0.1px;"> tuple </span><span style="font-family: inherit; letter-spacing: 0.1px;">with the strings </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Tony</code><span style="font-family: inherit; letter-spacing: 0.1px;"> and </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Stark</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a copy of the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">ironMan</code><span style="font-family: inherit; letter-spacing: 0.1px;"> inst</span><span style="font-family: inherit; letter-spacing: 0.1px;">ance, named </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">parent</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Change the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">ironMan.0</code><span style="font-family: inherit; letter-spacing: 0.1px;"> inde</span><span style="font-family: inherit; letter-spacing: 0.1px;">x to </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Alfred</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Print the 2 different instanc</span><span style="font-family: inherit; letter-spacing: 0.1px;">es. The first </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">print</code><span style="font-family: inherit; letter-spacing: 0.1px;">, prints </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">("Alfred", "Stark")</code><span style="font-family: inherit; letter-spacing: 0.1px;"> and the second one, prints </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">("Tony", "Stark")</code><span style="font-family: inherit; letter-spacing: 0.1px;">. Again, the instances remain independent.</span></li></ul></span><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">You can now be certain that <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">structs</code>, <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">enums</code>, and <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">tuples</code> are <em style="box-sizing: inherit;">value types</em>! </span></p><h3 id="when-to-use-value-types" style="box-sizing: inherit; color: #333333; letter-spacing: 0.1px; line-height: 38px; margin-bottom: 12px; margin-top: 6px; position: relative;"><span style="font-family: inherit;">When to Use Value Types</span></h3><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">Use <em style="box-sizing: inherit;">value types</em> when comparing instance data with <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">==</code> makes sense.<br style="box-sizing: inherit;" /><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">==</code> checks if every property of the two instances is the same.<br style="box-sizing: inherit;" />With <em style="box-sizing: inherit;">value types</em> you always get a unique, copied instance, and you can be sure that no other part of your app is changing the data under the hood. This is especially helpful in multi-threaded environments where a different thread could alter your data.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">Use a <em style="box-sizing: inherit;">value type</em> when you want copies to have an independent state, and the data will be used in code across multiple threads.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">In Swift, <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">Array</code>, <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">String</code>, and <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">Dictionary</code> are all value types.</span></p><h2 id="reference-types" style="box-sizing: inherit; color: #333333; letter-spacing: 0.1px; line-height: 38px; margin-bottom: 12px; margin-top: 6px; position: relative;"><span style="font-family: inherit;">Reference Types</span></h2><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">In Swift, <em style="box-sizing: inherit;">reference type</em> instances share a single copy of their data, so that every new instance will point to the same address in memory. A typical example is a <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">class</code>, <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">function</code>, or <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">closure</code>.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">To explore these, add a new function to your playground:</span></p><pre style="background-color: #f8f8f8; border-color: rgba(222, 229, 231, 0.6); box-sizing: inherit; display: inline-block; font-size: 14px; letter-spacing: 0.1px; margin-bottom: 31.2px; margin-left: -18px; overflow-x: scroll; padding: 1.4em; width: 636px;"><code class="language-swift" style="box-sizing: inherit; line-height: 22px; padding: 0px; word-break: normal;"><span style="font-family: inherit;"><span class="hljs-function" style="box-sizing: inherit;"><span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">func</span> <span class="hljs-title" style="box-sizing: inherit; color: #990000; font-weight: bold;">address</span><T: AnyObject><span class="hljs-params" style="box-sizing: inherit;">(of object: T)</span></span> -> <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Int</span> {
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">return</span> <span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">unsafeBitCast</span>(object, to: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Int</span>.<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">self</span>)
}
</span></code></pre><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">This function prints the address of an object, which will help you check whether you're referencing the same instance or not.</span></p><h3 id="class" style="box-sizing: inherit; color: #333333; letter-spacing: 0.1px; line-height: 38px; margin-bottom: 12px; margin-top: 6px; position: relative;"><span style="font-family: inherit;">Class</span></h3><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">The first <em style="box-sizing: inherit;">reference type</em> that you'll look at is a <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">class</code>.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">Add the following code to your playground:</span></p><pre style="background-color: #f8f8f8; border-color: rgba(222, 229, 231, 0.6); box-sizing: inherit; display: inline-block; font-size: 14px; letter-spacing: 0.1px; margin-bottom: 31.2px; margin-left: -18px; overflow-x: scroll; padding: 1.4em; width: 636px;"><code class="language-swift" style="box-sizing: inherit; line-height: 22px; padding: 0px; word-break: normal;"><span style="font-family: inherit;"><span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 1</span>
<span class="hljs-class" style="box-sizing: inherit;"><span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">class</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: bold;">Dog</span>: <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: bold;">CustomStringConvertible</span> </span>{
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">var</span> age: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Int</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">var</span> weight: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Int</span>
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 2</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">var</span> description: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">String</span> {
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">return</span> <span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Age \(age) - Weight \(weight)"</span>
}
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 3</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">init</span>(age: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Int</span>, weight: <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Int</span>) {
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">self</span>.age = age
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">self</span>.weight = weight
}
}
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 4</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">let</span> doberman = <span class="hljs-type" style="box-sizing: inherit; color: #445588; font-weight: bold;">Dog</span>(age: <span class="hljs-number" style="box-sizing: inherit; color: teal;">1</span>, weight: <span class="hljs-number" style="box-sizing: inherit; color: teal;">70</span>)
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 5</span>
<span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">let</span> chihuahua = doberman
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 6</span>
doberman.age = <span class="hljs-number" style="box-sizing: inherit; color: teal;">2</span>
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 7</span>
chihuahua.weight = <span class="hljs-number" style="box-sizing: inherit; color: teal;">10</span>
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 8</span>
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(doberman)
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(chihuahua)
<span class="hljs-comment" style="box-sizing: inherit; color: #999988; font-style: italic;">// 9</span>
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(address(of: doberman))
<span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(address(of: chihuahua))
</span></code></pre><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">In the code above, you will:</span></p><span style="color: #444444; font-family: inherit; font-size: 18px; letter-spacing: 0.1px;"><ul style="text-align: left;"><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a new </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">class</code><span style="font-family: inherit; letter-spacing: 0.1px;"> na</span><span style="font-family: inherit; letter-spacing: 0.1px;">med </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Dog</code><span style="font-family: inherit; letter-spacing: 0.1px;">, that conforms to </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">CustomStringConvertible</code><span style="font-family: inherit; letter-spacing: 0.1px;"> to print the custom descriptions of the object.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Define the custom desc</span><span style="font-family: inherit; letter-spacing: 0.1px;">ription of the object.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a new </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">init</code><span style="font-family: inherit; letter-spacing: 0.1px;"> functio</span><span style="font-family: inherit; letter-spacing: 0.1px;">n. This is needed because, unlike a </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">struct</code><span style="font-family: inherit; letter-spacing: 0.1px;">, a </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">class</code><span style="font-family: inherit; letter-spacing: 0.1px;"> doesn't automatically create an initialization function based on the variables of the object.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">doberman</code><span style="font-family: inherit; letter-spacing: 0.1px;"> in</span><span style="font-family: inherit; letter-spacing: 0.1px;">stance of </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Dog</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Create a copy of </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">doberman</code><span style="font-family: inherit; letter-spacing: 0.1px;">, na</span><span style="font-family: inherit; letter-spacing: 0.1px;">med </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">chihuahua</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Change the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">doberman.age</code><span style="font-family: inherit; letter-spacing: 0.1px;"> t</span><span style="font-family: inherit; letter-spacing: 0.1px;">o </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">2</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Change the </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">chihuahua.weight</code><span style="font-family: inherit; letter-spacing: 0.1px;"> t</span><span style="font-family: inherit; letter-spacing: 0.1px;">o </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">10</code><span style="font-family: inherit; letter-spacing: 0.1px;">.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Print the description of th</span><span style="font-family: inherit; letter-spacing: 0.1px;">e two different instances. The first </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">print</code><span style="font-family: inherit; letter-spacing: 0.1px;">, prints </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Age 2 - Weight 10</code><span style="font-family: inherit; letter-spacing: 0.1px;">, and the second one prints the same; </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">Age 2 - Weight 10</code><span style="font-family: inherit; letter-spacing: 0.1px;">. This is because you're actually referencing the same object.</span></li><li><span style="font-family: inherit; letter-spacing: 0.1px;">Print the address of th</span><span style="font-family: inherit; letter-spacing: 0.1px;">e two different instances. With these prints, you'll be sure that you're referencing the same address. You'll see that both </span><code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; letter-spacing: 0.1px; padding: 4px 6px; word-break: normal;">print</code><span style="font-family: inherit; letter-spacing: 0.1px;"> statements print the same value.</span></li></ul></span><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">You can rest assured that a <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">class</code> is a <em style="box-sizing: inherit;">reference type</em>.</span></p><h3 id="functions-and-closures" style="box-sizing: inherit; color: #333333; letter-spacing: 0.1px; line-height: 38px; margin-bottom: 12px; margin-top: 6px; position: relative;"><span style="font-family: inherit;">Functions and Closures</span></h3><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">A <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">closure</code> is used to refer to a function along with the variables from its scope that it encapsulates. Functions are essentially closures that store references to variables in their context.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">Take a look at the code below:</span></p><pre style="background-color: #f8f8f8; border-color: rgba(222, 229, 231, 0.6); box-sizing: inherit; display: inline-block; font-size: 14px; letter-spacing: 0.1px; margin-bottom: 31.2px; margin-left: -18px; overflow-x: scroll; padding: 1.4em; width: 636px;"><code class="language-swift" style="box-sizing: inherit; line-height: 22px; padding: 0px; word-break: normal;"><span style="font-family: inherit;"><span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">let</span> closure = { <span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(<span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Test"</span>) }
<span class="hljs-function" style="box-sizing: inherit;"><span class="hljs-keyword" style="box-sizing: inherit; color: #333333; font-weight: bold;">func</span> <span class="hljs-title" style="box-sizing: inherit; color: #990000; font-weight: bold;">function</span><span class="hljs-params" style="box-sizing: inherit;">()</span></span> -> (){ <span class="hljs-built_in" style="box-sizing: inherit; color: #0086b3;">print</span>(<span class="hljs-string" style="box-sizing: inherit; color: #dd1144;">"Test"</span>) }
closure()
function()
</span></code></pre><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">They both do the same thing.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">You can find more info about closures <a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID104" rel="noopener" style="box-sizing: inherit; color: #1aafd0; cursor: pointer; text-decoration: none;" target="_blank">in Swift's docs</a>.</span></p><h3 id="when-to-use-reference-types" style="box-sizing: inherit; color: #333333; letter-spacing: 0.1px; line-height: 38px; margin-bottom: 12px; margin-top: 6px; position: relative;"><span style="font-family: inherit;">When to Use Reference Types</span></h3><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">Use a <em style="box-sizing: inherit;">reference type</em> when comparing instance identity with <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">===</code>makes sense. <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">===</code> checks if two objects share the same memory address.</span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">They’re also useful when you want to create a shared, mutable state.</span></p><p><span style="font-family: inherit;"><span face=""Open Sans", sans-serif" style="background-color: white; font-size: 18px; letter-spacing: 0.1px;"></span><span face=""Open Sans", sans-serif" style="background-color: white; font-size: 18px; letter-spacing: 0.1px;"></span><span face=""Open Sans", sans-serif" style="background-color: white; font-size: 18px; letter-spacing: 0.1px;"></span><span face=""Open Sans", sans-serif" style="background-color: white; font-size: 18px; letter-spacing: 0.1px;"></span><span face=""Open Sans", sans-serif" style="background-color: white; font-size: 18px; letter-spacing: 0.1px;"></span><span face=""Open Sans", sans-serif" style="background-color: white; font-size: 18px; letter-spacing: 0.1px;"></span></span></p><p style="box-sizing: inherit; color: #444444; font-size: 18px; letter-spacing: 0.1px; line-height: 32px; margin-bottom: 26px; margin-top: 0px;"><span style="font-family: inherit;">As a general rule, start by creating your instance as an <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">enum</code>, then move to a <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">struct</code> if you need more customization, and finally move to <code style="background-color: #f8f8f8; box-sizing: inherit; font-size: 14px; padding: 4px 6px; word-break: normal;">class</code> when needed.</span></p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-32393454503634805342021-09-26T14:06:00.007+05:302023-11-26T14:55:25.204+05:30iOS 15 Release date , features - All answers related to iOS 15<div style="text-align: left;"><span style="font-family: arial;"><br /><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqfysrlGOzlRg7N-5ANNN9qV2dkGARFS7bwWOj1622PFju6WtVtcrZf06eJImIMM5i9Y4yA31CA0OrcYZdMmYMv0-mC2un9nFAq2k1jo0RyXpXhY91AIWk3pnGgXGsT_eWMBsQkNijZ48/s384/IOS_15_logo.png" style="margin-left: auto; margin-right: auto;"><img alt="iOS 15 Release date , features" border="0" data-original-height="384" data-original-width="384" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqfysrlGOzlRg7N-5ANNN9qV2dkGARFS7bwWOj1622PFju6WtVtcrZf06eJImIMM5i9Y4yA31CA0OrcYZdMmYMv0-mC2un9nFAq2k1jo0RyXpXhY91AIWk3pnGgXGsT_eWMBsQkNijZ48/w200-h200/IOS_15_logo.png" title="iOS 15 Release date , features" width="200" /></a><br /><br /></td></tr><tr><td class="tr-caption" style="text-align: center;"><br /><a href="https://amzn.to/3IXPLrd" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><img border="0" data-original-height="300" data-original-width="600" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYiSsWZWODMLEviuJsOjCCZdNgsapOh1RiaLlt_NoD8kHBEj4UP-MHSbkJuNdipvTVhbn5-WE9xEPY8weZ-AsL3yzMNlkPirtk9SgYERS36Ctje7KF7uqR4oDbV_uF6DtgAADStP5qJjHPmIo9nwf9jKCtzGiiuATEYAEKFEcYNQUD0fvyMjYua9cs/w400-h200/amazon-prime-day-sale-live-sticky-600x300.png" width="400" /></a></td></tr></tbody></table><h3><br /></h3><h3>FAQ for iOS 15</h3></span></div><div style="text-align: left;"><span style="font-family: arial;"><br /></span></div><div style="text-align: left;"><span style="font-family: arial;">Q. Which is latest iOS version?</span></div><div style="text-align: left;"><span style="font-family: arial;">A. iOS 15 is latest iOS Version.</span></div><div style="text-align: left;"><span style="font-family: arial;"><br /></span></div><div style="text-align: left;"><span style="font-family: arial;">Q. What is iOS 15 release date?</span></div><div style="text-align: left;"><span style="font-family: arial;">A. iOS 15 is released on 20 September, 2021 worldwide.</span></div><div style="text-align: left;"><span style="font-family: arial;"><br /></span></div><div style="text-align: left;"><span style="font-family: arial;">Q. Is there any version after iOS 15 ?</span></div><div style="text-align: left;"><span style="font-family: arial;">A. iOS 15.1 beta version is released on 21 September, 2021.</span></div><div style="text-align: left;"><span style="font-family: arial;"><br /></span></div><div style="text-align: left;"><span style="font-family: arial;">Q. What is iOS 15 release date in India?</span></div><div style="text-align: left;"><span style="font-family: arial;">A. iOS 15 is released on 20 Sepember, 2021 in India.</span></div><div style="text-align: left;"><span style="font-family: "Helvetica Neue";"><br /></span></div><div style="text-align: left;"><div><h2 style="background-color: white; box-sizing: border-box; color: #262626; font-family: Roboto, sans-serif; line-height: 1.1; margin: 20px 0px 10px; padding: 0px 0px 5px;"><span style="box-sizing: border-box; font-weight: normal;"><span style="font-size: small;"><h3>How to install iOS 15 now</h3></span></span></h2></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">Step 1: Goto settings menu in iPhone</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">Step 2: Scroll down and tap on General</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">Step 3: Check if iOS 15 update is there or not.</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">Step 4: If yes, tap on the Download option displayed on the screen.</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">Step 5: Apple also provides the option to install the update while you are asleep. Select the best-suited option and install the iOS 15 update.</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">Once the iOS 15 update is installed, your iPhone will boot up. It will take some time due to update size is almost 3 GB. Before starting with the installation process, below are a few tips to keep in mind.</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;"><h3>Tips to keep in mind before installing iOS 15</h3></span></div><div><span style="font-family: Helvetica Neue;">–Connect your iOS Device with stable high speed wifi.</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">–As iOS 15 update is almost 3 GB update, keep such space in memory</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;">–Installation will take around half an hour. So keep your iOS device in charging mode.</span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;"><br /></span></div><div><span style="font-family: Helvetica Neue;"><div><h3>Important features in iOS 15 updates for daily usse</h3></div><div><br /></div><div><h4><ul style="text-align: left;"><li>Facetime</li></ul></h4></div><div>iOS 15 includes a new features for FaceTime. It will help apple to compete with other online meeting service like JioMeet, Zoom, Microsoft Team, etc.</div><div><br /></div><div>Features in Facetime:</div><div>Spatial Audio support</div><div>Portrait mode for videos</div><div>Grid view for videos</div><div>Join FaceTime calls from the web on Android and Windows</div><div>FaceTime links</div><div>SharePlay for sharing content during FaceTime, including screen sharing, music, and more</div><div><br /></div><div><h4><ul style="text-align: left;"><li>Messages</li></ul></h4></div><div>Apple has also announced new features for Messages, including new ways to easily access content that people send you. Shared Stacks will bring over content from Messages to Photos app.</div><div><br /></div><div><h4><ul style="text-align: left;"><li>Notifications</li></ul></h4></div><div><br /></div><div>Notification design is changed and anyone can choose to view summary.</div><div>When you enable DND, your status will be shown to other people via the Messages app</div><div>Notification design has been changed with large icons.</div><div>You can set different “Focus” status that change your Home Screen, notification preferences, and more.</div><div><br /></div><div><h4><ul style="text-align: left;"><li>Photos</li></ul></h4></div><div>Photos app now includes OCR capability. Due to this, it enables covert from image to text with artificial intelligence.</div><div>Photos can also be searched in spotlight. So it can be easily searched.</div><div><br /></div><div><h4><ul style="text-align: left;"><li>Weather</li></ul></h4></div><div>The Weather app has been revamped with lots of new data.</div><div>Layout design of the Weather app changes based on the weather in your current location.</div><div><br /></div><div><h4><ul style="text-align: left;"><li>Safari</li></ul></h4></div><div>iOS 15 brings a completely new design to Safari. Controls are brought to the bottom of the screen so that they are easier to reach with one hand.</div><div><br /></div><div>There is a new, compact tab bar that floats at the bottom of the screen so that users can easily swipe between tabs, and it also contains a Smart Search field. Tab Groups allow users to save their tabs in a folder and sync across the iPhone, iPad, and Mac. In addition, there is a new tab overview grid view.</div><div><br /></div><div>Users can simply pull down a web page to refresh it and there is now support for voice search. Safari also gains a customizable start page and mobile web extensions for the first time.</div><div><br /></div><div><br /></div><div>These are some important features that normal users can see changes. Otherwise apple has changed in many in-built application but it is not daily use for every people.</div><div><br /></div><div>If you have any doubt or any question related iOS 15 release, please comment on box below.</div></span></div></div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0Unnamed Road, Maharashtra 424101, India20.5481563 74.9751505-12.357191376927648 39.8189005 53.453503976927649 110.1314005tag:blogger.com,1999:blog-1475345808851342812.post-40546172986251533622021-04-08T08:39:00.004+05:302021-04-08T08:39:44.848+05:30What is push notification payload maximum size?<div style="text-align: left;">Apple Push Notification service (APNs) refuses a notification if the total size of its payload exceeds the following limits:<br /><br /></div><div style="text-align: left;"><ul style="text-align: left;"><li>FCM - 2 Kb</li><li>VOIP Notification - 5 kb</li><li>For all other remote notification - 4 kb</li></ul><div><br /></div></div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-57717010246815308672021-04-08T08:29:00.006+05:302021-04-08T08:29:52.697+05:30What is trailing closure?<p> If the last parameter to a function is a closure, Swift lets you use special syntax called trailing closure syntax. Rather than pass in your closure as a parameter, you pass it directly after the function inside braces.</p><p>To demonstrate this, here’s our travel() function again. It accepts an action closure so that it can be run between two print() calls:</p><p>func travel(action: () -> Void) {</p><p> print("I'm getting ready to go.")</p><p> action()</p><p> print("I arrived!")</p><p>}</p><p>Because its last parameter is a closure, we can call travel() using trailing closure syntax like this:</p><p>travel() {</p><p> print("I'm driving in my car")</p><p>}</p><p>In fact, because there aren’t any other parameters, we can eliminate the parentheses entirely:</p><p>travel {</p><p> print("I'm driving in my car")</p><p>}</p><p>Trailing closure syntax is extremely common in Swift, so it’s worth getting used to.</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com2tag:blogger.com,1999:blog-1475345808851342812.post-62752536511661144032021-04-06T23:31:00.000+05:302021-04-06T23:31:02.449+05:30Difference between method and function<p>Methods belong to classes, structs, and enums, whereas functions do not.</p><p>Methods always belong to a data type, they have a concept of self that functions do not. This is a special value passed in by Swift, and it refers to whatever instance the method was called on.</p><p>Swift uses the same keyword, func, for both functions and methods.</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-58676544222019716962021-04-06T23:19:00.000+05:302021-04-06T23:19:24.787+05:30Difference between type method and Instance method?<p><b>Type Method: </b>We can call the method using Struct, Class, or Enum name. The method can be static or Class for making such methods. Static method can not be override but class method can be override.</p><p><b>Instance Method:</b> We can call normal method using making instance of strcut or class. This methods are called instance method.</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-59199536650674339222021-04-06T22:39:00.006+05:302023-03-18T12:35:15.605+05:30Difference between Swift and Objective C<p><span style="font-family: inherit;"> </span></p><table style="background-color: white; border-collapse: collapse; border-spacing: 0px; border: 0px; color: #40424e; display: block; letter-spacing: 0.162px; margin: 25px auto; overflow-x: scroll; padding: 0px; vertical-align: baseline; width: 604.656px;"><tbody style="border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><tr style="background-color: var(--gfg-body-color); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><th style="border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; text-align: left; vertical-align: baseline;"><div><span style="font-family: courier;"><br /></span></div></th><th style="border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; text-align: left; vertical-align: baseline;"><p><span><span style="font-family: courier;"> </span><span style="font-family: inherit; font-size: medium;">SWIFT </span></span></p></th><th style="border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; text-align: left; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"> OBJECTIVE C</span></p></th></tr><tr style="background-color: var(--gfg-darkgrey); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Swift is a general-purpose, high-level programming language that is highly concerned about safety, and performance.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Objective C is a general-purpose language that is considered a superset of C language it was designed with the aim of providing object-oriented capabilities.</span></p></td></tr><tr style="background-color: var(--gfg-body-color); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It was developed by Chris Lattner with eventual collaboration with other programmers at Apple.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It was developed by Brad Cox and Tom Love at their company Stepstone.</span></p></td></tr><tr style="background-color: var(--gfg-darkgrey); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It was influenced by Objective C, Rust, Ruby, and Python.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It was influenced by C and Smalltalk.</span></p></td></tr><tr style="background-color: var(--gfg-body-color); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Swift first appeared on the year 2014.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Objective C first appeared on the year 1984.</span></p></td></tr><tr style="background-color: var(--gfg-darkgrey); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Swift is a static type.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Objective C is dynamic type.</span></p></td></tr><tr style="background-color: var(--gfg-body-color); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Swift is apache licensed open-source project.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Objective C is licensed under General Public License.</span></p></td></tr><tr style="background-color: var(--gfg-darkgrey); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It only has classes.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It has both Structs and classes.</span></p></td></tr><tr style="background-color: var(--gfg-body-color); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It was designed for building apps for iOS, Mac, Apple TV, and Apple Watch.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Objective C was designed to be Smalltalk messaging features.</span></p></td></tr><tr style="background-color: var(--gfg-darkgrey); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Swift polymorphism does not exist directly.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Polymorphism in Objective C exists directly in compile time.</span></p></td></tr><tr style="background-color: var(--gfg-body-color); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It uses true and false values.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">It uses YES and NO values and also BOOl.</span></p></td></tr><tr style="background-color: var(--gfg-darkgrey); border: 0px; box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline;"><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;"><br /></span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium; letter-spacing: 0.162px;">Swift has multiple types of templates than Objective C.</span></p></td><td style="border: 0px; box-sizing: border-box; margin: 0px; padding: 14px 10px; vertical-align: baseline;"><p><span style="font-family: inherit; font-size: medium;">Objective C lacks templates than Swift.</span></p></td></tr></tbody></table>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-55772546660411733912021-04-06T22:28:00.001+05:302021-04-06T22:28:12.223+05:30What is remote config in firebase<p>Change the behavior and appearance of your app without publishing an app update, at no cost, for unlimited daily active users.</p><p>Firebase Remote Config is a cloud service that lets you change the behavior and appearance of your app without requiring users to download an app update. When using Remote Config, you create in-app default values that control the behavior and appearance of your app. Then, you can later use the Firebase console or the Remote Config backend APIs to override in-app default values for all app users or for segments of your user base. Your app controls when updates are applied, and it can frequently check for updates and apply them with a negligible impact on performance.</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-50197116009470192032021-04-05T17:27:00.003+05:302021-04-05T17:27:32.353+05:30What is KVO?<p>Key-value observing is the ability for Swift to attach code to variables, so that whenever the variable is changed the code runs. It’s similar to property observers (willSet and didSet ), except KVO is for adding observers outside of the type definition.</p><p><br /></p><p>KVO isn’t terribly nice in pure Swift code, because it relies on the Objective-C runtime – you need to use @objc classes that inherit from NSObject, then mark each of your properties with @objc dynamic.</p><p><br /></p><p>For example, we could create a Car class like this:</p><p><br /></p><p>@objc class Car: NSObject {</p><p> @objc dynamic var name = "BMW"</p><p>}</p><p><br /></p><p>let bmw= Car()</p><p>You could then observe that user’s name changing like this:</p><p><br /></p><p>bmw.observe(\Car.name, options: .new) { car, change in</p><p> print("I'm now called \(car.name)")</p><p>}</p><p>That asks BMW to watch for new values coming in, then prints the person’s name as soon as the new value is set.</p><p><br /></p><p>To try it out, just change the car's name to something else:</p><p><br /></p><p>bmw.name = "Mercedese"</p><p>That will print “I’m now called Mercedese.”</p><p><br /></p><p>Although KVO is unpleasant in pure Swift code, it’s better when working with Apple’s own APIs – they are all automatically both @objc and dynamic because they are written in Objective-C.</p><p><br /></p><p>However, one warning: even though large parts of UIKit might work with KVO, this is a coincidence rather than a promise – Apple makes no guarantees about UIKit remaining KVO-compatible in the future.</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-22624146004749683242021-04-04T16:17:00.000+05:302021-04-04T16:17:09.592+05:30What is Protocol Oriented Programming?Protocol-Oriented Programming is a new programming paradigm ushered in by Swift 2.0. In the Protocol-Oriented approach, we start designing our system by defining protocols. We rely on new concepts: protocol extensions, protocol inheritance, and protocol compositions.<div><br /></div><div>In Swift, value types are preferred over classes. However, object-oriented concepts don’t work well with structs and enums: a struct cannot inherit from another struct, neither can an enum inherit from another enum. </div><div><br /></div><div>On the other hand, value types can inherit from protocols, even multiple protocols. Thus, with POP, value types have become first-class citizens in Swift.</div><div><br /></div><div><b>Pillars of POPs</b></div><div><br /></div><div><div><b>Protocol Extensions</b></div><div>Protocols serve as blueprints: they tell us what adopters shall implement, but you can’t provide implementation within a protocol. What if we need to define default behavior for conforming types? We need to implement it in a base class, right? Wrong! Having to rely on a base class for default implementation would eclipse the benefits of protocols. Besides, that would not work for value types. Luckily, there is another way: protocol extensions are the way to go! In Swift, you can extend a protocol and provide a default implementation for methods, computed properties, subscripts, and convenience initializers. In the following example, I provided default implementation for the type method uid().</div><div><br /></div><div>extension Entity {</div><div> static func uid() -> String {</div><div> return UUID().uuidString</div><div> }</div><div>}</div><div>swift</div><div>Now types that adopt the protocol need not implement the uid() method anymore.</div><div><br /></div><div>struct Order: Entity {</div><div> var name: String</div><div> let uid: String = Order.uid()</div><div>}</div><div>let order = Order(name: "My Order")</div><div>print(order.uid)</div><div>// 4812B485-3965-443B-A76D-72986B0A4FF4</div><div><b><br /></b></div><div><b>Protocol Inheritance</b></div><div><b><br /></b></div><div>A protocol can inherit from other protocols and then add further requirements on top of the requirements it inherits. In the following example, the protocol Persistable inherits from the Entity protocol I introduced earlier. It adds the requirement to save an entity to file and load it based on its unique identifier.</div><div><br /></div><div>protocol Persistable: Entity {</div><div> func write(instance: Entity, to filePath: String)</div><div> init?(by uid: String)</div><div>}</div><div>swift</div><div>The types that adopt the Persistable protocol must satisfy the requirements defined in both the Entity and the Persistable protocol.</div><div><br /></div><div>If your type requires persistence capabilities, it should implement the Persistable protocol.</div><div><br /></div><div>struct PersistableEntity: Persistable {</div><div> var name: String</div><div> func write(instance: Entity, to filePath: String) { // ...</div><div> } </div><div> init?(by uid: String) {</div><div> // try to load from the filesystem based on id</div><div> }</div><div>}</div><div>swift</div><div>Whereas types that do not need to be persisted shall only implement the Entity protocol:</div><div><br /></div><div>struct InMemoryEntity: Entity {</div><div> var name: String</div><div>}</div><div><br /></div><div>Protocol inheritance is a powerful feature that allows for more granular and flexible designs.</div><div><br /></div><div><b>Protocol Composition</b></div><div><b><br /></b></div><div>Swift does not allow multiple inheritances for classes. However, Swift types can adopt multiple protocols. Sometimes you may find this feature useful.</div><div><br /></div><div>Here’s an example: let’s assume that we need a type that represents an Entity.</div><div><br /></div><div>We also need to compare instances of a given type. And we want to provide a custom description, too.</div><div><br /></div><div>We have three protocols that define the mentioned requirements:</div><div><br /></div><div>Entity</div><div>Equatable</div><div>CustomStringConvertible</div><div>If these were base classes, we’d have to merge the functionality into one superclass; however, with POP and protocol composition, the solution becomes:</div><div><br /></div><div>struct MyEntity: Entity, Equatable, CustomStringConvertible {</div><div> var name: String</div><div> // Equatable</div><div> public static func ==(lhs: MyEntity, rhs: MyEntity) -> Bool {</div><div> return lhs.name == rhs.name</div><div> }</div><div> // CustomStringConvertible</div><div> public var description: String {</div><div> return "MyEntity: \(name)"</div><div> }</div><div>}</div><div>let entity1 = MyEntity(name: "42")</div><div>print(entity1)</div><div>let entity2 = MyEntity(name: "42")</div><div>assert(entity1 == entity2, "Entities shall be equal")</div><div><br /></div><div>This design not only is more flexible than squeezing all the required functionality into a monolithic base class but also works for value types.</div></div><div><span style="background-color: white; color: rgba(0, 0, 0, 0.95); font-family: "Gotham SSm A", "Gotham SSm B", "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;"><span style="font-size: medium;"><br /></span></span></div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-86269949356396084962021-04-01T11:20:00.005+05:302021-04-04T15:43:00.918+05:30Difference between compact map and Flat map<p><b>Compact Map :</b></p><p>Use this method to receive an array of nonoptional values when your transformation produces an optional value.</p><pre class="language-swift" style="background: rgb(39, 40, 34); border-radius: 0.3em; border: 1px solid rgb(227, 237, 243); box-sizing: border-box; color: #f8f8f2; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; font-size: 1em; hyphens: none; line-height: 1.5; margin-bottom: 0.5em; margin-top: 0.5em; overflow-wrap: normal; overflow: auto; padding: 1em; tab-size: 4; text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; width: 600px; word-break: normal;"><code class="language-swift" style="background: 0px 0px; border-radius: 0px; border: 0px; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; font-size: 1em; hyphens: none; line-height: 1.5; overflow-wrap: normal; padding: 3px 5px; tab-size: 4; text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; word-break: normal; word-spacing: normal;"><span class="token keyword" style="color: #66d9ef;">let</span> scores <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string" style="color: #a6e22e;">"1"</span><span class="token punctuation">,</span> <span class="token string" style="color: #a6e22e;">"2"</span><span class="token punctuation">,</span> <span class="token string" style="color: #a6e22e;">"three"</span><span class="token punctuation">,</span> <span class="token string" style="color: #a6e22e;">"four"</span><span class="token punctuation">,</span> <span class="token string" style="color: #a6e22e;">"5"</span><span class="token punctuation">]</span>
<span class="token keyword" style="color: #66d9ef;">let</span> mapped<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token builtin" style="color: #a6e22e;">Int</span><span class="token operator">?</span><span class="token punctuation">]</span> <span class="token operator">=</span> scores<span class="token punctuation">.</span><span class="token builtin" style="color: #a6e22e;">map</span> <span class="token punctuation">{</span> str <span class="token keyword" style="color: #66d9ef;">in</span> <span class="token function" style="color: #e6db74;">Int</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token comment" style="border: none; color: #8292a2; margin: 0px; padding: 0px;">// [1, 2, nil, nil, 5] - Two nil values as "three" and "four" are strings.</span>
<span class="token keyword" style="color: #66d9ef;">let</span> compactMapped<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token builtin" style="color: #a6e22e;">Int</span><span class="token punctuation">]</span> <span class="token operator">=</span> scores<span class="token punctuation">.</span>compactMap <span class="token punctuation">{</span> str <span class="token keyword" style="color: #66d9ef;">in</span> <span class="token function" style="color: #e6db74;">Int</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span> <span class="token punctuation">}</span> </code></pre><p><span style="color: #8292a2; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; font-size: 1em; word-spacing: normal;">// [1, 2, 5] - The nil values for "three" and "four" are filtered out.</span> </p><h4 style="text-align: left;"><b>Flat Map :</b></h4><div>Use this method to receive a single-level collection when your transformation produces a sequence or collection for each element.</div><div><br /></div><div><b><u>Map vs FlatMap</u></b></div><div><pre class="language-swift" style="background: rgb(39, 40, 34); border-radius: 0.3em; border: 1px solid rgb(227, 237, 243); box-sizing: border-box; color: #f8f8f2; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; font-size: 1em; hyphens: none; line-height: 1.5; margin-bottom: 0.5em; margin-top: 0.5em; overflow-wrap: normal; overflow: auto; padding: 1em; tab-size: 4; text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; width: 600px; word-break: normal;"><code class="language-swift" style="background: 0px 0px; border-radius: 0px; border: 0px; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; font-size: 1em; hyphens: none; line-height: 1.5; overflow-wrap: normal; padding: 3px 5px; tab-size: 4; text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; word-break: normal; word-spacing: normal;"><span class="token keyword" style="color: #66d9ef;">let</span> scoresByName <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string" style="color: #a6e22e;">"Henk"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number" style="color: #ae81ff;">0</span><span class="token punctuation">,</span> <span class="token number" style="color: #ae81ff;">5</span><span class="token punctuation">,</span> <span class="token number" style="color: #ae81ff;">8</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string" style="color: #a6e22e;">"John"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number" style="color: #ae81ff;">2</span><span class="token punctuation">,</span> <span class="token number" style="color: #ae81ff;">5</span><span class="token punctuation">,</span> <span class="token number" style="color: #ae81ff;">8</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token keyword" style="color: #66d9ef;">let</span> mapped <span class="token operator">=</span> scoresByName<span class="token punctuation">.</span><span class="token builtin" style="color: #a6e22e;">map</span> <span class="token punctuation">{</span> $<span class="token number" style="color: #ae81ff;">0</span><span class="token punctuation">.</span>value <span class="token punctuation">}</span>
<span class="token comment" style="border: none; color: #8292a2; margin: 0px; padding: 0px;">// [[0, 5, 8], [2, 5, 8]] - An array of arrays</span>
<span class="token function" style="color: #e6db74;">print</span><span class="token punctuation">(</span>mapped<span class="token punctuation">)</span>
<span class="token keyword" style="color: #66d9ef;">let</span> flatMapped <span class="token operator">=</span> scoresByName<span class="token punctuation">.</span>flatMap <span class="token punctuation">{</span> $<span class="token number" style="color: #ae81ff;">0</span><span class="token punctuation">.</span>value <span class="token punctuation">}</span>
<span class="token comment" style="border: none; color: #8292a2; margin: 0px; padding: 0px;">// [0, 5, 8, 2, 5, 8] - flattened to only one array</span></code></pre></div><div><br /></div><div><b><u>CompactMap vs FlatMap</u></b></div><div><br /></div><div>Used on a sequence and having a transformation returning an optional value, use CompactMap. If not, either map or flatMap should give you the results you need.</div><div><br /></div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com1tag:blogger.com,1999:blog-1475345808851342812.post-44193864248447283132021-04-01T11:00:00.001+05:302021-04-01T11:00:06.219+05:30How to switch from background queue to main queue?<p>If you're on a background thread and want to execute code on the main thread, you need to call async() again. However, this time, you do it on DispatchQueue.main, which is the main thread, rather than one of the global quality of service queues.</p><p><br /></p><p>DispatchQueue.global(qos: .userInitiated).async {</p><p> if let url = URL(string: urlString) {</p><p> if let data = try? Data(contentsOf: url) {</p><p> self.parse(json: data)</p><p> return</p><p> }</p><p> }</p><p> self.showError()</p><p>}</p><p><br /></p><p>func showError() {</p><p> DispatchQueue.main.async {</p><p> let ac = UIAlertController(title: "Loading error", message: "There was a problem loading the feed; please check your connection and try again.", preferredStyle: .alert)</p><p> ac.addAction(UIAlertAction(title: "OK", style: .default))</p><p> self.present(ac, animated: true)</p><p> }</p><p>}</p><p><br /></p><p>Sof here, If I want to work with UI-related stuff, I must switch queue to main. So to execute that code under DispatchQueue.main.async { } block.</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-48262928112737274342021-04-01T10:52:00.004+05:302021-04-01T10:52:33.721+05:30Difference between FCM and APNS<div style="text-align: left;"><ul style="text-align: left;"><li>FCM is sent as JSON payloads and APNS sends either string or dictionary.</li><li>FCM has a payload of 2KB while APNS has a payload of 4KB.</li><li>APNS saves 1 notification per App while FCM saves 100 notifications per device.</li><li>FCM supports multiple platforms while APNS requires their proprietary platform.</li><li>Acknowledgment can be sent in FCM if using XMPP, but it's not possible on APNS.</li></ul><h4 style="text-align: left;"><b>Advatage of FCM</b></h4><ul style="text-align: left;"><li>Even if the user disallows notification, you can notify your app if the app is running in the foreground (using shouldEstablishDirectChannel).</li><li>Don't need to create dashboard to send notification on the device.</li><li>Notification analytics on FCM Dashboard.</li><li>Easy to create notification payload structure.</li><li>App Server side handling is easy, Only one key is required for multiple apps and platform (iOS, Android, Web)</li></ul></div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-51342769739674493502021-03-31T10:24:00.009+05:302021-03-31T10:26:27.559+05:30GIT Interview Questions 2<p></p><ol style="text-align: left;"><li><b>GIT Commit command</b><br />The command that is used to write a commit message is “git commit -a”<br />Now explain about -a flag by saying -a on the command line instructs git to commit the new content of all tracked files that have been modified. Also, mention you can use “git add <file>” before git commit -a if new files need to be committed for the first time.<br /><br /></li><li><b>How can you fix a broken commit?</b><br />In order to fix any broken commit, use the command “git commit --amend”. When you run this command, you can fix the broken commit message in the editor.<br /><br /></li><li><p data-selectable-paragraph="" style="box-sizing: border-box; color: #4a4a4a; font-family: "Open Sans", sans-serif; font-size: 16px; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; word-break: break-word;"> Basic Git commands:</p><p style="box-sizing: border-box; color: #4a4a4a; font-family: "Open Sans", sans-serif; font-size: 16px; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; word-break: break-word;"><a name="intermediate" style="box-sizing: border-box; color: inherit;"></a></p><table border="1" style="border-collapse: collapse; color: #4a4a4a; font-family: "Open Sans", sans-serif; font-size: 16px; margin-bottom: 1.125rem; table-layout: fixed; width: unset;"><tbody style="box-sizing: border-box;"><tr style="background-color: #008dd9; box-sizing: border-box; color: white; text-align: center;"><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><span style="color: black;">Command</span></td><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><span style="color: black;">Function</span></td></tr><tr style="box-sizing: border-box;"><td style="box-sizing: border-box; padding-left: 5px; text-align: center; width: 372px;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; word-break: break-word;"><span style="color: black;">git rm [file]</span></code></td><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><span style="color: black;">deletes the file from your working directory and stages the deletion.</span></td></tr><tr style="box-sizing: border-box;"><td style="box-sizing: border-box; padding-left: 5px; text-align: center; width: 372px;"><span style="color: black;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; word-break: break-word;">git log</code><span id="_tmp_pre_17" style="box-sizing: border-box;"> </span></span></td><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><span style="color: black;">list the version history for the current branch.</span></td></tr><tr style="box-sizing: border-box;"><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><p style="box-sizing: border-box; font-size: 1rem; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; text-align: center; word-break: break-word;"><span style="color: black;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; word-break: break-word;">git show [commit]</code><span id="_tmp_pre_19" style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;"> </span></span></p></td><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><span style="box-sizing: border-box;"><span style="color: black;">shows the metadata and content changes of the specified commit.</span></span></td></tr><tr style="box-sizing: border-box;"><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><p style="box-sizing: border-box; font-size: 1rem; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; text-align: center; word-break: break-word;"><span style="color: black;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; word-break: break-word;">git tag [commitID]</code><span id="_tmp_pre_20" style="box-sizing: border-box;"> </span></span></p></td><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><span style="box-sizing: border-box;"><span style="color: black;">used to give tags to the specified commit.</span></span></td></tr><tr style="box-sizing: border-box;"><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><p style="box-sizing: border-box; font-size: 1rem; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; text-align: center; word-break: break-word;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; word-break: break-word;"><span style="color: black;">git checkout [branch name]</span></code></p><p style="box-sizing: border-box; font-size: 1rem; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; text-align: center; word-break: break-word;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; word-break: break-word;"><span style="color: black;">git checkout -b [branch name]</span></code></p></td><td style="box-sizing: border-box; padding-left: 5px; width: 372px;"><span style="box-sizing: border-box;"><span style="color: black;">used to switch from one branch to another.</span></span><p style="box-sizing: border-box; font-size: 1rem; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; word-break: break-word;"></p><p style="box-sizing: border-box; font-size: 1rem; line-height: 1.7; margin-bottom: 1rem; margin-top: 0px; word-break: break-word;"><span style="box-sizing: border-box;"><span style="color: black;">creates a new branch and also switches to it.</span></span></p><div><span style="box-sizing: border-box;"><br /></span></div></td></tr></tbody></table><br /></li><li><b>In Git how do you revert a commit that has already been pushed and made public?</b><br />There can be two approaches to tackle this question and make sure that you include both because any of the below options can be used depending on the situation:<br /><br />Remove or fix the bad file in a new commit and then push it to the remote repository. This is the most obvious way to fix an error. Once you have made necessary changes to the file, then commit it to the remote repository using the command: git commit -m “commit message”<br /><br />Also, you can create a new commit that undoes all changes that were made in the bad commit. To do this use the command<br /><br />git revert <name of bad commit><br /><br /></li><li><b>What is the difference between git pull and git fetch?</b><br />Git pull command pulls new changes or commits from a particular branch from your central repository and updates your target branch in your local repository.<br /><br />Git fetch is also used for the same purpose but it works in a slightly different way. When you perform a git fetch, it pulls all new commits from the desired branch and stores it in a new branch in your local repository. If you want to reflect these changes in your target branch, git fetch must be followed with a git merge. Your target branch will only be updated after merging the target branch and fetched branch. Just to make it easy for you, remember the equation below:<br /><br />Git pull = git fetch + git merge<br /><br /></li><li><b>What is git stash?</b><br />Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for some time to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is Git stash.<br /><br />Stashing takes your working directory that is, your modified tracked files and staged changes and saves it on a stack of unfinished changes that you can reapply at any time.<br /><br /></li><li><b>What is the function of ‘git stash apply’?</b><br />If you want to continue working where you had left your work then ‘git stash apply‘ command is used to bring back the saved changes onto your current working directory.<br /><br /></li><li><b>What is git stash drop?</b><br />Git ‘stash drop’ command is used to remove the stashed item. It will remove the last added stash item by default, and it can also remove a specific item if you include it as an argument.<br /><br />Now give an example.<br /><br />If you want to remove a particular stash item from the list of stashed items you can use the below commands:</li></ol><ul style="text-align: left;"><li>git stash list: It will display the list of stashed items like:</li><li>stash@{0}: WIP on master: 049d078 added the index file</li><li>stash@{1}: WIP on master: c264051 Revert “added file_size”</li><li>stash@{2}: WIP on master: 21d80a5 added number to log<br /><br />If you want to remove an item named stash@{0} use command git stash drop stash@{0}.<br /><br /></li></ul><div><ol style="text-align: left;"><li><b>Can you explain the Gitflow workflow?</b><br />To record the history of the project, Gitflow workflow employs two parallel long-running branches – master and develop:<br /><br />Master – this branch is always ready to be released on LIVE, with everything fully tested and approved (production-ready).<br /><br />Hotfix – these branches are used to quickly patch production releases. These branches are a lot like release branches and feature branches except they’re based on master instead of develop.<br /><br />Develop – this is the branch to which all feature branches are merged and where all tests are performed. Only when everything’s been thoroughly checked and fixed it can be merged to the master.<br /><br />Feature – each new feature should reside in its own branch, which can be pushed to the develop branch as their parent one.<br /><br /></li><li><b>What is Git fork? What is the difference between fork, branch, and clone?</b><br />A fork is a copy of a repository. Normally you fork a repository so that you are able to freely experiment with changes without affecting the original project. Most commonly, forks are used to either propose changes to someone else’s project or to use someone else’s project as a starting point for your own idea.<br /><br />git cloning means pointing to an existing repository and make a copy of that repository in a new directory, at some other location. The original repository can be located on the local file system or on remote machine accessible supported protocols. The git clone command is used to create a copy of an existing Git repository.<br /><br />In very simple words, git branches are individual projects within a git repository. Different branches within a repository can have completely different files and folders, or it could have everything the same except for some lines of code in a file.<br /><br /></li><li><b>What is the difference between rebasing and merge in Git?</b><br />In Git, the rebase command is used to integrate changes from one branch into another. It is an alternative to the “merge” command. The difference between rebasing and merge is that rebase rewrites the commit history in order to produce a straight, linear succession of commits.<br /><br />Merging is Git’s way of putting a forked history back together again. The git merge command helps you take the independent lines of development created by git branch and integrate them into a single branch.<br /><br /></li><li><b>What is git cherry-pick?</b><br />The command git cherry-pick is normally used to introduce particular commits from one branch within a repository onto a different branch. Another common use is to forward- or back-port commits from a maintenance branch to a development branch. This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.<br /><br />Consider:<br /><br />git cherry-pick <commit-hash><br /><br /></li><li><b>How do you squash the last N commits into a single commit?</b><br />There are two options to squash the last N commits into a single commit include both of the below-mentioned options in your answer<br />If you want to write the new commit message from scratch use the following command<br /><br />git reset –soft HEAD~N &&git commit<br /><br />If you want to start editing the new commit message with a concatenation of the existing commit messages then you need to extract those messages and pass them to Git commit for that I will use<br /><br />git reset –soft HEAD~N &&git commit –edit -m”$(git log –format=%B –reverse .HEAD@{N})”<br /><br /></li><li><b>How do I rename a local Git branch?</b><br />Here are the steps to rename the branch:<br /><br />Switch to the branch which needs to be renamed<br /><br />git branch -m <new_name><br />git push origin :<old_name><br />git push origin <new_name>:refs/heads/<new_name></li></ol><div><br /></div></div><p></p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-69869411038682517182021-03-30T22:10:00.005+05:302021-03-30T22:10:50.675+05:30Difference between MVC and Viper architecture<div dir="ltr" style="text-align: left;" trbidi="on"><a href="https://themindstudios.com/blog/mvp-vs-mvc-vs-mvvm-vs-viper/">
https://themindstudios.com/blog/mvp-vs-mvc-vs-mvvm-vs-viper/</a></div>
Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-28325212893486446572021-03-30T22:10:00.003+05:302021-03-30T22:10:19.422+05:30What is MVVM?MVVM - Model View ViewModel<div><br /></div><div><b>Advantage : </b></div><div><b><br /></b></div><div><div><b>Better Separation of Concerns</b></div><div>In a project built with the Model-View-Controller pattern, you are often faced with the question which code goes where. Code that doesn’t fit or belong in the model or view layer is often put in the controller layer. This inevitably leads to fat controllers that are difficult to test and manage.</div><div><br /></div><div>The MVVM pattern presents a better separation of concerns by adding view models to the mix. The view model translates the data of the model layer into something the view layer can use. The controller is no longer responsible for this task.</div><div><br /></div><div><b>Improved Testability</b></div><div>View controllers are notoriously hard to test because of their relation to the view layer. By migrating data manipulation to the view model, testing becomes much easier. Testing view models is easy. Because a view model doesn’t have a reference to the object it is owned by, it easy to write unit tests for a view model.</div><div><br /></div><div>Another, and often overlooked, benefit of MVVM is improved testability of view controllers. The view controller no longer depends on the model layer, which makes them easier to test.</div><div><br /></div><div><b>Transparent Communication</b></div><div>The responsibilities of the view controller are reduced to controlling the interaction between the view layer and the model layer, glueing both layers together.</div><div><br /></div><div>The view model provides a transparent interface to the view controller, which it uses to populate the view layer and interact with the model layer. This results in a transparent communication between the four layers of your application.</div></div><div><br /></div><div><b><u>There are some rules you know when using MVVM</u></b></div><div><br /></div><div><b>Rule1 :</b> The view does not know about the controller it is owned by. Remember that views are supposed to be dumb. They only know how to present the data they are given to the user.</div><div><b>Rule 2 :</b> The controller does not know about the model. This is what separates MVC from MVVM.</div><div><b>Rule 3 : </b>The model does not know about the view model it is owned by.</div><div><b>Rule 4 :</b> The view model owns the model. When using the Model-View-Controller pattern, the model is usually owned by the controller.</div><div><b>Rule 5 : </b>The view controller owns the view. This relationship remains unchanged when using the Model-View-ViewModel pattern.</div><div><b>Rule 6 :</b> And, finally, the controller owns the view model. It interacts with the model layer through one or more view models.</div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-7497459845313003022021-03-30T21:43:00.000+05:302021-03-30T21:43:03.164+05:30Download task when app is inactive<p><a href="https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background"> https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background</a></p><p><br /></p><p style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: var(--s-prose-spacing); margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; vertical-align: baseline;">For the background download, there is <code style="background-color: var(--black-075); border-radius: 3px; border: 0px; box-sizing: inherit; color: var(--black-800); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 2px 4px; vertical-align: baseline; white-space: pre-wrap;">beginBackgroundTaskWithExpirationHandler:</code> that is specifically designed to do that. When you use it, you will get few more minutes to execute whatever you need (after that limit, your application will get terminated no matter what).</p><p style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: var(--s-prose-spacing); margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; vertical-align: baseline;">You can write following methods:</p><pre class="lang-swift s-code-block hljs" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-function" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">func</span> <span class="hljs-title" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">beginBackgroundTask</span>()</span> -> <span class="hljs-type" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">UIBackgroundTaskIdentifier</span> {
<span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">return</span> <span class="hljs-type" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">UIApplication</span>.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
}
<span class="hljs-function" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">func</span> <span class="hljs-title" style="border: 0px; box-sizing: inherit; color: var(--highlight-literal); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">endBackgroundTask</span>(<span class="hljs-params" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">taskID</span>: <span class="hljs-type" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">UIBackgroundTaskIdentifier</span>)</span> {
<span class="hljs-type" style="border: 0px; box-sizing: inherit; color: var(--highlight-namespace); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">UIApplication</span>.sharedApplication().endBackgroundTask(taskID)
}
</code></pre><p style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: var(--s-prose-spacing); margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; vertical-align: baseline;">When you want to use it, you just simple begin / end the task when starting / finishing the download call:</p><pre class="lang-swift s-code-block hljs" style="border-radius: 5px; border: 0px; box-sizing: inherit; color: var(--highlight-color); font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.30769; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-top: 0px; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="hljs-comment" style="border: 0px; box-sizing: inherit; color: var(--highlight-comment); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">// Start task</span>
<span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">let</span> task <span class="hljs-operator" style="border: 0px; box-sizing: inherit; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span> <span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">self</span>.beginBackgroundTask()
<span class="hljs-comment" style="border: 0px; box-sizing: inherit; color: var(--highlight-comment); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">// Do whatever you need</span>
<span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">self</span>.someBackgroundTask()
<span class="hljs-comment" style="border: 0px; box-sizing: inherit; color: var(--highlight-comment); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">// End task</span>
<span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">self</span>.endBackgroundTask(task)
</code></pre><p style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Hope it helps!</p>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0tag:blogger.com,1999:blog-1475345808851342812.post-40391861227658303722021-03-30T21:21:00.004+05:302021-03-30T21:23:24.643+05:30What is SSL Pinning and How to implement it?<p>SSL stands for Secure Socket Layer, which is a protocol for creating an encrypted connection between client and server. It ensures that all data pass in network will be private and integral.</p><p>How SSL works? When client establishes the connection with server (called SSL handshake):</p><p></p><ol style="text-align: left;"><li>Client connects to server and requests server identify itself.</li><li>Server sends certificate to client (include public key)</li><li>Client checks if that certificate is valid. If it is, client creates a symmetric key (session key), encrypts with public key, then sends back to server</li><li>Server receives encrypted symmetric key, decrypts by its private key, then sends acknowledge packet to client</li><li>Client receives ACK and starts the session</li></ol><p></p><div>Using SSL, the client will allow the connection only from trusted sources that have the valid certificate. And it looks good for most cases. But what if someone stands between client and server, and acts like they are the real server? Let's call client is C, server is S and the attacker is A.</div><div><div><br /></div><div>In step 1, instead of sending packet to S, A can catch the packet and pretend it as S. What if instead of receiving certificate from S, client C will receive fake certificate from A and believe it's valid. A can make C think that it is communicating with S, but actually all connection flows will be directed to attacker A.</div><div><br /></div><div>Hence, SSL pinning can be the solution to prevent Man-In-The-Middle (MITM) attack. SSL pinning will ensure that client connect with designated server. The main key of SSL pinning that server certificate will be saved in app bundle. Then, when client receives certificate from server, it then compares 2 certificates to make sure that they are the same before establishing the connection.</div></div><div><br /></div><div><b>URLSession</b></div><div><b><br /></b></div><div><div>For NSURLSession, the main method to handle SSL pinning is URLSession:didReceiveChallenge:completionHandler:delegate. Set your class to conform URLSessionDelegate and paste this function to your class:</div><div><br /></div><div><br /></div><div><div>func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {</div><div> if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {</div><div> if let serverTrust = challenge.protectionSpace.serverTrust {</div><div> var secresult = SecTrustResultType.invalid</div><div> let status = SecTrustEvaluate(serverTrust, &secresult)</div><div> </div><div> if (errSecSuccess == status) {</div><div> if let serverCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0) {</div><div> let serverCertificateData = SecCertificateCopyData(serverCertificate)</div><div> let data = CFDataGetBytePtr(serverCertificateData);</div><div> let size = CFDataGetLength(serverCertificateData);</div><div> let cert1 = NSData(bytes: data, length: size)</div><div> let file_der = Bundle.main.path(forResource: "name-of-cert-file", ofType: "cer")</div><div> </div><div> if let file = file_der {</div><div> if let cert2 = NSData(contentsOfFile: file) {</div><div> if cert1.isEqual(to: cert2 as Data) {</div><div> completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust:serverTrust))</div><div> return</div><div> }</div><div> }</div><div> }</div><div> }</div><div> }</div><div> }</div><div> }</div><div> </div><div> // Pinning failed</div><div> completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)</div><div>}</div></div><div><br /></div><div>This function will “requests credentials from the delegate in response to an authentication request from the remote server.” We will compare the certificate from server with the one that saved in app bundle. If 2 certificates are identical, the authentication will let it pass and client can connect to server.</div></div><div><br /></div><div><div><b>Types of SSL Pinning(What to Pin)?</b></div><div><br /></div><div><b>Pin the certificate: </b>You can download the server’s certificate and put this in your app bundle. At runtime, the app compares the server’s certificate to the one you’ve embedded.</div><div><br /></div><div><div>One <b>disadvantage </b>of SSL pinning is that you have to save the certificate in the app. Whenever the certificate is updated, we need to release new version of app. But this also leads to another problem: what we do with older version?</div><div><ul style="text-align: left;"><li>Maintain the old certificate for a time, until we make sure all users have downloaded new version already.</li></ul></div></div><div><br /></div><div><b>Pin the public key:</b> You can retrieve the certificate’s public key and include it in your code as a string. At runtime, the app compares the certificate’s public key to the one hard-coded hash string in your code.</div></div>Manan Shahhttp://www.blogger.com/profile/00814179426513564510noreply@blogger.com0