<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vikas Kumar on Medium]]></title>
        <description><![CDATA[Stories by Vikas Kumar on Medium]]></description>
        <link>https://medium.com/@kumar.vikas?source=rss-48fe6423e190------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3THsEh-_15QDqaeiXokLiA.png</url>
            <title>Stories by Vikas Kumar on Medium</title>
            <link>https://medium.com/@kumar.vikas?source=rss-48fe6423e190------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:27:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kumar.vikas/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Swift Gem — 9]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-9-ecbf943b264e?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecbf943b264e</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Mon, 18 Aug 2025 14:38:52 GMT</pubDate>
            <atom:updated>2025-08-18T14:38:52.937Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift Gem — 9</h3><h4>Simplify array mapping with key paths</h4><p>Starting with <strong>Swift 5.2</strong>, key paths can be used more dynamically, including their ability to be automatically converted into functions. This enhancement significantly improves the ease of working with collections of objects.</p><p>Here’s an example to illustrate how key paths can be used with methods like <strong>map() </strong>on an array. Consider an array of <strong>Person</strong>objects, each with a <strong>name</strong> property.</p><pre>struct Person {<br>    var name: String<br>    var age: Int<br>}<br><br>let people = [<br>    Person(name: &quot;Alice&quot;, age: 30),<br>    Person(name: &quot;Bob&quot;, age: 25),<br>    Person(name: &quot;Charlie&quot;, age: 35)<br>]</pre><p>Traditionally, to create an array of names, we would use <strong>map()</strong> with a closure.</p><pre>let names = people.map { $0.name }</pre><p>However, with a key path, this becomes much more concise.</p><pre>let names = people.map(\.name)</pre><p>Here, <strong>\Person.name</strong> is a key path that directly references the name property of a <strong>Person</strong> object. When passed to <strong>map()</strong>, Swift automatically treats this key path as a function that extracts the name from each Person in the array.</p><p>This approach is not only more succinct but also enhances readability. It’s particularly beneficial when dealing with arrays of complex objects where we need to transform or extract specific properties.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecbf943b264e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 8]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-8-76b37eac450d?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/76b37eac450d</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[protocol]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Wed, 06 Aug 2025 14:51:41 GMT</pubDate>
            <atom:updated>2025-08-06T14:51:41.133Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SDYQ20DixkZi9OD1x76FVA.jpeg" /></figure><h3>Swift Gem — 8</h3><h4>Build versatile protocols with associated types</h4><p>Swift’s protocols with associated types provide a robust framework for building adaptable and modular components. This feature allows protocols to declare a placeholder for a type that each conforming type can then define, enabling us to write generic code that works across many data types. This approach not only increases the reusability of our code but also maintains strong type safety, making our applications easier to manage and scale.</p><p>Let’s look at an example with a <strong>Container</strong> protocol that is defined with an associated type <strong>ItemType</strong>. This associated type acts as a placeholder that each conforming type specifies, allowing the protocol to be used with a variety of different data types.</p><pre>protocol Container {<br>    associatedtype ItemType<br>    mutating func append(_ item: ItemType)<br>    var count: Int { get }<br>    subscript(i: Int) -&gt; ItemType { get }<br>}<br><br>struct IntStack: Container {<br>    var items = [Int]()<br><br>    mutating func append(_ item: Int) {<br>       items. Append(item)<br>    }<br><br>    var count: Int {<br>        return items.count<br>    }<br><br>    subscript(i: Int) -&gt; Int {<br>        return items[i]<br>    }<br>}</pre><pre>struct StringQueue: Container {<br>    var items = [String]()<br><br>    mutating func append(_ item: String) {<br>        items. Append(item)<br>    }<br><br>    var count: Int {<br>        return items.count<br>    }<br><br>    subscript(i: Int) -&gt; String {<br>        return items[i]<br>    }<br>}</pre><p>In our example, the <strong>IntStack </strong>struct conforms to <strong>Container</strong> with <strong>ItemType </strong>specified as <strong>Int</strong>. It implements all required methods and properties, handling a collection of integers. The <strong>append(_:)</strong> method adds an integer to the stack, the count property provides the number of items in the stack, and the subscript provides indexed access to its elements.</p><p>Similarly, the <strong>StringQueue</strong> struct specifies <strong>ItemType</strong> as <strong>String</strong> and conforms to the <strong>Container</strong> protocol. It manages a queue of strings, implementing the same functionalities as <strong>IntStack</strong> but tailored for string handling.</p><p>Using associated types with protocols allows for significant flexibility in how protocols are implemented across various types, fostering code reusability without sacrificing type safety. This technique is particularly useful in creating data structures, where operations such as adding items, counting, or item retrieval need to be implemented differently depending on the data type handled. By leveraging associated types, we can create more generic and modular APIs that are easier to extend and maintain.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76b37eac450d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 7]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-7-58d8e9561a30?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/58d8e9561a30</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Sat, 02 Aug 2025 10:50:49 GMT</pubDate>
            <atom:updated>2025-08-02T10:51:34.860Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kYWvxU5BSLb6ANR5dOCnHw.jpeg" /></figure><h3>Swift Gem — 7</h3><h4>Encapsulate property-specific behavior in property wrappers</h4><p>Property wrappers in Swift are a powerful feature designed to encapsulate shared logic, thereby enhancing code efficiency and maintainability. They offer a modular approach to manage common behaviors and transformations applied to properties, such as input validation, synchronization, or lifecycle management, across your codebase.</p><p>A property wrapper abstracts the logic needed to read and write a property’s value while adding additional behavior or side effects. This allows us to reuse code effectively and maintain consistency in property management throughout an application.</p><p>To implement a property wrapper, we can define a structure, class, or enumeration with the <strong>@<em>propertyWrapper </em></strong>annotation. The property wrapper itself manages how a property is stored and defines what actions are taken when the property is accessed or modified.</p><p>Consider a use case where it’s crucial to ensure that all string inputs in a user model are free from unwanted whitespace. Instead of manually trimming these strings each time they’re set, we can create a <strong><em>SafeString</em></strong> property wrapper to handle this automatically.</p><pre>import Foundation<br><br>@propertyWrapper struct SafeString {<br>    var wrappedValue: String {<br>        didSet {<br>            wrappedValue = wrappedValue<br>                .trimmingCharacters(in: .whitespacesAndNewlines)<br>        }<br>    }<br>    <br>    init(wrappedValue: String) {<br>        self.wrappedValue = wrappedValue<br>            .trimmingCharacters(in: .whitespacesAndNewlines)<br>    }<br>}<br><br>struct User {<br>    @SafeString var username: String<br>}<br><br>var user = User(username: &quot; John Doe &quot;)<br><br>// Prints `John Doe`<br>print(user. Username)</pre><p>Property wrappers reduce repetitive code by handling common transformations or checks in one place, thus simplifying the property declarations in our models or view controllers. Encapsulating property-specific behavior in wrappers helps keep our business logic clean and focused by separating the concerns of data storage and data transformation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58d8e9561a30" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 6]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-6-3b7d3bae8e88?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b7d3bae8e88</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Tue, 15 Jul 2025 07:19:19 GMT</pubDate>
            <atom:updated>2025-08-02T10:07:01.642Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vtdjYCT1pAgWRpDnG4PmPw.jpeg" /></figure><h3>Swift Gem — 6</h3><h4><strong>Indicate to the compiler that a function will never return</strong></h4><p>When we need to explicitly inform the compiler that a certain point in the program should not be reached, we should use the <strong>Never</strong> return type. This special return type is used to indicate that the function will end the program’s execution or throw an error, thus it will never return to its caller</p><p>Let’s consider an example to understand how <strong>Never</strong> can be used effectively.</p><pre>func assertUnreachableCode(<br>file: String = #file,<br>line: Int = #line,<br>function: String = #function<br>) -&gt; Never {<br>    print(&quot;&quot;&quot;<br>    Fatal error: Unreachable code reached \<br>    in \(file) at line \(line), function \(function)<br>    &quot;&quot;&quot;)<br><br>    fatalError(&quot;Unreachable code reached&quot;)<br>}<br><br>enum UserCommand {<br>    case start, stop, pause, resume, unknown<br>}<br><br>func processCommand(_ command: UserCommand) -&gt; String {<br>    switch command {<br>    case .start: return &quot;Started&quot;<br>    case .stop: return &quot;Stopped&quot;<br>    case .pause: return &quot;Paused&quot;<br>    case .resume: return &quot;Resumed&quot;<br>    case .unknown:<br>        // This case should logically never occur<br>        assertUnreachableCode()<br>    }<br>}</pre><p><strong>print(processCommand(.start))</strong></p><p><strong>print(processCommand(.unknown))</strong></p><p>In the above code, we define <strong>UserCommand </strong>, an enumeration that represents different user commands within an application. The function <strong>processCommand() </strong>processes these commands, returning a string response for each valid command. Notably, the <strong>unknown </strong>case is included to handle any undefined command inputs, which ideally should never occur if the application logic is correctly implemented.</p><p>The <strong>assertUnreachableCode()</strong> function is strategically used within the <strong>unknown</strong> case of the <strong>switch</strong> statement. Marked with a <strong>Never</strong> return type, it provides a clear indication of an anomaly if this case is ever triggered. When executed, it not only terminates the application but also provides detailed debugging information about where and why the termination occurred.</p><p>The compiler understands that after calling <strong>assertUnreachableCode()</strong>, the function will not continue its normal execution flow. And for anyone reading the code, it becomes immediately clear that reaching this point in the code is abnormal and should be investigated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b7d3bae8e88" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 5]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-5-7fb9f1bb6015?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/7fb9f1bb6015</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Tue, 15 Jul 2025 07:01:10 GMT</pubDate>
            <atom:updated>2025-08-02T10:07:54.885Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FRWBVwOHPYFDIVTp_ylZNQ.jpeg" /></figure><h3>Swift Gem — 2</h3><h4><strong>Ignore return values without generating a warning</strong></h4><p>Functions that return a value typically require that value to be used or explicitly ignored, as unused return values can often signify a programming oversight or a potential bug. However, in some cases, a function might return a value that is useful only under certain conditions, and not using the return value should not necessarily trigger a warning. To handle such scenarios, Swift provides the <strong><em>@discardaleResult </em></strong>attribute. This attribute can be applied to functions to indicate that the caller may choose to ignore the returned value without the compiler generating a warning.</p><p>For example, consider a logging function that returns a Boolean status indicating whether the log entry was successfully recorded. While this information can be crucial in some debugging or error-handling scenarios, in many cases, the developer might simply want to perform the logging action without checking the result</p><pre>@discardableResult<br>func log(message: String) -&gt; Bool {<br>    // Logic to log the message<br>    print(“Log: \(message)”)<br>    return true<br>}<br><br>// Use the result<br>let success = log(message: &quot;User performed an action&quot;)<br>if !success {<br>    print(“Logging failed”)<br>}<br><br>// Ignore the result<br>log(message: “User performed an action”)</pre><p>In the code above, the <strong>log() </strong>function is decorated with the <strong><em>@discardaleResult</em></strong> attribute, allowing it to be called without handling the Boolean result. This functionality is particularly useful because it provides the flexibility to either monitor the success of the logging operation or to simply execute the logging without cluttering the code with unnecessary result checks.</p><p>By using <strong><em>@discardaleResult</em></strong>, we can design our APIs to be both flexible and convenient, accommodating various use cases without compromising the clarity or safety of the application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7fb9f1bb6015" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Xcode Project Renamer]]></title>
            <link>https://medium.com/@kumar.vikas/xcode-project-renamer-30e7ab010746?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/30e7ab010746</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Mon, 07 Jul 2025 14:41:04 GMT</pubDate>
            <atom:updated>2025-08-02T10:14:15.406Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8SOFCuV864Sql_2G0nnmhQ.jpeg" /></figure><h4>Very useful tool for Developers</h4><p><strong>Follow the steps:</strong></p><ol><li>Open Xcode project folder/directory in terminal by right-clicking the folder, you will get options — <strong>New Terminal at Folder</strong></li><li>Run below code in terminal one by one</li></ol><pre>// download script and make it executable<br>$ curl https://raw.githubusercontent.com/tadija/xcode-project-renamer/master/Sources/main.swift -o rename.swift &amp;&amp; chmod +x rename.swift</pre><pre>// run script<br>$ ./rename.swift &quot;$OLD_PROJECT_NAME&quot; &quot;$NEW_PROJECT_NAME&quot;</pre><pre>// remove script<br>$ rm rename.swift</pre><p>Now, Open your Xcode project and see the magic.</p><blockquote><strong>Note</strong>: Do not try this on your <strong>production</strong> projects.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30e7ab010746" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 5]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-5-fe6505a0e2f9?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe6505a0e2f9</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[closure]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Sat, 05 Jul 2025 09:25:38 GMT</pubDate>
            <atom:updated>2025-08-02T10:15:53.854Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JuOAX7nKZSg9i_aFK3Bp-Q.jpeg" /></figure><h3>Swift Gem — 5</h3><h4><strong>Optimize function arguments with autoclosures</strong></h4><p>When we define a function parameter as an <strong>@autoclosure</strong> in Swift, we allow the caller to pass in an expression as if it is a direct value. Swift then automatically wraps this expression in a closure. The key benefit is that the expression inside the <strong>@autoclosure</strong> is not evaluated at the point of the function call, but rather at the point of execution within the function. It’s useful for optimizing performance, especially in situations where the evaluation of an argument is resource-intensive.</p><p>To illustrate the use of <strong>autoclosures</strong>, let’s consider a logging function.</p><pre>enum LogLevel: Comparable {<br>    case debug, info, warning, error<br>}<br><br>var currentLogLevel: LogLevel = .info<br><br>func logMessage(<br>    level: LogLevel,<br>    message: @autoclosure () -&gt; String<br>) {<br>    if level &gt;= currentLogLevel {<br>        print(message())<br>    }<br>}<br><br>func expensiveStringComputation() -&gt; String {<br>    // Simulate an expensive operation<br>    return &quot;Expensive Computed String&quot;<br>}<br><br>currentLogLevel = .debug<br><br>logMessage(<br>    level: .debug,<br>    message: &quot;Debug: \(expensiveStringComputation())&quot;<br>)</pre><p>In this example, the <strong>logMessage()<em> </em></strong>function takes a message parameter marked with <strong>@autoclosure</strong>. This means the <strong>expensiveStringComputation()</strong> is not executed when the <strong>logMessage()</strong> function is called. Instead, it’s wrapped in a closure and only executed when the if condition evaluates to <strong>true</strong>. This approach avoids unnecessary computation when the log level is lower than <strong>debug.</strong></p><blockquote><strong>Autoclosures</strong> in Swift offer a sophisticated yet straightforward way to optimize function arguments, especially when dealing with potentially expensive computations. By automatically wrapping arguments in closures, they enable deferred execution, leading to more efficient and cleaner code.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe6505a0e2f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 4]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-4-6f695a6935a3?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f695a6935a3</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Mon, 16 Jun 2025 16:19:04 GMT</pubDate>
            <atom:updated>2025-08-02T10:24:16.376Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kYWvxU5BSLb6ANR5dOCnHw.jpeg" /></figure><h3>Swift Gem — 4</h3><h4>Enable direct modification of function arguments</h4><p>Managing data mutation within functions often requires careful consideration. Sometimes, it’s not sufficient to pass values into a function and have it operate on them independently. Instead, there are situations where you need the function to directly modify the original variables passed to it.</p><p>In Swift development, managing mutable state within functions can be elegantly handled using <strong>inout </strong>parameters. They provide a way for a function to modify its parameters directly, rather than working with copies. This can be particularly handy when dealing with complex data structures or when performance optimization is a concern.</p><p>Imagine a scenario where we’re tasked with designing a function to update user details. These details, such as <strong>name</strong>and <strong><em>age</em></strong>, may need to be modified based on various requirements.</p><pre>struct User {<br>    var name: String<br>    var age: Int<br>}<br><br>func updateUserDetails(<br>    user: inout User, <br>    newname: String, <br>    newAge: Int<br>) {<br>    user.name = newname<br>    user.Age = newAge<br>}</pre><pre>var currentUser = User(name: “Alice”, age: 30)<br><br>updateUserDetails(user: &amp;currentUser, newName: “Bob”, newAge: 25)<br><br>// Prints `User(name: “Bob”, age: 25)`<br>print(currentUser)</pre><p>In this example, the <strong><em>updateUserDetails()</em></strong> function takes an <strong>inout</strong> parameter user, along with new name and age values. This allows the function to directly modify the properties of the <strong>User</strong> instance passed to it.</p><p>By avoiding unnecessary copying of data, <strong>inout</strong> parameters can improve performance and memory usage, particularly when dealing with large data structures.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f695a6935a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 3]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-3-278d0884e7c9?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/278d0884e7c9</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Thu, 12 Jun 2025 14:30:39 GMT</pubDate>
            <atom:updated>2025-08-02T10:23:01.581Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U7ev30hNi4HRi1KHjmaNOA.jpeg" /></figure><h3>Swift Gem — 3</h3><h4>Define a set of configurations with <strong>OptionSet</strong></h4><p>In Swift, managing a group of related configuration options for a method can be elegantly handled using the <strong><em>OptionSet</em></strong>.</p><p><strong><em>OptionSet </em></strong>is a protocol used to represent a collection of options. Each option is a unique value, and multiple options can be combined. It’s particularly useful for configurations where options are not mutually exclusive.</p><p>Imagine we are writing a function to fetch data from a server. We might have several options for this fetch operation, like whether to use cache, whether to retry on failure, or whether to run the operation in the background.</p><p>First, let’s define an <strong><em>OptionSet</em></strong> to represent these options.</p><pre>struct FetchOptions: OptionSet {<br>    let rawValue: Int<br>  <br>    static let useCache = FetchOptions(rawValue: 1 &lt;&lt; 0)<br>    static let retryOnFailure = FetchOptions(rawValue: 1 &lt;&lt; 1)<br>    static let background = FetchOptions(rawValue: 1 &lt;&lt; 2)<br>}</pre><p>Each option is assigned a unique value, a power of two, which allows them to be uniquely combined using bitwise operations without overlap.</p><p>Now, we can use this <strong><em>OptionSet</em></strong> in our method definition. When calling the method, we can specify a single option or a combination of options.</p><pre>func fetchData(options: FetchOptions) {<br>    if options. Contains(.useCache) {<br>        // Implement cache logic<br>    }<br>    if options. Contains(.retryOnFailure) {<br>        // Implement retry logic<br>    }<br>    if options. Contains(.background) {<br>        // Implement background execution logic<br>    }<br>    // Rest of the fetch operation<br>}</pre><p><strong>fetchData(options: [.useCache, .retryOnFailure])</strong></p><p>Using an <strong><em>OptionSet</em></strong> to pass configurations to a method can make our code more readable and expressive. We can easily combine different options without cluttering our method signatures with multiple Boolean parameters.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=278d0884e7c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Gem — 1]]></title>
            <link>https://medium.com/@kumar.vikas/swift-gem-1-451596b29348?source=rss-48fe6423e190------2</link>
            <guid isPermaLink="false">https://medium.com/p/451596b29348</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Vikas Kumar]]></dc:creator>
            <pubDate>Sun, 08 Jun 2025 13:17:41 GMT</pubDate>
            <atom:updated>2025-08-02T10:25:05.436Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8SOFCuV864Sql_2G0nnmhQ.jpeg" /></figure><h3>Swift Gem — 1</h3><h4><strong>Enhance your types with custom string representation</strong></h4><p>In Swift, the <strong>CustomeStringConvertibleProtocol </strong>allows custom types to define how they should be represented as strings, making it possible to provide a more meaningful and user-friendly description of an instance. Implementing this protocol is particularly useful for debugging and logging purposes, where a clear and readable output can significantly ease the process of understanding and using custom types.</p><p>Let’s consider a <strong>Coordinates</strong>struct that holds geographic coordinates. By conforming to <strong><em>CustomeStringConvertibleProtocol</em></strong>, we can customize how these coordinates are represented when converted to a string, thereby enhancing clarity and readability.</p><pre>struct Coordinates: CustomStringConvertible {<br>    var latitude: Double<br>    var longitude: Double<br><br>    var description: String {<br>        let latDirection = latitude &gt;= 0 ? &quot;N&quot; : &quot;S&quot;<br>        let lonDirection = longitude &gt;= 0 ? &quot;E&quot; : &quot;W&quot;<br>        <br>        return &quot;&quot;&quot;<br>               Coordinates: \(abs(latitude))°\(latDirection), \<br>               \(abs(longitude))°\(lonDirection)<br>               &quot;&quot;&quot;<br>    }<br>}</pre><blockquote><strong>Example Usage:</strong></blockquote><pre>let location = Coordinates(latitude: 37.7749, longitude: -122.4194)<br><br>// Prints `Coordinates: 37.7749°N, 122.4194°W`<br>print(location)</pre><p>The <strong>description</strong> property allows us to define a custom string that represents the instance. In the Coordinates example, the description includes the absolute values of the latitude and longitude, along with their respective directions <strong><em>(North, South, East, West)</em></strong>. This approach is not only more informative but also aligns with common representations in mapping and navigation systems.</p><p>Implementing <strong>CustomeStringConvertible </strong>makes debugging more straight for-ward as objects can be printed directly with meaningful information. This reduces the need for manually appending properties or writing separate functions to decipher object states. For types used extensively throughout an application, providing a clear string representation ensures that logs, error messages, and debug outputs are accessible and helpful to developers and users alike.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=451596b29348" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>