{"id":7999,"date":"2020-03-17T16:30:08","date_gmt":"2020-03-17T16:30:08","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7999"},"modified":"2020-03-17T16:31:06","modified_gmt":"2020-03-17T16:31:06","slug":"creating-a-swift-package","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-swift-package\/","title":{"rendered":"Creating a Swift Package"},"content":{"rendered":"<p>In my previous post I looked at creating a CircleImage, basically creating a reusable SwiftUI control. Now let&#8217;s look at how we might start creating packages to allow us to share such library code.<\/p>\n<p><strong>Creating a Swift Package<\/strong><\/p>\n<p>From Xcode, select File | New | Swift Package. A very basic package is created (mine&#8217;s name MyLibrary) with a Sources\/YouPackageName\/YourPackageName.swift file containing the following code<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nstruct MyLibrary {\r\n    var text = &quot;Hello, World!&quot;\r\n}\r\n<\/pre>\n<p>We&#8217;ll stick with this simple bit of code but make the struct and the text var public. Also we&#8217;ll need to add a public init method or you&#8217;ll see an error such as this<\/p>\n<p><em>initializer is inaccessible due to &#8216;internal&#8217; protection level<\/em><\/p>\n<p>So the new code looks like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic struct MyLibrary {\r\n    public var text = &quot;Hello, World!&quot;\r\n    \r\n    public init() {\r\n    }\r\n}\r\n<\/pre>\n<p>Now use Command + B to build the package.<\/p>\n<p><strong>Local Package<\/strong><\/p>\n<p>To use the package locally, i.e. not having it deployed to a remote repos or the likes.<\/p>\n<p>Within the application that you wish to use the package, simply (using Finder) select the package folder (i..e mine&#8217;s MyLibrary) and drag and drop this into your application under the top level project node in the Project Navigator.<\/p>\n<p>Now we can use the code by simply importing it, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport MyLibrary\r\n<\/pre>\n<p>and now within your code<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet text = MyLibrary().text\r\n<\/pre>\n<p><strong>Making our package more useful<\/strong><\/p>\n<p>Let&#8217;s now move the code for our CircleImage into the package, we&#8217;ll leave the package name and therefore the component will be renamed, as MyLibrary<\/p>\n<p>Here&#8217;s the code<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport SwiftUI\r\n\r\n@available(iOS 13, *)\r\npublic struct MyLibrary: View {\r\n    var image: Image\r\n    var borderColor: Color\r\n    var shadowRadius: CGFloat\r\n\r\n    public var body: some View {\r\n        image\r\n            .clipShape(Circle())\r\n            .overlay(Circle().stroke(borderColor, lineWidth: 4))\r\n            .shadow(radius: shadowRadius)\r\n    }\r\n    \r\n    public init(image: Image,\r\n                borderColor: Color = .white,\r\n                shadowRadius: CGFloat = 10) {\r\n        self.image = image\r\n        self.borderColor = borderColor\r\n        self.shadowRadius = shadowRadius\r\n    }\r\n}\r\n<\/pre>\n<p>In the above we&#8217;ve removed the public keyword on the fields and instead relied on the init method to set up any defaults. We also need to mark the code with the @available attribute to state what versions of OSX or iOS is supported, for example Image is available in 13.0 of iOS. <\/p>\n<p>Within the package file Package.swift we also add  the platforms value, here&#8217;s a snippet of my code, showing where to expect the platforms value<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n...\r\n    name: &quot;MyLibrary&quot;,\r\n    platforms: &#x5B;\r\n        .macOS(.v10_15),\r\n        .iOS(.v13),\r\n    ],\r\n    products: &#x5B;\r\n...\r\n<\/pre>\n<p>and in our application we now simply use something like the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nMyLibrary(image: landmark.image)\r\n   .offset(x: 0, y: -130)\r\n   .padding(.bottom, -130)\r\n<\/pre>\n<p>We&#8217;ll look at using GitHub or the likes to host our package in a subsequent post, but this gives an idea on how to get something up and running.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post I looked at creating a CircleImage, basically creating a reusable SwiftUI control. Now let&#8217;s look at how we might start creating packages to allow us to share such library code. Creating a Swift Package From Xcode, select File | New | Swift Package. A very basic package is created (mine&#8217;s name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286,288],"tags":[],"class_list":["post-7999","post","type-post","status-publish","format-standard","hentry","category-swift","category-xcode"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7999"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7999\/revisions"}],"predecessor-version":[{"id":8006,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7999\/revisions\/8006"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}