<?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 Jean-David Morgenstern-P on Medium]]></title>
        <description><![CDATA[Stories by Jean-David Morgenstern-P on Medium]]></description>
        <link>https://medium.com/@jeandavidpeirolo?source=rss-fd8c794f2bdc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ntooehPfPs4CeZFb.</url>
            <title>Stories by Jean-David Morgenstern-P on Medium</title>
            <link>https://medium.com/@jeandavidpeirolo?source=rss-fd8c794f2bdc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:55:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jeandavidpeirolo/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[Is there such a thing as a static framework?]]></title>
            <link>https://jeandavidpeirolo.medium.com/is-there-such-a-thing-as-a-static-framework-f535c2fdba64?source=rss-fd8c794f2bdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/f535c2fdba64</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Jean-David Morgenstern-P]]></dc:creator>
            <pubDate>Fri, 12 Mar 2021 09:42:35 GMT</pubDate>
            <atom:updated>2021-03-12T09:42:35.762Z</atom:updated>
            <content:encoded><![CDATA[<p>At Apple support they seem quite definitive on the topic: There is no such thing as a <a href="https://developer.apple.com/forums/thread/105062">“static framework”</a>. On the other hand, the internet is full of discussions that compare static vs dynamic frameworks. This posts tries to resolve the ambiguity by considering two concepts: Bundle programming, a macOS and iOS technology, and dynamic vs static linking, a more generic concept in computer science.</p><h4>A type of bundle</h4><p>A framework might have a different meaning depending on how you look at it. From a Swift developer point of view, a framework is a module, which is a reusable piece of code that one can access via an import statement. From an Xcode perspective, a framework is one of the available product for targets, along with applications, plugins and libraries.</p><p>Both views offer some truth about frameworks but at the simplest, a framework is a hierarchical directory with platform specific content. Apple uses another name for this kind of structured folder, that is a bundle. A framework is one type of bundle. Other types of bundles include the Application bundle and the Plug-ins bundle. Unlike an application bundle (.app file), a framework bundle is not a package, it doesn’t appear as an opaque file to the user but as a regular directory file. This is an invite for the framework’s consumer to check its content.</p><p>Let us peek inside the <a href="https://github.com/Yummypets/YPImagePicker">YPImagePicker</a> open source framework:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lWh0QPT6FTdRR1HHgre8Bg.png" /><figcaption>YPImagePicker Framework</figcaption></figure><p>We can see what problem a framework solve. It bundles together an executable and the resources (such as images, localized strings, xibs) it depends on, making the all entity easy to share and reuse.</p><h4>Let us inspect the executable</h4><p>MacOS and iOS uses the Mach-O executable format for their binaries. Mach-O binaries can be of several types: executable, archive or dynamically shared library.</p><p>The file command provides us with information on the binary Mach-O type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9XaXFiuJ-u5ewhG5EminPQ.png" /><figcaption>file YPImagePicker</figcaption></figure><p>The YPImagePicker framework includes a dynamically linked shared library file, or dynamic library. The dynamic framework tag seems indeed appropriate in this case. (In fact, the binary stacks four different architectures into one file. This used to be an easy way to ship one binary and let the developers strip unwanted architectures in the build process. xcFramework makes this system obsolete.)</p><p>Not all frameworks include dynamic libraries. Let us now peek inside the <a href="https://developers.google.com/identity/sign-in/ios/sdk#download_the_google_sign-in_sdk">GoogleSignIn</a> framework:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AMTEAcQU1PTph-WWeAWW8Q.png" /><figcaption>GoogleSignIn Framework</figcaption></figure><p>And run the file command on the binary:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QZByRt7XImjHFq1ZACH0ew.png" /><figcaption>file GoogleSignIn</figcaption></figure><p>The GoogleSignIn framework includes an archive, or static library, which is a convenient way to package multiple relocatable object files.</p><p>There is nothing exceptional in having a framework host a static library. Go over the build settings of your Xcode project and check the Mach-O type setting:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KZcNErrc9cOXmCY7juJ34Q.png" /></figure><p>As expected, the default Mach-O type for a Framework project is Dynamic Library, for a Static Libary project is Static Library and for an Application project is Executable. But you are free to edit it.</p><h4>The Xcode archive</h4><p>Some frameworks encapsulate dynamic libraries, others static libraries. It seems therefore natural to call the former dynamic frameworks and the latter static frameworks. However there is a fundamental difference in how dynamic frameworks are consumed.</p><p>Dynamic frameworks and their dynamically shared libraries are not fully linked into the application executable at the end of the build process. As such, they need to be embedded into the application bundle. On the contrary, once the static linker is done, all symbols have been resolved. Text and data sections have been merged into one self-sufficient executable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8z6FVkilMoMo3cJiqUCN8g.png" /></figure><p>The output of the Xcode build process is an Xcode archive (a type of package specific to macOS and iOS, not to be confused with a static archive!). If we were to Xcode archive an app with the framework setup as shown in the above picture, we would get an application bundle with this configuration:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*Ayou6Pdw9lVVTnO_ISVXiA.png" /><figcaption>App Bundle</figcaption></figure><p>The application bundle includes the MyApp executable along with frameworks that were embedded into the app, all residing in the Frameworks folder. But the application bundle doesn’t keep any trace of the non-embedded frameworks, the frameworks hosting static libraries. The GoogleSignIn executable is now part of the MyApp executable.</p><h4>Accessing Resources</h4><p>If frameworks are a convenient way to ship source code with the resources it depends on, then Apple has probably made it easy to access those resources from the source code. And they did! As a type of bundle, frameworks can use the Bundle class of the Foundation utility to access their content without worrying about the underlying structure.</p><p>Consider setting an image view’s image where the source code and the resource itself are part of the framework:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/572e1758c82348697f691c264a91d6ea/href">https://medium.com/media/572e1758c82348697f691c264a91d6ea/href</a></iframe><p>This code works fine in the case of a dynamic framework embedded in an application bundle. But would the framework executable be statically linked, it would crash. Indeed, in the latter case, the framework compiled source is already part of the app executable, the framework bundle is not embedded in the application bundle, so the frameworkBundle variable at line 5 returns nil at runtime.</p><p>One way to partially fix the issue uses another initializer of the Bundle class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a739d5341ef795e8d902aa77a067cd21/href">https://medium.com/media/a739d5341ef795e8d902aa77a067cd21/href</a></iframe><p>Here we assume that the source code and the resource will sit together. This is always true for a dynamic, embedded framework. But it requires an additional step for the consumer of a static framework. One should copy the framework’s resources into the main application bundle. That is the reason why static framework that relies on resources often comes with specific resource bundles.</p><p>Even though frameworks can host statically linked binaries, their bundle won’t be part of the final application bundle. The source code can’t assume resources are part of the framework’s bundle, which is one of the main “raison d’etre” of frameworks. Static frameworks try to merge two concepts but at the end of the road, the static linking takes precedence over the framework.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f535c2fdba64" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How UILayoutGuide can help you manage UI complexity]]></title>
            <link>https://jeandavidpeirolo.medium.com/how-uilayoutguide-can-help-you-manage-ui-complexity-8e911c9bcdd7?source=rss-fd8c794f2bdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e911c9bcdd7</guid>
            <category><![CDATA[uikit]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Jean-David Morgenstern-P]]></dc:creator>
            <pubDate>Tue, 17 Nov 2020 15:01:57 GMT</pubDate>
            <atom:updated>2020-11-22T11:05:05.936Z</atom:updated>
            <content:encoded><![CDATA[<p>Lay it out once, use it anywhere.</p><p>Do you know the default leading margin of a view controller’s root view on an iPhone 11 in landscape mode? If you don’t, that’s ok. The important thing to know is that the default leading margin is different on an iPhone 11 in landscape mode (64px) than it is on an iPhone 12 in portrait mode (20px) or on an iPhone 8 in portrait mode (16px).</p><p>Today iOS developers can deploy their code on multiple devices, from an Apple Watch to a Mac. A few years ago, life was simpler. There was one iPhone device, two orientations. With the introduction of iPadOS, Split View, and multi window support, there is now an infinity of window’s sizes iOS developers should adapt their UI code to. Devices come with edge cases, from top notch to bottom home bar.</p><p>Apple has introduced several APIs to help developers manage UI complexity. This article will show you what you should care about and what you should let UIKit deal with.</p><h3>Constraints with fixed constants</h3><p>When AutoLayout was introduced, developers started to convert hard coded frames into constraints. Back then, the unique iPhone’s default leading margin was 16px. Constraining a label’s leading anchor to a view controller’s root view would often include a fixed 16x offset:</p><pre>label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: <strong>16</strong>).isActive = true</pre><p>Unfortunately adding a fixed constant to a constraint in that way breaks the label’s layout on many devices and orientations. While that layout would work fine on an iPhone 8 in portrait mode, the label appears offscreen on an iPhone 11 in landscape mode:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XFtRPvBfjYQVHnY-0EHsYQ.jpeg" /></figure><h3>Safe Area Layout Guide</h3><p>When Apple released the iPhone X, the first iPhone to include a top notch and a bottom home bar, it also introduced safeAreaLayoutGuide, a new UIKit layout guide to safely layout views within the visible portion of its superview. Modify the constraint to take into account the safe area:</p><pre>label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true</pre><p>Now the label is rendered within the visible portion of its superview, highlighted in red:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ofga21w1KTE3UK_Ojo2NlQ.png" /><figcaption>Label constrained to safe area layout guide on iPhone 11</figcaption></figure><p>However the safe area is only part of the solution. If it helps to ensure UI elements stay visible in any situation, it doesn’t take into account a view’s margin. Flip the device into portrait mode and the constrained label is laid out at the screen edge. Indeed in portrait mode, views are unobscured by other contents and therefore the safe area fits along the screen vertical edges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uar5sS1deR18azXRr4v2KA.jpeg" /></figure><h3>Layout Margins Guide</h3><p>There are in fact two requirements for the label layout. First it should reside within its superview safe area so it stays unobscured in landscape mode. Second, the label should have a leading margin in portrait mode.</p><p>How about adding a fixed 16px margin to the safe area guide?</p><pre>label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16).isActive = true</pre><p>There are a couple of issues with that, one of them being the number 16 itself. 16px is the default horizontal margin for iPhone 8 in portrait mode. But on iPhone 11 the default horizontal margin goes up to 20px.</p><p>Why should you care about the system default margin? Because you live in an iOS world with iOS components that all use the system default margins. Suppose you have a navigation bar. Then you would probably want the label to have the same leading margin than the bar button items.</p><p>The layoutMarginsGuide, while older than the safeAreaLayoutGuide, will keep your content both unobscured and within the system margins. Set the label constraint once again but this time against the layout margins guide, highlighted in green in below screenshots.</p><pre>label.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3n-51al7i6nyDlHwt1fuEA.jpeg" /><figcaption>Label constrained to layout margins guide on iPhone 8</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NRUnOxTGREBD1-YlbiDIAw.jpeg" /><figcaption>Label constrained to layout margins guide on iPhone 11</figcaption></figure><p>Any view’s layout margin can be modified through its layoutMargins property, or even better its directionalLayoutMargin property.</p><p>View controller’s root view default layout margins vary a lot across devices and orientations. I mean, this is the all point of this article, right? So modify them at your own risk:)</p><p>However setting a subview layout margin is a great alternative to position content. Subviews have a default spacing of 8px in each direction. Constrain your view to its container view layout margins guide, set the view’s directionalLayoutMargin property and you will never need to add a positive constant (or is it negative?) to a constraint ever again.</p><h3>Readable Content Guide</h3><p>It would not feel right to delve into layout guides without mentioning another layout guide the system provides, the readable content guide.</p><blockquote>The readable content guide defines an area that can easily be read without forcing users to move their head to track the lines.</blockquote><p>Open the medium app on an iPad in landscape mode, tap on an article, and you will see that the text is vertically centered into an area whose width is smaller than that of the layout margins guide.</p><p>Let me show you the three system layout guides on an iPhone 11 in landscape mode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XR7RqasdyaHN1CRAdTlBpA.png" /><figcaption>safeAreaLayout in red, layoutMargins in green, readableContent in blue</figcaption></figure><p>In this article, you have seen how to use UIKit layoutMarginsGuide to create an adaptive layout that responds to every size, edges and orientations. And by leveraging a view’s directionalLayoutMargin property, you should be able to constraint your UI without ever adding any fixed constants.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e911c9bcdd7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DYLD, Symbol not found]]></title>
            <link>https://jeandavidpeirolo.medium.com/dyld-symbol-not-found-4b750d93bd46?source=rss-fd8c794f2bdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b750d93bd46</guid>
            <category><![CDATA[linking]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Jean-David Morgenstern-P]]></dc:creator>
            <pubDate>Thu, 02 Jul 2020 05:25:28 GMT</pubDate>
            <atom:updated>2020-07-02T05:33:28.972Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yoC2gdrlRZZje4SFzE9yzg.jpeg" /></figure><p>Let us revisit dynamic linking and then solve an otherwise cryptic iOS crash.</p><p>I have recently come across an unusual bug, whose Xcode crash point output is:</p><pre>Exception Type:  EXC_CRASH (SIGKILL)<br>Exception Codes: 0x0000000000000000, 0x0000000000000000<br>Exception Note:  EXC_CORPSE_NOTIFY<br>Termination Description: DYLD, Symbol not found: _objc_opt_new | Referenced from: /private/var/containers/Bundle/Application/MyApp.app/Frameworks/MyFramework.framework/MyFramework | Expected in: dyld shared cache</pre><pre>Thread 0 Crashed:<br>...<br>4 dyld           0x0000000100f38a14 dyld::fastBindLazySymbol (ImageLoader**, unsigned long) + 284 (dyld.cpp:4112)<br>5 libdyld.dylib  0x00000001f7634848 dyld_stub_binder + 60<br>6 MyFramework  0x00000001033721f4 +[MyLibrary myMethod:]<br>...</pre><p>Apple crash report points to a missing symbol referenced in some custom framework. Looking at the crashing thread backtrace, we see indeed some familiar method call at line 6. But then the execution moves to a system module called dyld at line 4. It is the same module that “Terminate Description” mentions: DYLD, Symbol not found. DYLD stands for dynamic loader. It is Apple’s macOS and iOS module responsible for dynamically linking libraries.</p><p>To decipher our bug, we should first remind ourselves how linking works and how dynamic linking differs from static linking. Hopefully this will help us understand how on earth could some symbol not be found at runtime and cause our app to crash.</p><h3>The Linking Step</h3><p>Building an executable involves a series of steps. Programs are translated from human readable code into machine code. The linker is usually the last step of the compilation system. It takes as input multiple object files and concatenates their sections to produce an executable ready to be loaded into memory and executed.</p><p>An object file defines and references symbols (a variable or a method name). One of the linker’s jobs is to resolve symbol references. It associates each reference with a definition, possibly across different modules.</p><p>Linking can be performed at compile time, right after the assembler outputs an object file containing machine code. This is called static linking. Once the static linker is done, all symbols have been resolved. Text and data sections have been merged into one self-sufficient executable.</p><h3>Dynamic Linking</h3><p>Linking can also be performed later. At load time, when the program is copied into memory; or even at run time. This is called dynamic linking.</p><p>The idea of dynamic linking is to split the linking into two steps. First, when the executable is created, the linker does some of its job statically. For instance, it resolves symbol references for locally defined symbols. Then, a program called the dynamic linker completes the linking process by relocating text and data sections, updating previously unresolved symbol references with their final memory location.</p><h3>Linking on iOS</h3><p>On iOS, frameworks and libraries are two types of modules that can be linked to another module.</p><p>Frameworks can be dynamically or statically linked. Custom libraries can only be statically linked (Swift Package Manager recently introduced dynamic linking for Libraries). System libraries however, such as UIKit, are dynamically linked into applications.</p><h3>Symbol Not Found</h3><p>Let us now go back to our crash and let me show you once again its backtrace, right before the dylb module is executed:</p><pre>6 MyFramework  0x00000001033721f4 +[MyLibrary myMethod:]</pre><p>First I owe you some important precisions:</p><ul><li>MyFramework is a framework, compiled from source code when Xcode builds the application.</li><li>MyFramework is dynamically linked into the application.</li><li>MyLibrary is a pre-compiled library: it’s an archive, .a file, that MyFramework links against.</li><li>MyLibrary is statically linked into MyFramework.</li><li>MyLibrary links against UIKit.</li></ul><p>We now have all the information we need. It’s time to piece them together.</p><p>The crash happens when we call myMethod of MyLibrary. This method triggers a call to the dyld. It means that myMethod calls another method or references a variable from a library it is dynamically linked against. As listed above, MyLibrary links against UIKit.</p><p>How could MyLibrary reference a symbol from UIKit that the dynamic loader can’t find at run time? This scenario would happen if MyLibrary had been pre-compiled against a version of UIKit that is different from the one the dynamic linker finds at run time.</p><p>To recap:</p><ul><li>MyLibrary is pre-compiled against a newer version of UIKit.</li><li>MyFramework statically links agaisnt the pre-compiled MyLibrary.</li><li>The iOS application embeds MyFramework — dynamic linking.</li><li>Xcode builds the iOS application against an older version of UIKit</li><li>At some point of its execution, myMethod of MyLibrary references a UIKit symbol. The dynamic loader can’t resolve this symbol because it can’t find its definition in the older UIKit the application links against.</li></ul><p>The mix of source and binary frameworks/libraries, along with static and dynamic linking, can lead to runtime bugs Xcode build process can’t help you with.</p><p>The funny thing about this bug is how easy it is to fix once you understand it. One just needs to recompile MyLibrary against the same version of UIKit the iOS app uses.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b750d93bd46" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build and use an XCFramework]]></title>
            <link>https://medium.com/macoclock/how-to-build-and-use-an-xcframework-40e4e7a53e2c?source=rss-fd8c794f2bdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/40e4e7a53e2c</guid>
            <category><![CDATA[xcframework]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[objective-c]]></category>
            <dc:creator><![CDATA[Jean-David Morgenstern-P]]></dc:creator>
            <pubDate>Mon, 18 May 2020 05:55:05 GMT</pubDate>
            <atom:updated>2022-06-17T07:02:41.598Z</atom:updated>
            <content:encoded><![CDATA[<p>XCFramework were introduced on wwdc 2019. They allow us to package different architectures of the same module into one entity.</p><p>On iOS, frameworks (dynamic or static) and libraries (static only) are two types of module that can be linked to another module, either in a compiled state or not.</p><p>Building an XCFramework is a two step process. First the framework or library needs to be compiled from source code. Xcode calls this product an archive. Then the archive is packaged into an XCFramework.</p><p>This article will show you how to proceed with both steps.</p><h3>Why an archive</h3><p>There are two main reasons for picking an archive (a compiled state of code) over source code.</p><p>The most obvious one is when you have no other choice. The framework or library might be from a vendor that only shares the compiled version of their code.</p><p>The second reason is to improve compile time. Even though Xcode gets smarter with the years, linking against a non compiled framework or library will increase your app compile time. If your project includes a framework or library that doesn’t change very often, it makes sense to consider compiling it and linking against its archive.</p><h3>How to archive a framework</h3><p>iPhone and Simulators run apps on different architectures. When Xcode builds an archive, it builds it for a specific architecture. Therefore if you want to create an XCFramework to run on both iPhone and Simulators, you will need to create two versions of the archive. Here’s the command line for archiving a framework with the iPhone sdk:</p><pre>xcodebuild archive <br>    -project SomeFramework.xcodeproj<br>    -scheme SomeFramework <br>    -destination &quot;generic/platform=iOS&quot;<br>    -archivePath &quot;archives/SomeFramework&quot;<br>    SKIP_INSTALL=NO<br>    BUILD_LIBRARY_FOR_DISTRIBUTION=YES</pre><p>One small edit will archive the framework for the simulators: -destination &quot;generic/platform=iOS Simulator&quot;. Notice the BUILD_LIBRARY_FOR_DISTRIBUTION flag. You need it to create a stable Swift module, i.e a module that is compatible with different version of Swift.</p><h3>How to archive a library</h3><p>Building an archive for a library is quite similar to the process of archiving a framework. Except if you want to make it available to Swift. Whereas the default option for a framework is to define a module, it is not the case for a library.</p><p>Packaging a library into a module requires editing two build settings and adding a build phase:</p><ul><li>Turn the Define Module setting to YES.</li><li>Create a module.modulemap (create a new file, pick the empty template) and place it in your library directory. A simple module map file can look like: (see <a href="http://clang.llvm.org/docs/Modules.html#module-map-file">http://clang.llvm.org/docs/Modules.html#module-map-file</a> for documentation on module maps)</li></ul><pre>module SomeLibrary {<br>    umbrella header &quot;SomeLibrary.h&quot;<br>    export *<br>}</pre><ul><li>Edit the Module Map setting with your file locationSomeLibrary/module.modulemap. Make sure the module map is in the same directory than the public header files. Or modify the header’s path accordingly.</li><li>Add a Copy Files build phases and include the umbrealla header and the module map.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-x-8jkyUnD3bvec0wdYBhw.png" /></figure><p>After completing these preliminary steps, run the following command line for archiving a library with the iPhone sdk:</p><pre>xcodebuild archive <br>    -project SomeLibrary.xcodeproj<br>    -scheme SomeLibrary <br>    -destination &quot;generic/platform=iOS&quot;<br>    -archivePath &quot;archives/SomeLibrary&quot;<br>    SKIP_INSTALL=NO<br>    BUILD_LIBRARY_FOR_DISTRIBUTION=YES</pre><p>One small edit will archive the library for the simulators: -destination &quot;generic/platform=iOS Simulator&quot;.Notice the BUILD_LIBRARY_FOR_DISTRIBUTION flag. You need it to create a stable Swift module, i.e a module that is compatible with different version of Swift.</p><h3>Why an XCFramework</h3><p>Or why is an archive not enough.</p><p>As mentioned above, Xcode builds an archive for a specific architecture. Therefore an archive could be run on the iPhone or on the simulator but not on both.</p><p>One solution is to combine different archives, each supporting a targeted architecture, into one package and let Xcode pick the correct archive at compile time.</p><p>Before XCFramework was introduced, developers packaged multiple archives into a so called Fat Framework. This enabled developers to ship only one package, supporting different architectures, but it also meant that it was upon the package users to add a build phase to strip the Fat Framework from not compatible archives.</p><p>Apple introduced XCFramework in 2019. XCFramework packages different archives into one xcframework product. On top of that, XCFramework format is supported by Xcode, removing the need to manually strip out not compatible archives during a build phase. Xcode takes care of it automatically.</p><h3>How to build an XCFramework</h3><p>You can create an archive by running a command line, as shown above. Or you can use Xcode interface. At the time of this writing, Xcode doesn’t provide any graphical interface to create an XCFramework. You are left with the command line (official <a href="https://help.apple.com/xcode/mac/11.0/#/dev544efab96">documentation)</a>:</p><pre>xcodebuild -create-xcframework<br>    -framework &quot;archives/SomeFramework-Sim.xcarchive/Products/Library/Frameworks/SomeFramework.framework&quot;<br>    -framework &quot;archives/SomeFramework.xcarchive/<br>Products/Library/Frameworks/SomeFramework.framework&quot;<br>    -output &quot;xcframeworks/SomeFramework.xcframework&quot;</pre><p>It takes as input all your targeted architectures archives. If you have archived a library instead of a framework, simply change -framework with -library .</p><h3>How to use an XCFramework</h3><p>Drag and drop your .xcframework into the Frameworks, Libraries, and Embedded Content section of your target’s General tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u92gjG_UljkaMYl_HoKW8g.png" /></figure><p>Now is time to tell Xcode where to look for the module.</p><p>If your XCFramework includes a framework, edit the Framework Search Paths setting.</p><p>If your XCFramework includes a library, edit the Header Search Paths and Library Search Paths . If you link against a library that is not part of the same repo, you will need to copy over the public header files as well (in addition of the XCFramework of course!).</p><p>That’s it, you can now call your framework or library from another module. I hope that after reading this tutorial, you have acquired a better understanding of why you should consider using XCFramework and how to build them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40e4e7a53e2c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/macoclock/how-to-build-and-use-an-xcframework-40e4e7a53e2c">How to build and use an XCFramework</a> was originally published in <a href="https://medium.com/macoclock">Mac O’Clock</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrency and Asynchronism in (NS)Operation]]></title>
            <link>https://jeandavidpeirolo.medium.com/concurrency-and-asynchronism-in-ns-operation-82b7d9e16b74?source=rss-fd8c794f2bdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/82b7d9e16b74</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Jean-David Morgenstern-P]]></dc:creator>
            <pubDate>Fri, 07 Feb 2020 14:21:11 GMT</pubDate>
            <atom:updated>2020-02-09T14:08:26.251Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*U9cKl_8lNcBleTgfzYpAdg.jpeg" /></figure><p>What concurrency and asynchronism are. How to use them correctly in the context of Operation and OperationQueue.</p><p>I find Apple’s API and documentation on (NS)Operation confusing. It uses the notions of synchronism and concurrency interchangeably. In this article, we will define what synchronism and concurrency are and understand how to use them on Operation instances. We will outline an implementation of an asynchronous Operation subclass. By the end of our read, we will be able to design a set of operations, synchronous or asynchronous, that run concurrently in a synchronous or asynchronous way with respect to the current thread.</p><p>Operation (previously NSOperation) and OperationQueue are Foundation’s highest level of abstractions for managing concurrency. An operation instance encapsulates data and logic to run a single task. Operation and Operation Queues are particularly useful when we need to schedule a series of tasks in some order. They enable us to add dependencies to operations and to prioritise them in a declarative syntax. We won’t detail basic usages of Operation’s methods and properties. Great tutorials on the subject have already been published. Instead let’s turn our focus on two of Operation’s main features: asynchronism and concurrency.</p><h3>Asynchronism</h3><p>By default, operation instances are synchronous with respect to the current thread. Apple writes:</p><blockquote>When you call the <em>start()</em> method of a synchronous operation directly from your code, the operation executes immediately in the current thread. By the time the <em>start()</em> method of such an object returns control to the caller, the task itself is complete.</blockquote><p>Synchronous operations are operations whose state is finished when the start() method returns.</p><p>Asynchronous operations are operations whose state is undetermined when the start() method returns.</p><p>Let us design first a synchronous operation. At the very least, we should override the main() method. This method is called by the start() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5b0b4777746536a1c0f37a420528a30/href">https://medium.com/media/b5b0b4777746536a1c0f37a420528a30/href</a></iframe><p>The synchronous operation guarantees that its task has completed before we reach the next line of code that outputs “Done”.</p><p>Let us now design an asynchronous operation, where the start() method may return control to the caller before its task has completed. One way to do that is by dispatching a block of code to another thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55a702315e3b3484d95a8465265ac809/href">https://medium.com/media/55a702315e3b3484d95a8465265ac809/href</a></iframe><p>The asynchronous operation offers no guarantee that its task has completed before we reach the next line of code. Our code can produce the same output than the earlier synchronous example or it may first print “Done” and only then “Executing”.</p><p>A synchronous operation can still be executed asynchronously with respect to the current thread. For instance, this happens when you use an operation queue. In that case, you delegate to the queue the responsibilty to decide when to call the start method. The SDK makes sure that this call happens from a different thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5944c6b813b4dcdd121d27ccdd0c601d/href">https://medium.com/media/5944c6b813b4dcdd121d27ccdd0c601d/href</a></iframe><p>To sum up, there are two ways to make an operation executes asynchronously with respect to the current thread. You can manually start an asynchronous operation or you can dispatch a synchronous (or asynchronous) operation to a different thread.</p><h3>Ambiguous Asynchronism</h3><p>I find Apple’s use of the adjective synchronous in the context of Operation confusing. First there is a discrepancy between the <a href="https://developer.apple.com/documentation/foundation/operation">new</a> and the <a href="https://developer.apple.com/library/archive/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationObjects/OperationObjects.html#//apple_ref/doc/uid/TP40008091-CH101-SW1">old</a> documentation. The new documentation compares asynchronous vs synchronous operations, as quoted above. The old documentation refers to concurrent versus non-concurrent operations. In the same vein, the new API encourages us to use the isAsynchronous property instead of the old isConcurrent property.</p><p>Synchronism has nothing to do with concurrency or multi-threading but is concerned with dependency. Two tasks are asynchronous if their order of execution is of no consequence. The amazing fact is that Operation includes a clear API for synchronism:</p><blockquote>Dependencies are a convenient way to execute operations in a specific order. You can add and remove dependencies for an operation using the addDependency(_:) and removeDependency(_:) methods.</blockquote><p>How are we to understand an asynchronous operation in the language of Apple? It depends on the other side of the equation. If we look at the current thread, then an asynchronous operation is an operation whose undetermined state would have no effect on the next line of code. To introduce synchronism between two operations, we should add one as a dependency of the other.</p><h3>Designing an Asynchronous Operation</h3><p>Synchronous operations only need to override their main() method and their state is managed out of the box. This is no longer the case for asynchronous operations. Asynchronous operations are responsible for updating their state, overriding the start() method and both isExecuting and isFinished properties in a thread safe and KVO compliant way.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/871b71560838d2378202c97054199bbc/href">https://medium.com/media/871b71560838d2378202c97054199bbc/href</a></iframe><h3>Concurrency</h3><p>Two tasks are said to run concurrently when the second task starts before the first task finishes. This happens when the tasks are executed on different threads.</p><p>When a task completes before another task starts, we say that the tasks are executed serially. Here is a serial execution of a two operations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f5b27a68af1400be17b683267a2ee10/href">https://medium.com/media/5f5b27a68af1400be17b683267a2ee10/href</a></iframe><p>To introduce concurrency, we can use the dedicated OperationQueue.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aba753618c3d3349976c388081246d59/href">https://medium.com/media/aba753618c3d3349976c388081246d59/href</a></iframe><p>Operation queues manage a pool of thread upon which they execute operations concurrently. Without specifying dependencies, the order in which operations finish is undetermined.</p><h3>When Asynchronism meet Concurrency</h3><p>We noted above that both synchronous and asynchronous operations can be executed asynchronously with respect to the current thread. Operations queues add the possibility to run a set of operations concurrently in asynchronous as well as in synchronous way. To block the current thread, you can use the OperationQueue method addOperations(_ ops: [<a href="https://developer.apple.com/documentation/foundation/operation">Operation</a>], waitUntilFinished wait: <a href="https://developer.apple.com/documentation/swift/bool">Bool</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/acd2083fb1b6f35fe521abe98467bb61/href">https://medium.com/media/acd2083fb1b6f35fe521abe98467bb61/href</a></iframe><p>Let us wrap up with a question: How would you make sure two operations, the first one being asynchronous while the second one is synchronous, run serially and return control to the current thread in a synchronous manner?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82b7d9e16b74" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modularity on iOS]]></title>
            <link>https://jeandavidpeirolo.medium.com/modularity-on-ios-d294f45594f5?source=rss-fd8c794f2bdc------2</link>
            <guid isPermaLink="false">https://medium.com/p/d294f45594f5</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[modular]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[software]]></category>
            <category><![CDATA[mobile]]></category>
            <dc:creator><![CDATA[Jean-David Morgenstern-P]]></dc:creator>
            <pubDate>Tue, 28 Jan 2020 14:55:16 GMT</pubDate>
            <atom:updated>2020-02-05T09:32:38.191Z</atom:updated>
            <content:encoded><![CDATA[<p>What modularity is. What it is not. And why we need it.</p><p>Many have published articles on their journey to modularize an existing codebase. I wish to take a step backward and first consider what modularity is and why it is important for large projects and teams.</p><p>Once we have a clear understanding of the concept, and we all agree on the need, we can roll up our sleeves and decide upon the best way to achieve greater modularity.</p><h3><strong>What is Modularity?</strong></h3><p>Modularization is the process of splitting a codebase into <strong>small, reusable and independent</strong> components called modules.</p><p>A module is a piece of code that can be imported by another module. Frameworks, libraries, applications, and in fact any build target in Xcode, are modules. A single project can include multiple targets and therefore build multiple different modules.</p><h3>A Fat Dependency Graph</h3><p>While often understood at the conceptual level, modularity is hard to achieve in practice because it requires foresight.<br>Consider this graph usually regarded as describing an ideal modular state of a codebase:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P9WAlMGw9Y311K6-tiu0UQ.png" /><figcaption>Graph Dependency — In Theory</figcaption></figure><p>There are three layers. The top layer represents the application layer, the middle layer represents the features and the bottom layer represents the core services.</p><p>A non modular implementation would have the Application call a feature, say the Product module, which in turn imports all of the core services.<br>At first glance this division seems to reflect a modular codebase with small components. After a deeper look it becomes clear the components are not reusable or independent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BZx3v0TjyDvUJT4wroPL8w.png" /><figcaption>Graph Dependency — In Practice</figcaption></figure><p>What happens if we want to reuse the Product module in another application? Then this second application would also need to include the Network, Models and Analytics modules. Or whatever feature modules Product depends on such as Cart. We realize that these modules, despite being small, are not independent. Why is it an issue?</p><ul><li>The Product module can considerably increase the size of the second app.</li><li>What if the second app has its own network or analytics implementation? There could be a conflict or code duplication.</li><li>Any change to one of the imported modules and Xcode will recompile the all tree of dependencies.</li><li>Any bug to one of the imported modules can possibly break the Product module.</li></ul><h3>Hard vs Soft Dependencies</h3><p>Modules often depend on other modules for services such as Network, Analytics, Models. If that is so, how can we say that a module is independent? What does dependency mean?</p><p>There exists two types of dependencies: hard dependency and soft dependency. A hard dependency occurs when module A calls a specific implementation that lives in module B. Module A then needs to import module B in order to use it.</p><pre>import Network</pre><pre>class ProductViewController: UIViewController {<br>  let network: Network</pre><pre>  override func viewDidLoad() {<br>    super.viewDidLoad()</pre><pre>    network.fetchProduct { result in<br>    }<br>  }<br>}</pre><p>A soft dependency is one where module A doesn’t know module B. Module A declares what it needs but is not specific in who is going to provide it. As a consequence, module A doesn’t import module B nor any other module.</p><pre>protocol Networking {<br>  func fetchProduct(completion: <a href="http://twitter.com/escaping">@escaping</a> (Result&lt;[String], Error&gt;) -&gt; Void)<br>}</pre><pre>class ProductViewController: UIViewController {<br>  let networkService: Networking</pre><pre>  override func viewDidLoad() {<br>    super.viewDidLoad()</pre><pre>    networkService.fetchProduct { result in<br>    }<br>  }<br>}</pre><p>In this scenario, it is the Product caller’s responsibility to provide it with the Networking dependency. Product is not concerned with the details of the fetchProduct method. It might be a REST request or a GQL one. This is an implementation detail hidden to the Product module.</p><h3>A Flat Dependency Graph</h3><p>In Swift, a module can declare a soft dependency by using a protocol rather than a class, a struct or enum. Protocol programming enables modules to talk to one another without committing. If module A calls module B, it becomes easy to replace module B with an updated module C. Just make module C implement the same API as module B.</p><p>With protocol programming, our originally fat dependency graph can be flattened into:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IPxoms6RBluYuA0wn-HKAg.png" /><figcaption>A modular codebase</figcaption></figure><p>A second application can reuse the product features and provide its own implementation of core services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i_ruLCJdiU6gv-dloe0SyA.png" /></figure><h3>Why Is Modularity Important?</h3><p>As we scale our product pipeline and engineering team, modularity enables us to:</p><ul><li>Build multiple products that share some code</li><li>Work efficiently together on the same codebase</li></ul><p>Small reusable and independent components will have a positive impact on our app performance by reducing the app size. We will be able to A/B test features, replace components without breaking a tree of dependencies, and unit test our core services.</p><p>Second, but just as important, is that small and independent modules will have a positive impact on our work as engineers. Do you know the best way to reduce build time? By building less! Small independent components directly translate into faster build time. Independent components are also easier to reason about. Greater readability and easier maintenance will outweigh the additional boilerplate.</p><p>We will be able to deliver more features faster.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d294f45594f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>