<?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 mohamed Aglan on Medium]]></title>
        <description><![CDATA[Stories by mohamed Aglan on Medium]]></description>
        <link>https://medium.com/@MohamedAglan?source=rss-1fa950709942------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UWi5_0VuUrMa4xd6</url>
            <title>Stories by mohamed Aglan on Medium</title>
            <link>https://medium.com/@MohamedAglan?source=rss-1fa950709942------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:12:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@MohamedAglan/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[Singleton]]></title>
            <link>https://medium.com/@MohamedAglan/singleton-60d148f80404?source=rss-1fa950709942------2</link>
            <guid isPermaLink="false">https://medium.com/p/60d148f80404</guid>
            <dc:creator><![CDATA[mohamed Aglan]]></dc:creator>
            <pubDate>Wed, 01 May 2024 13:37:24 GMT</pubDate>
            <atom:updated>2024-05-01T13:37:24.743Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is the Singleton pattern?</h4><p>In the domain of iOS development, design patterns are categorized into three primary types: Creational, Structural, and Behavioral patterns. The Singleton pattern falls under the Creational category, which is dedicated to the process of object creation. This pattern is designed to ensure that only one instance of a specific class exists, thereby providing a unified point of access to it across various parts of a program.</p><p>The Singleton pattern is recognized as a foundational element in the arsenal of software design patterns. It guarantees that a class has a single instance and offers a global access point to this instance. Swift 5, known for its modern syntax and robust safety features, facilitates an efficient implementation of the Singleton pattern. This discussion will delve into the Singleton design pattern in Swift 5, examining its application, execution, and the common challenges that developers should be wary of.</p><p>Implementing the Singleton pattern in Swift 5 is notably straightforward, reflecting the language’s emphasis on safety and simplicity. Here is a standard approach to implement the Singleton pattern using Swift 5:</p><p><strong>class MySingleton { <br>static let shared = MySingleton() <br>private init() {<br>// Private initialization to ensure just one instance is created<br> }<br> func doSomething() { <br>//Functionality of the singleton <br> }<br>}</strong></p><h3>Key Components</h3><ul><li>Static Property: static let shared declares a static constant within the MySingleton class. This ensures that the instance is shared across all uses of this class.</li><li>Private Initializer: The private initializer prevents other parts of the app from creating an instance of MySingleton, enforcing the singleton&#39;s single instance rule.</li></ul><h3>Benefits of Using the Singleton Pattern</h3><ol><li>Controlled Access: By limiting the instantiation of a class to a single instance, you have controlled and predictable access to resources.</li><li>Ease of Implementation: As demonstrated, implementing a Singleton in Swift is straightforward and can be achieved with minimal code.</li><li>Shared Resources: Singletons are excellent for managing shared resources, such as network connections or database access.</li></ol><h3>Drawbacks and Considerations</h3><p>Despite its benefits, the Singleton pattern is not without criticism. Here are a few drawbacks to consider:</p><ul><li>Global State: Singletons essentially create global state, which can lead to hidden dependencies between classes, making the system harder to understand and debug.</li><li>Testing Challenges: Singletons can introduce challenges in testing because they carry state throughout the life of an application. This state can be difficult to reset between tests.</li><li>Concurrency Issues: If not handled carefully, singletons can lead to concurrency issues in a multithreaded environment, especially if the singleton involves mutable shared state.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60d148f80404" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implement Unit Testing in iOS]]></title>
            <link>https://medium.com/@MohamedAglan/implement-unit-testing-in-ios-a0351eb26395?source=rss-1fa950709942------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0351eb26395</guid>
            <dc:creator><![CDATA[mohamed Aglan]]></dc:creator>
            <pubDate>Sat, 13 Jan 2024 02:36:44 GMT</pubDate>
            <atom:updated>2024-01-13T02:36:44.762Z</atom:updated>
            <content:encoded><![CDATA[<p>Assume we have a view like this to convert a currency from dollar to euro</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*9k8x5Q8JKuRw61NLzRGAdA.png" /></figure><p>so you have to enter the pice in USD and then press on Calculate Now button to convert the price in USD to Euro.</p><p>For Example: if you entered 1 dollar then it must be a 0.91 euro.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*EPdDT_OCv-kK7UaPrzRwzw.png" /></figure><p>In the ViewController we will write this function that takes the price in USD to convert it to Euro by getting price in USD and multiply it for 0.91.</p><p>To write a unit test for this code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*pHVd0xpDwv5O9aYxFNsPIQ.png" /></figure><p>In UnitTestingDemoTests you will find it import XCTest then you have to write <strong>@testable</strong> <strong>import your project name </strong>to get an access of your Controller`s functions.</p><p>You will find two functions <strong>setUpWithError and tearDownWithError</strong></p><h4><strong>setUpWithError : this is an initializer function to initialize your instances</strong></h4><h4><strong>tearDownWithError</strong> : this is a de-initializer function to de-initialize your instances</h4><p>You declare a function call testSuccessCalculation()</p><p>you have to set an input with expected output to make sure that your function works fine.</p><p>1 USD = 0.91 Euro.</p><blockquote>basically when you wanna write a test you have to think of these three phases :<br><strong>Given — When — Then</strong></blockquote><p><strong>Given -&gt;</strong> a certain input</p><p><strong>when -&gt;</strong> is a condition or an operation is performed</p><p><strong>Then -&gt; </strong>the particular output is expected</p><p>All of your test must follows this format.</p><p>So, i will enter [1] and supposed i have to get [0.91].</p><p>How do you do that ?</p><p>Simply,</p><p>// in Given:</p><p><strong>let</strong> price = 1.0</p><p>// in When:</p><p><strong>let</strong> total = calculate.calculateServicesPrice(euroPrice: price)</p><p>// in Then:</p><p>XCTAssertEqual(total, 0.91).</p><p>This test case will run successfully but if you changed this function to be like this XCTAssertEqual(total, 0.92) will be fail.</p><h3><strong>Let`s deep dive more in XCTAssert functions:</strong></h3><p>Most <a href="https://www.swiftbysundell.com/basics/unit-testing">unit tests</a> written using Apple’s built-in XCTest framework tend to follow a pattern</p><p>A test assertion’s main role is to compare a certain result against a control value, and to fail the current test if those two values don’t match. The assertions that ship as part of the built-in XCTest framework all have the prefix XCTAssert , the most basic of which simply compares any boolean value against true</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*53n-i1P_5pKk4u0gCKHXOw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0351eb26395" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A simple way to understand a unit test in iOS]]></title>
            <link>https://medium.com/@MohamedAglan/the-importance-of-unit-testing-in-ios-development-c32145532e64?source=rss-1fa950709942------2</link>
            <guid isPermaLink="false">https://medium.com/p/c32145532e64</guid>
            <dc:creator><![CDATA[mohamed Aglan]]></dc:creator>
            <pubDate>Sun, 31 Dec 2023 20:06:41 GMT</pubDate>
            <atom:updated>2023-12-31T20:07:50.797Z</atom:updated>
            <content:encoded><![CDATA[<p>unit testing is a way to test your code logic Unit tests are automated tests that run and validate a piece of code (known as the “unit”) to ensure it behaves as intended and meets its design.</p><p>Unit tests have their target in Xcode and are written using the <a href="https://developer.apple.com/documentation/xctest">XCTest framework</a>. A subclass of XCTestCase contains test methods to run in which only the methods starting with “test” will be parsed by Xcode and available to run.</p><p>let say that you have a function that do an operation to calculate the Average price of employee salaries.</p><p>So you can create a func takes the minimum and maximum salaries and divide them on 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RWz6lbGXLTLlJpEm9Bis1g.png" /></figure><p>So why should i write a test code for this function.<br>let images that you wrote this function and finished your project and someone else came to try to edit on some of your code and by accident changed the + operation to be -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fp6EqAR0wK7KFGWUdjKzrA.png" /></figure><p>in this case of you write a unit test for this code so you avoid this conflict.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c32145532e64" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory Management in IOS]]></title>
            <link>https://medium.com/@MohamedAglan/memory-management-in-ios-aa6ced2ea0f7?source=rss-1fa950709942------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa6ced2ea0f7</guid>
            <dc:creator><![CDATA[mohamed Aglan]]></dc:creator>
            <pubDate>Fri, 03 Dec 2021 22:42:12 GMT</pubDate>
            <atom:updated>2021-12-04T15:28:19.890Z</atom:updated>
            <content:encoded><![CDATA[<p>At the beginning we need to know what is memory management means ?</p><p>Memory Management is literally means that i need to handle memory space by take a piece of the memory to work on it when i need it and then clear this piece again after finishing my work on it to allow to something else to use this piece again.</p><p>So before we diving into memory management we need to know the basic definition of Value &amp; Reference Types:</p><ol><li>“<strong>Value Types</strong>” where <strong>Each instance keeps a unique copy of its data.</strong> Usually defined by</li></ol><ul><li>structs (incl. arrays and dictionaries)</li><li>enumerations</li><li>tuple</li><li>basic data types (boolean, integer, float, etc.)</li></ul><p>2) “<strong>Reference Types</strong>” where <strong>Instances share a single copy of the data</strong>, and the type is usually<strong> defined as a class</strong>.</p><p>So how can we do this?</p><p>Actually swift uses something call ARC (Automatic Reference Count), it helps us to manage memory.</p><p>So if we have ARC that can manage our memory So why is a retain cycle and memory leak happens?</p><p>A memory leak in iOS is when an amount of allocated space in memory cannot be deallocated due to retain cycles. Since Swift uses <a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html">Automatic Reference Counting (ARC)</a>, a retain cycle occurs when two or more objects hold strong references to each other. As a result these objects retain each other in memory because their retain count would never decrement to 0, which would prevent <a href="https://docs.swift.org/swift-book/LanguageGuide/Deinitialization.html">deinit</a> from ever being called and memory from being freed.</p><p>So what is strong and weak ?</p><p><strong>Strong</strong> references increase the <em>retain count</em> of instances they reference (by 1). This prevents the Automatic Reference Counting (ARC) from removing the object from memory, as long as the object is in use.</p><p>The opposite of a strong reference is a <strong>weak</strong> reference. In Swift, strong references are the default, so to make a reference weak you can use the weak keyword. Unlike strong references, a weak reference does not affect on instance’s retain count. It does not hold onto the object.</p><p>The next example is a an app that has “show red” button that will navigate to another view that has a tableView with red background color</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DcI9SZi0ekDMKlSOI_EJdw.png" /></figure><p>deinit function here will be printed after navigation to tell us that there is no memory leak here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*tbWZKtD9XMUqLpwZatHCcQ.gif" /></figure><p>after pressing on back button you will notice in the console will be printed “deinit Successfully” which means that there is no memory leaks.</p><p>so in the next example i will show this app with memory leak.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0iqssqVj26SyT63CCGPb9A.png" /></figure><p>i have created a new class call “Service” which has a relation with ViewControllerTwo class</p><p>and in the ViewControllerTwo class there is also a relation with Service Class using this line “<strong>let service = Service()”</strong></p><p>the relation between those two classes are strong and if we run the app again you will notice that it will not print <strong>deinit func</strong> So this mean there is a memory leak here.</p><p>So how can we fix this ???</p><p>the solution is very simple by using <strong>Weak</strong> keyword before “var viewControllerTwo”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mHw_qBHKWPEVcRehz9_Prw.png" /></figure><p>if you run your app again you will find the deinit is printed in console.</p><p>what you make here is breaking the strong relation between these two references by making this relation weak.</p><p>This is how memory leak happens so you can ask if there is another case that memory leak may happens ?</p><p>Yeah there is another case inside closures.</p><p>So how a retain cycle happens inside Closures?</p><p>let`s see the next code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zc7q6C3qhN_8Yhyj9sMH-Q.png" /></figure><p>i have a closure call refreshTable that takes Data and Error.</p><p>and i declared it inside ViewDidLoad()</p><p>and inside it`s declaration i printed the “<strong>closureVariableText</strong>” and used <strong>Self </strong>keyword because i`m inside a closure.</p><p>so there is a retain cycle will be introduced because self keyword.</p><p>so What is causing memory leak ?</p><p>When we use any external value inside the closure, it needs to keep a reference to them in order to execute itself. so it will create a strong reference to it.</p><p>So the solution here to write <strong>[Weak Self] </strong>inside the closure to break the strong reference and we make self keyword optional like this “self?.closureVariable”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PcDoMu5LrXFQCLQmglk54w.png" /></figure><p>so what if i don`t need to make self optional ??</p><p>so you have to check that self is not optional at first using guard like the code down</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d9fvGQIQVb2tcT3yeiGGug.png" /></figure><p>So there is another solution ?</p><p>Actually yeah you can use unowned keyword instead</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M9HtIkt6uxzJ7o9BT6b4Vw.png" /></figure><p>but in this case you have to guarantee that this self will never be nil.</p><p>And there is another way to check on memory leaks using Xcode instrument tool.</p><p>1- open your project and from product — profile or press on cmd + i</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ikx_9VCnNSrJYefYpPkCvQ.png" /></figure><p>it will opens this screen,Select Leaks</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZN0i7Xx5CV7fbk7v8O-8bQ.png" /></figure><p>to start testing your project press on the circle red button at the top left corner.and start using your application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5OWwfKDxNjmrHDroBbyAiw.png" /></figure><p>if you have a memory leak it will show you a red marker as shown in the top and if you don`t have it will show a green marker.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa6ced2ea0f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>