<?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 Inefficient Swift on Medium]]></title>
        <description><![CDATA[Stories by Inefficient Swift on Medium]]></description>
        <link>https://medium.com/@inefficientswift?source=rss-531a7fb61fda------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2C12z9uDpGs4uW_9NLlQcQ.png</url>
            <title>Stories by Inefficient Swift on Medium</title>
            <link>https://medium.com/@inefficientswift?source=rss-531a7fb61fda------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:58:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@inefficientswift/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[Image Loading with 17 Layers of Abstraction in SwiftUI]]></title>
            <link>https://inefficientswift.medium.com/image-loading-with-17-layers-of-abstraction-75f16a544406?source=rss-531a7fb61fda------2</link>
            <guid isPermaLink="false">https://medium.com/p/75f16a544406</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Inefficient Swift]]></dc:creator>
            <pubDate>Fri, 08 Aug 2025 06:56:07 GMT</pubDate>
            <atom:updated>2025-08-11T07:36:01.703Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Nail tech interviews. Confuse future you. Load an image.</strong></h4><p><em>Dedicated to everyone who’s ever spent a weekend crafting a clean architecture for a one-screen technical assignment.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DjZucCOzVftGihUpnsTMgA.png" /><figcaption><em>Every image must earn its way to the screen.</em></figcaption></figure><p>Anyone can load an image in SwiftUI with:</p><pre>Image(&quot;logo&quot;)</pre><p>But real professionals know that every image should pass through a labyrinth of protocols, factories, and mappers — just in case you ever decide to replace your app’s logo with a <strong>distributed blockchain‑rendered hologram</strong>.</p><h3>Step 1: Define a Protocol for Image Fetching</h3><p>Start with a protocol — not because you need one, but because it feels wrong not to.</p><pre>protocol ImageFetching {<br>    func fetchImage(named: String) async throws -&gt; Data<br>}</pre><h3>Step 2: Create a Repository Layer</h3><p>The repository doesn’t do anything except forward the request to another object.<br>Which is exactly how we like it.</p><pre>class ImageRepository: ImageFetching {<br>    private let dataSource: ImageDataSource<br>    <br>    init(dataSource: ImageDataSource) {<br>        self.dataSource = dataSource<br>    }<br>    <br>    func fetchImage(named: String) async throws -&gt; Data {<br>        try await dataSource.load(named: named)<br>    }<br>}</pre><h3>Step 3: Build the Data Source Interface</h3><p>We separate the data source so we can swap implementations later.<br>We won’t, but it’s the thought that counts.</p><pre>protocol ImageDataSource {<br>    func load(named: String) async throws -&gt; Data<br>}</pre><h3>Step 4: Implement the Asset Catalog Data Source</h3><p>No UI layer should ever touch an image file.<br>We’ll load it as raw Data so it’s <em>purely business logic</em>.</p><pre>class AssetCatalogDataLoader: ImageDataSource {<br>    func load(named: String) async throws -&gt; Data {<br>        guard let url = Bundle.main.url(/service/forresource: named, withExtension: &quot;png&quot;),<br>              let data = try? Data(contentsOf: url) else {<br>            throw NSError(domain: &quot;ImageError&quot;, code: 404)<br>        }<br>        return data<br>    }<br>}</pre><p><strong>Note:</strong> We’re now slower, using more memory, and ignoring asset catalog optimizations. Perfect. ✅</p><h3>Step 5: Add an Image Mapper Interface</h3><p>Because one more protocol never hurt anyone.</p><pre>protocol ImageMapping {<br>    func map(data: Data) -&gt; CGImage?<br>}</pre><p>Now we can swap out our mapping strategy whenever Apple breaks image decoding. Again.</p><h3>Step 6: Implement the Image Mapper</h3><p>Mappers are where we hide the ugly code.<br>And this one? Deserves it.</p><pre>class DefaultImageMapper: ImageMapping {<br>    func map(data: Data) -&gt; CGImage? {<br>        guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {<br>            return nil<br>        }<br>        return CGImageSourceCreateImageAtIndex(source, 0, nil)<br>    }<br>}</pre><h3>Step 7: Create the Image Loader</h3><p>Now we tie together the fetcher and the mapper to create an extremely roundabout image loader.</p><pre>class ImageLoader {<br>    private let repository: ImageFetching<br>    private let mapper: ImageMapping<br><br>    init(repository: ImageFetching, mapper: ImageMapping) {<br>        self.repository = repository<br>        self.mapper = mapper<br>    }<br><br>    func load(named name: String) async -&gt; CGImage? {<br>        guard let data = try? await repository.fetchImage(named: name) else {<br>            return nil<br>        }<br>        return mapper.map(data: data)<br>    }<br>}</pre><h3>Step 8: Create a Factory</h3><p>Because we might one day want to swap implementations using a YAML file.</p><pre>enum ImageLoaderFactory {<br>    static func make() -&gt; ImageLoader {<br>        let dataSource = AssetCatalogDataLoader()<br>        let repository = ImageRepository(dataSource: dataSource)<br>        let mapper = DefaultImageMapper()<br>        return ImageLoader(repository: repository, mapper: mapper)<br>    }<br>}</pre><h3>Step 9: Add a ViewModel</h3><p>SwiftUI doesn’t want one — but we do.</p><pre>class LogoViewModel: ObservableObject {<br>    @Published var image: CGImage?<br><br>    func loadImage() {<br>        Task {<br>            let loader = ImageLoaderFactory.make()<br>            self.image = await loader.load(named: &quot;logo&quot;)<br>        }<br>    }<br>}</pre><h3>Step 10: Build the SwiftUI View</h3><p>At long last, we render the image — the way nature never intended.</p><pre>struct LogoView: View {<br>    @StateObject var viewModel = LogoViewModel()<br><br>    var body: some View {<br>        Group {<br>            if let image = viewModel.image {<br>                Image(decorative: cgImage, scale: 1, orientation: .up)<br>            } else {<br>                ProgressView()<br>            }<br>        }<br>        .onAppear {<br>            viewModel.loadImage()<br>        }<br>    }<br>}</pre><h3>Conclusion</h3><p>✅ <strong>Benefits</strong>:</p><ul><li>Can replace your asset catalog at any time with an S3 bucket, CD‑ROM, or floppy disk.</li><li>Impresses enterprise architects.</li><li>Provides plenty of job security.</li></ul><p>❌ <strong>Drawbacks</strong>:</p><ul><li>Literally everything else.</li></ul><p><strong>Enjoyed this?<br></strong>Subscribe so you don’t miss the next one: <em>Mastering Corner Radius with Machine Learning.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75f16a544406" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>