Hacking with Swift News, tips, and tutorials from Hacking with Swift https://www.hackingwithswift.com/articles/rss (c)2020 Paul Hudson https://www.hackingwithswift.com/favicon-96x96.png Hacking with Swift https://www.hackingwithswift.com/articles/rss How to Become an iOS Developer in 2021 https://www.hackingwithswift.com/articles/230/how-to-become-an-ios-developer https://www.hackingwithswift.com/articles/230/how-to-become-an-ios-developer The skills, resources, and networking that will get you to your goal Wed, 31 Mar 2021 20:37:46 +0000 In this article I want to walk you through what I think it takes to become an iOS developer in 2021. I’m aiming this squarely at two groups of people: absolute beginners to Swift – folks who have never built anything for iOS before – and also so-called false beginners, who are folks who might have tried to learn Swift before but never really reached their goal of getting a full-time job.

Regardless of what level you are right now, the goal here is the same: getting you in the right position to apply for a junior iOS developer role at a company.

We’re going to look at the skills you should be learning, the courses you can take, how to get connected to the community, common mistakes folks make, and more, and best of all everything I’ll talk about will be completely free so you won’t need a penny to follow along.

Seriously, too many people think spending a ton of money is the fast track to reaching their dream job, when really the most important things are determination and willpower. And here’s a hint for you: if you’re already thinking you want to skip ahead in this article, maybe you should consider working on your willpower!

There are 7 sections to this article:

  1. Core skills you absolutely need to know to get a job.
  2. Extension skills that are nice to have – the ones that will set you apart from the pack, if you have the time to learn them
  3. Common mistakes people make when they are trying to learn.
  4. Resources you can use to learn Swift that are completely free.
  5. How you can connect to the iOS development community.
  6. An idea of how long it will take to reach your goal.
  7. Preparing to apply for your first job
  • This article is also available as a video if you prefer. The content is the same, so use whichever is easiest for you.

What are the core skills you need to be an iOS developer?

What is the absolute minimum set of skills you need to get a job in iOS development? I think there are five things in total:

  1. S...
]]>
24 Quick Xcode Tips https://www.hackingwithswift.com/articles/229/24-quick-xcode-tips https://www.hackingwithswift.com/articles/229/24-quick-xcode-tips Write code faster, build better tests, and navigate around projects more efficiently. Fri, 12 Feb 2021 22:02:25 +0000 Xcode is a huge program, and if you can learn one or two new tips that help you work more effectively you can save hours of time every week.

In this article I want to walk you through 24 quick Xcode tips that help you write code faster, build better tests, and navigate around large projects more efficiently – there’s something for everyone!

  • This article is also available as a video if you prefer. The content is the same, so use whichever is easiest for you.

1. Generating class initializers

Swift can synthesize memberwise initializers for structs, but not for classes. Fortunately, Xcode can generate the code for us: select your class name, then go to the Editor menu and choose Refactor > Generate Memberwise Initializer. Done!

2. Environment overrides

When building software, it’s important to make sure your app works for everyone. One great way to do that is with Xcode’s environment overrides panel, which lets you adjust Dynamic Type size, accessibility options, and appearance all in one place.

3. Selecting blocks of code

You probably already know that you can click after any open { or close } brace to have Xcode highlight its matching brace, but another useful tip is that you can double click a brace to select the entire block of code it contains.

4. Checking our spelling

Xcode can check if your spelling is correct, and it even understands that camelCaseNames should be treated as separate words. Go to the Edit menu and choose Format > Spelling and Grammar > Check Spelling While Typing, and Xcode will check your code for typos.

5. Fixing all issues

If you have a Swift file with many errors, you can use Xcode’s fix-its to try to correct them one by one. But a faster way is to go to the Editor menu and choose Fix All Issues to apply fix-its all at once. (Make sure and double-check which changes were made!)

6. Deleting search results

When you search using Xcode’s find navigator, you can click to v...

]]>
What’s new in Swift 5.4? https://www.hackingwithswift.com/articles/228/whats-new-in-swift-5-4 https://www.hackingwithswift.com/articles/228/whats-new-in-swift-5-4 Multiple variadic parameters, improved implicit member syntax, result builders, and more! Tue, 02 Feb 2021 15:35:14 +0000 Swift 5.4 brings with it some huge compilation improvements, including better code completion in expressions with errors and big speed ups for incremental compilation. However, it also adds some important new features and refinements, so let’s dig into them here…

Improved implicit member syntax

SE-0287 improves Swift’s ability to use implicit member expressions, so rather than just having support for exactly one single static member you can make chains of them.

Swift has always had the ability to use implicit member syntax for simple expressions, for example if you wanted to color some text in SwiftUI you could use .red rather than Color.red:

struct ContentView1: View {
    var body: some View {
        Text("You're not my supervisor!")
            .foregroundColor(.red)
    }
}

Prior to Swift 5.4 this did not work with more complex expressions. For example, if you wanted your red color to be slightly transparent you would need to write this:

struct ContentView2: View {
    var body: some View {
        Text("You're not my supervisor!")
            .foregroundColor(Color.red.opacity(0.5))
    }
}

From Swift 5.4 onwards the compiler is able to understand multiple chained members, meaning that the Color type can be inferred:

struct ContentView3: View {
    var body: some View {
        Text("You're not my supervisor!")
            .foregroundColor(.red.opacity(0.5))
    }
}

Multiple variadic parameters in functions

SE-0284 introduced the ability to have functions, subscripts, and initializers use multiple variadic paramete...

]]>
Which SwiftUI property wrapper to choose in any situation https://www.hackingwithswift.com/articles/227/which-swiftui-property-wrapper https://www.hackingwithswift.com/articles/227/which-swiftui-property-wrapper Decide which property wrapper is the right choice for your needs. Wed, 27 Jan 2021 17:51:44 +0000 SwiftUI uses property wrappers to understand how we create and store data in our views, but if you need helping choosing which property wrapper is right for you I've made a tool to help. To find the right property wrapper for you, answer the questions below 🚀

1. Are you storing a struct, an enum, or a class?

I want to store a struct or an enum.

2. Will the value change over time?

Yes, it will change as my app runs.

3. Are you creating the value in your view, or will it be passed in from somewhere else?

I'll be creating the value locally in my view.

4. Is the property being used to track an active user gesture?

Yes, this is for a gesture.

You should use the @GestureState property wrapper, like this:

@GestureState private var dragAmount = CGSize.zero

Although you could also use @State for this purpose, @GestureState is significantly more efficient.

You can learn more about the @GestureState property wrapper here: What is the @GestureState property wrapper?

No, this is just a regular value.

You should use the @State property wrapper, like this:

@State private var username = "Taylor"

Technically there's nothing stopping y...]]> 5 Steps to Better SwiftUI Views https://www.hackingwithswift.com/articles/226/5-steps-to-better-swiftui-views https://www.hackingwithswift.com/articles/226/5-steps-to-better-swiftui-views Learn to make your SwiftUI views smaller, simpler, and more reusable. Wed, 27 Jan 2021 11:57:00 +0000 As SwiftUI projects grow more complex, it’s common to find your views get larger and larger as you cram more functionality into them. In this article I’ll walk you through the underlying problem, and demonstrate five steps you can take to make your views simpler, smaller, and smarter – all demonstrated using a real SwiftUI project, so you have actual code to follow along.

You can get the project source code, including both before and after versions, from this link.

  • This article is also available as a video if you prefer. The content is the same, so use whichever is easiest for you.

Still here? Okay, let’s get to it…

What’s the problem?

This is a really challenging time for SwiftUI because at this point we’re kind of past the novelty of SwiftUI itself – we’re used to the idea that we can build user interfaces really fast, we’re used to the idea of tracking state effectively, and we’re used to the idea that we can write layouts that work great on all of Apple’s platforms.

However, although many people are already comfortable with the way SwiftUI works, we are still quite a long way from agreeing best practices. For example, if you have several different ways of solving a problem, we’re still in the process of figuring out which one makes most sense for a given situation.

So, we’re at this strange position where many people are learning SwiftUI and writing lots of it, but the apps we build are growing in size and complexity and as a result are starting to suffer from very similar problems that we faced in UIKit in the early years.

Previously it was common to tackle the long-standing UIKit curse of Massive View Controllers, but in SwiftUI we don’t have view controllers at all – in fact, many would say that the lack of view controllers is the best feature of SwiftUI!

However, we do make a lot of view structs where we cram a lot of functionality. This created a wholl...

]]> Year in review: 2020 https://www.hackingwithswift.com/articles/225/year-in-review-2020 https://www.hackingwithswift.com/articles/225/year-in-review-2020 A brief look back on another year of Hacking with Swift Fri, 01 Jan 2021 22:20:32 +0000 What the heck just happened? 😂

Many people – myself included – had big plans for the year. I wanted to work more on my open source software (Unwrap, Control Room, Sitrep, and more), I wanted to update a whole collection of books to be more focused on SwiftUI, I wanted to write new, shorter books to go alongside SwiftUI By Example, and more.

And then COVID-19 happened.

This meant my kids were home from school for many months, it meant I spent way too much time scrolling through depressing news rather than working, and honestly it meant a lot of the time I struggled to find the willpower to think about – never mind write about – anything.

So, this year turned out quite different from what I had anticipated, but there were still a few highlights…

Advance warning: This is a hugely self-indulgent article talking about what went well – and not so well – in my Hacking with Swift work this year. Hopefully you don’t find it too dull, but I only do it once a year!

Updates, rewrites, and more

This year I published a new, free book called Understanding Swift, which was entirely unlike all the other books I’ve written previously because I wanted each chapter to answer one question. The goal here was to focus firmly on why Swift is the way it is, because there are lots of code samples out there for any given topic but not a great deal of explanation of why features and functionality exists in the first place.

So, you’ll find chapters such as “What the heck are closures and why does Swift love them so much?”, “When should you write throwing functions?”, “How do Swift’s memberwise initializers work?”, and more – all questions that I was commonly asked by folks who were completing the 100 Days of SwiftUI.

I also published [Swift on Sundays Volume 1](h...

]]>
8 Common SwiftUI Mistakes - and how to fix them https://www.hackingwithswift.com/articles/224/common-swiftui-mistakes-and-how-to-fix-them https://www.hackingwithswift.com/articles/224/common-swiftui-mistakes-and-how-to-fix-them Write less code and get more done Fri, 09 Oct 2020 13:29:31 +0000 SwiftUI is a big and complex framework, and although it’s great fun to work with there’s also a lot of scope for making mistakes. In this article I’m going to walk through eight common mistakes SwiftUI learners make, and how to fix them.

Some of these mistakes are simple misunderstandings, and with SwiftUI being so big these are easy to make. Others are about getting a deeper understanding of how SwiftUI works, and still others are more a sign of legacy thinking – sometimes you spend a lot of time writing views and modifiers and don’t take the time to simplify the end result.

Anyway, I’m not going to keep you in suspense about what these eight mistakes are, so here’s a brief summary before we dive into them:

  1. Adding views and modifiers where they aren’t needed
  2. Using @ObservedObject when they mean @StateObject
  3. Putting modifiers in the wrong order
  4. Attaching property observers to property wrappers
  5. Stroking shapes when they mean to stroke the border
  6. Using alerts and sheets with optionals
  7. Trying to get “behind” their SwiftUI view
  8. Creating dynamic views using invalid ranges

If you prefer to watch a video instead, I've got you covered!

Still here? Okay, let’s get to it…

1. Adding views and modifiers where they aren’t needed

Let’s start with one of the most common, which is writing more SwiftUI code than you actually need. This is common partly because it’s often we write a lot of code as we’re figuring out a problem, but it’s easy to forget to clean up that code afterwards. It’s also sometimes a matter of slipping back into old habits, particular if you’ve come from UIKit or other user interface frameworks.

As a starting example, how might you fill the screen with a red rectangle? You might write this:

Rectangle()
    .fill(Color.red)

And honestly that works great – it gets the exact result you want. But half that code isn’t needed, because you can instead just say...

]]>
Hacking with Swift Live 2020 raises $38,000 for charity https://www.hackingwithswift.com/articles/223/hacking-with-swift-live-2020-raises-38000-for-charity https://www.hackingwithswift.com/articles/223/hacking-with-swift-live-2020-raises-38000-for-charity Make apps, make friends, make a difference Thu, 16 Jul 2020 16:55:11 +0000 Hacking with Swift Live 2020 just finished, delivering four days of packed tutorials and workshops walking through all the major new features for SwiftUI, UIKit, and more. Even better, because all our profits go to charity we were able to donate $38,000 to an incredible cause – folks made friends, learned lots of new Swift skills, and made a difference to the wider world.

I’m hugely grateful to everyone who bought a ticket to the event, and the feedback so far has been really positive. Over four days we really got to know each other even though it was a remote event, and it’s amazing to see folks leaving with new friends. Thank you so much for coming!

I’m also really grateful to our two sponsors: Bitrise came back for a second year as platinum sponsor, and several members of their team came along to the event. Instabug also joined as silver sponsor for the first time, and it was great to have them on board.

Both those companies could easily have used that money to show adverts on Google, Twitter, or other services, but they instead chose to make a significant charitable donation in order to support a community conference like this one. Thank you so much!

Just like last year, we donated all our profits to Special Effect, who do remarkable work to help bring joy, laughter, creativity, and confidence into the lives of disabled children and adults. With the pandemic still raging many charities are having a hard time raising funds, so we’re glad to play a part in helping continue their awesome work.

This event wouldn’t have been possible without the help of the organizing team: Chris Rivers, Daniel Leivers, Ian Lockett, and my wife Ildiko. We all work really hard to bring this event to you and don’t get paid a penny – we send everything we can to charity.

So, ...

]]>
WWDC20: Wrap up and recommended talks https://www.hackingwithswift.com/articles/222/wwdc20-wrap-up-and-recommended-talks https://www.hackingwithswift.com/articles/222/wwdc20-wrap-up-and-recommended-talks Packed Sessions, hands-on labs, and a look forward to next year Sun, 28 Jun 2020 14:30:24 +0000 WWDC20 was a packed event as always, with some incredible steps forward for macOS, SwiftUI, and more. Was it better than the regular event? Yes. Will it go back to how it was before? That’s harder to say.

In this article I’ll go over how I thought the sessions and labs compared to an in-person WWDC event, pick out my favorite community events, cover the talks I enjoyed the most and the talks I think you should watch if you're short on time, and also look forward to what might happen next year.

Full stream ahead from Apple Park

Even from the very first minutes of the keynote it was clear these WWDC presentations were going to be like no other. First, the video quality was – predictably – stunning, and on a 4K TV it felt like you were in the room with Tim, Craig, and the others. Second, Apple Park was a key cast member: they jumped around from the Steve Jobs Theater to the health studio, to the “secret underwater lab” and it felt amazing.

A long camera shot that seems to take us from Steve Jobs Theater to a secret underwater lab.

None of this would have been possible in a regular WWDC20. You’d never get that close to the various senior Apple folks, and although I have a fondness for the McEnery Center it’s never going to come close to Apple Park.

Throughout the week, the video production naturally became more practical and less glitzy as the topics became hands on, but the actual production quality was still very high. I can’t imagine how much time it must have taken to make each video, because each one was clearly stitched together from many takes.

The remote format allows several things:

  1. Each video was only as long as it needed to be. Most previous WWDC talks aimed to be about 40 minutes so they could keep a fixed structure at the venue, but at WWDC20 I watched many that were under 15 minutes. These weren’t just “easy talks” either – [Advances in diffable data sources](https://developer.apple.com/videos/play/wwdc20...
]]>
What’s new in SwiftUI for iOS 14 https://www.hackingwithswift.com/articles/221/whats-new-in-swiftui-for-ios-14 https://www.hackingwithswift.com/articles/221/whats-new-in-swiftui-for-ios-14 Text views, grids, color pickers, and more! Mon, 22 Jun 2020 22:27:42 +0000 SwiftUI was inevitably going to see big changes this year, and I’m really excited to experiment with them all – text views, color pickers, progress views, and even limited support for grids have all landed. Alongside massive improvements to Swift itself (see What's new in Swift 5.3 for more on that), this is a huge leap forward for SwiftUI – we can now make a much wider range of apps.

Please keep in mind that these changes are very new – I'm pushing it as hard as I can, experimenting, refining, sharing, and learning all at the same time. If you have any feedback, please tweet me @twostraws.

You can watch the video below, or scroll down for links to articles.

The big stuff

More welcome improvements

]]>
Introducing Hacking with Swift+ https://www.hackingwithswift.com/articles/220/introducing-hacking-with-swift-plus https://www.hackingwithswift.com/articles/220/introducing-hacking-with-swift-plus A new subscription service to help deepen your knowledge of Swift Thu, 04 Jun 2020 14:53:44 +0000 Every day 40,000 people visit Hacking with Swift to learn Swift, solve a problem with their code, or get advice for their iOS career, reading 5,000,000 pages over the course of an average month.

Serving the community I’m part of has always been important to me, which is why I try to give away as much free content as I can – free videos, free tutorials, free example code, free interview tests, and more, all to help people achieve their goals with Swift.

I want to continue this work as long as I can, and help as many people as I can, which means I need a solution to financially support my work for the long term.

Well, that solution is finally here: it’s called Hacking with Swift+, and it’s my first ever subscription option for Swift content. The first articles are already published, with each one having 4K Ultra HD video attached, and there are many more to come.

All articles posted there will come with video, so folks can read or watch depending on what they prefer, and three courses already have their first videos published: Advanced Swift, Custom SwiftUI Components, and High-Performance Apps – many more will come soon.

I still plan to release as much free content as I can, but I’m hoping this subscription option will create a firm foundation for Hacking with Swift for years to come. I’m working on so many exciting things right now, and I can’t wait to share them!

To find out more or subscribe to Hacking with Swift+, click here.

]]>
New book: Understanding Swift https://www.hackingwithswift.com/articles/219/new-book-understanding-swift https://www.hackingwithswift.com/articles/219/new-book-understanding-swift Answers common beginner questions and more Fri, 29 May 2020 09:03:27 +0000 I've just published a new book called Understanding Swift, complementing my 100 Days of Swift and 100 Days of SwiftUI courses by answering the most common questions folks have. Everything I have released for the last 20 months has been available free, and this new book is also free right here: Understanding Swift

This book is different from my others that show you how to build stuff with Swift, because this one is trying to answer many of the "why" questions learners have – why are tuples different from structs? Why do copies of a class share data? Why would you want a failable initializer?

You might look at some of the questions and think they are easy, but every one of them was drawn from questions I've been asked by folks completing the 100 Days of Swift/SwiftUI – this book tries to bring them into one place so folks hit fewer problems and feel more encouraged.

If you'd prefer a download edition of Understanding Swift, you can buy it here. But I want to get this to everyone who needs it, so I'm retroactively adding it to my Swift Power Pack

Last December I launched a Patreon so folks could help support my work, and it's helped keep things afloat. So, Understanding Swift is dedicated to everyone who supports me on Patreon – thanks, folks!

You can find out more about my Patreon here.

PS: If you're following the 100 Days of Swift/SwiftUI, I have added links to Understanding Swift directly to each day so you have optional extra reading if you want it.

]]>
What’s new in Swift 5.3? https://www.hackingwithswift.com/articles/218/whats-new-in-swift-5-3 https://www.hackingwithswift.com/articles/218/whats-new-in-swift-5-3 Multiple trailing closures, massive package manager improvements, and more. Fri, 15 May 2020 13:10:35 +0000 Swift 5.3 brings with it another raft of improvements for Swift, including some powerful new features such as multi-pattern catch clauses and multiple trailing closures, plus some important changes for Swift Package Manager.

In this article I’m going to walk through each of the major changes, while providing hands-on code samples so you can try them yourself. I encourage you to follow the links through to the Swift Evolution proposals for more information, and if you missed my earlier what's new in Swift 5.2 article then check that out too.

Multi-pattern catch clauses

SE-0276 introduced the ability to catch multiple error cases inside a single catch block, which allows us to remove some duplication in our error handling.

For example, we might have some code that defines two enum cases for an error:

enum TemperatureError: Error {
    case tooCold, tooHot
}

When reading the temperature of something, we can either throw one of those errors, or send back “OK”:

func getReactorTemperature() -> Int {
    90
}

func checkReactorOperational() throws -> String {
    let temp = getReactorTemperature()

    if temp < 10 {
        throw TemperatureError.tooCold
    } else if temp > 90 {
        throw TemperatureError.tooHot
    } else {
        return "OK"
    }
}

When it comes to catching errors thrown there, SE-0276 lets us handle both tooHot and tooCold in the same way by separating them with a comma:

do {
    let result = try checkReactorOperational()
    print("Result: \(result)")
} catch TemperatureError.tooHot, TemperatureError.tooCold {
    print("Shut down the reactor!")
} catch {
    print("An unknown error occurred.")
}

You can handle as many error cases as you want, and you can even bind values from ...

]]>
The Complete Guide to Layout in SwiftUI https://www.hackingwithswift.com/articles/217/complete-guide-to-layout-in-swiftui https://www.hackingwithswift.com/articles/217/complete-guide-to-layout-in-swiftui HStack, VStack, ZStack, Spacer, GeometryReader, and more Fri, 01 May 2020 15:48:40 +0000 SwiftUI gives us a huge range of tools to control layouts, from the core tools of HStack, VStack, and ZStack, through to more advanced views such as GeometryReader and Group, plus modifiers such as layoutPriority().

I’ve published a lot of articles and videos about those and more, but this article brings them all together in one place for easy reference.

If I missed something off this list, let me know on Twitter.

How layout works in SwiftUI

Before we get into the details of specific layout techniques, you should start with my video “How layout works in SwiftUI” – it outlines exactly how things fit together to give us lots of power and flexibility:

If you’d rather read the article instead, you can find it here: How layout works in SwiftUI.

The basics of layout stacks, frames, and ForEach

A handful of view types lie at the center of all our SwiftUI layouts, and if you understand them then you’re already half way to making great UI.

Start with these four to get a firm understanding of the basics:

Of course, you’re not restricted to working in one dimension – you can create your own stacks if you want....

]]>
The Complete Guide to NavigationView in SwiftUI https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui Programmatic navigation, customization, and more Wed, 29 Apr 2020 16:11:35 +0000 NavigationView is one of the most important components of a SwiftUI app, allowing us to push and pop screens with ease, presenting information in a clear, hierarchical way for users. In this article I want to demonstrate the full range of ways you can use NavigationView in your apps, including simple things like setting a title and adding buttons, but also programmatic navigation, creating split views, and even handling other Apple platforms such as macOS and watchOS.

 

 

Getting a basic NavigationView with a title

To get started with NavigationView you should wrap one around whatever you’re trying to display, like this:

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!")
        }
    }
}

For simpler layouts navigation views should be the top-level thing in your view, but if you’re using them inside a TabView then the navigation view should be inside the tab view.

When learning SwiftUI, one thing that folks find confusing is how we attach titles to a navigation view:

NavigationView {
    Text("Hello, World!")
        .navigationBarTitle("Navigation")
}

Notice how the navigationBarTitle() modifier belongs to the text view, not to the navigation view? That’s intentional, and is the correct way to add a title here.

You see, navigation views let us display new screens of content by sliding them in from the right edge. Each screen can have its own title, and it’s the job of SwiftUI to make sure that title is shown in the navigation view at all times – you’ll see the old title animate away, while the new title animates in.

Now think about this: if we had attached the title directly to the navigation view, w...

]]>
Spot the Swifty https://www.hackingwithswift.com/articles/215/spot-the-swifty https://www.hackingwithswift.com/articles/215/spot-the-swifty Explore a cartoon world without social distancing. Wed, 01 Apr 2020 08:00:04 +0000 It’s April Fools day, and with the world as it is right now I figured folks in our community could use a little distraction – even if only for 10 minutes.

So, I’ve spent the last six weeks working with the talented illustrator Gus Morais to produce a massive picture that’s packed with fun details for you to discover. Plus, there are 30 well-known folks from the Swift community scattered through the picture – can you find them all?

The original picture itself is huge, weighing in at 5374x3508, but once you factor in a key helping you identify everyone you should be looking for, the finished result is an eye-watering 12850x5700 pixels in size.

So, you should open one of the pictures below in a new window, then zoom to see all the details – along with the 30 people there’s lots more to discover! Some people are harder to discover than others, and honestly I think you’ll struggle to find all 30. Prove me wrong, if you dare!

Regardless, I hope this picture provides you with a little fun today. Thanks for being a part of our incredible community ❤️

Remember: these folks can stand close together because they are cartoons, but you’re not – stay safe, and practice social distancing!

I’ve provided two versions of the picture: the original, and the original plus a key. If you think you really know the Swift community then try the first one, but if you need a little help check out the second…

Spot the Swifty

Make sure and click the pictures to see the full-sized version!

Spot the Swifty with Key

]]>
8 essential apps for iOS developers https://www.hackingwithswift.com/articles/214/essential-apps-for-ios-developers https://www.hackingwithswift.com/articles/214/essential-apps-for-ios-developers Make iOS and macOS do more work for you Mon, 30 Mar 2020 15:07:21 +0000 App developers use apps just as much as anyone else, but as well as things like Overcast, PCalc, and Reddit, we also use apps specifically designed to make development easier – apps aimed at developers.

In this article I want to pick out a handful I can personally recommend. None of these were provided free; I bought them all and can recommend them all from experience. If you make a development-focused app that I missed off, send me a tweet with a link and I’ll check it out!

Adaptivity A

Apple gives us many sizes, colors, icons, and more to work with, and sometimes it can be hard to remember which is which – should you use readable content margins here, to layout margins? Which Dynamic Type font is the correct choice for a particular layout? How about when all those are used inside a local notification UI?

Adaptivity by Geoff Hackworth is an app that aims to answer all those questions and more, allowing you to see visually how various layout guides interact, how all the Dynamic Type font families look (even with custom font support), how the safe area adjusts as navigation views and toolbars are shown and hidden, and practically everything else you could want from your layouts.

The app itself is remarkably simple: you see various common layouts such as plain view controllers, table view controllers, split view controllers, and more, each overlaid with various colored lines showing you the available space using differing measurement guides. But the app then goes on to show you more complex scenarios: what happens if you show this view controller in a context menu, or as part of a local notification? It even has a home screen quick action showing you how the layout adapts inside an action extension!

It seems ridiculous that this app costs just $3.99, because if you use it just once to figure out a layout problem it has earned its money back immediately. Even better, it’s a universal purchase that works acr...

]]>
How to build neumorphic designs with SwiftUI https://www.hackingwithswift.com/articles/213/how-to-build-neumorphic-designs-with-swiftui https://www.hackingwithswift.com/articles/213/how-to-build-neumorphic-designs-with-swiftui Try out a new design trend and learn something new Wed, 26 Feb 2020 14:35:41 +0000 Neumorphic design is easily the most interesting design trend of recent months, although to be fair Apple did use it as their design motif way back in WWDC18. In this article we’re going to look at how you can build neumorphic designs using SwiftUI, why you might want to, and – most importantly – how we can modify those designs to be more accessible.

Important: Neumorphism – sometimes called neomorphism – has serious implications for accessibility, so although it’s tempting to read the first part of this article then skip the rest, I encourage you to stick around and learn the downsides as well as the upsides so you get the full picture.

 

 

The basics of neumorphism

Before we get into the code, I just want to briefly touch on the two core principles of this design trend, because it will be relevant as we progress:

  1. It uses highlights and shadows to define the shapes of objects on the screen.
  2. Contrast is generally reduced; full white or black aren’t used, which is what allows the highlights and shadows to stand out.

The end result is an “extruded plastic” look – a user interface design that certainly looks fresh and interesting without being harsh on your eyes. I can’t repeat enough that reducing contrast and relying on shadows for shapes has serious impacts on accessibility, and we’ll be coming back to this later.

However, I still think it’s worth taking the time to explore neumorphism in SwiftUI – even if you don’t use it in your own apps, it’s a bit like a coding kata that will help hone your skills.

OK, enough waffle – let’s look at some code.

Building a neumorphic card

The simplest starting point is to build a neumorphic card: a rounded rectangle that will contain some inf...

]]>
What’s new in Swift 5.2 https://www.hackingwithswift.com/articles/212/whats-new-in-swift-5-2 https://www.hackingwithswift.com/articles/212/whats-new-in-swift-5-2 Key path expressions as functions, callAsFunction, and more Wed, 05 Feb 2020 23:29:22 +0000 Swift 5.2 arrived with Xcode 11.4, and includes a handful of language changes alongside reductions in code size and memory usage, plus a new diagnostic architecture that will help you understand and resolve errors faster.

In this article I'm going to walk through what's changed with some hands-on examples so you can see for yourself how things have evolved. I encourage you to follow the links through to the Swift Evolution proposals for more information, and if you missed my earlier what's new in Swift 5.1 article then check that out too.

Key Path Expressions as Functions

SE-0249 introduced a marvelous shortcut that allows us to use keypaths in a handful of specific circumstances.

The Evolution proposal describes this as being able to use “\Root.value wherever functions of (Root) -> Value are allowed”, but what it means is that if previously you sent a Car into a method and got back its license plate, you can now use Car.licensePlate instead.

This is best understood as an example, so here’s a User type that defines four properties:

struct User {
    let name: String
    let age: Int
    let bestFriend: String?

    var canVote: Bool {
        age >= 18
    }
}

We could create some instance of that struct and put them into an array, like this:

let eric = User(name: "Eric Effiong", age: 18, bestFriend: "Otis Milburn")
let maeve = User(name: "Maeve Wiley", age: 19, bestFriend: nil)
let otis = User(name: "Otis Milburn", age: 17, bestFriend: "Eric Effiong")
let users = [eric, maeve, otis]

Now for the important part: if you want to get an array of all the users names, you can do so by using a key path like this:

let userNames = users....
]]>
Apple announces SwiftCrypto, an open-source implementation of CryptoKit https://www.hackingwithswift.com/articles/211/apple-announces-swiftcrypto-an-open-source-implementation-of-cryptokit https://www.hackingwithswift.com/articles/211/apple-announces-swiftcrypto-an-open-source-implementation-of-cryptokit Get Swifty cryptography everywhere Mon, 03 Feb 2020 17:35:39 +0000 Update: Cory Benfield from Apple has confirmed that on Apple's platforms SwiftCrypto is limited to those that support CryptoKit, and can't act as a polyfill for older releases such as iOS 11/iOS 12 and macOS 10.14. However, there's hope that might change – Cory added "we’d be happy to discuss that use-case with the community." In theory that could mean we get SwiftCrypto support for iOS 12 and earlier, which would be awesome!

Apple today released SwiftCrypto, an open-source implementation of the CryptoKit framework that shipped in iOS 13 and macOS Catalina, allowing us to use the same APIs for encryption and hashing on Linux.

The release of SwiftCrypto is a big step forward for server-side Swift, because although we’ve had open-source Swift cryptography libraries in the past this is the first one officially supported by Apple. Even better, Apple states that the “vast majority of the SwiftCrypto code is intended to remain in lockstep with the current version of Apple CryptoKit,” which means it’s easy for developers to share code between Apple’s own platforms and Linux.

What’s particularly awesome about SwiftCrypto is that if you use it on Apple platforms it effectively becomes transparent – it just passes your calls directly on to CryptoKit. This means you can write your code once using import Crypto, then share it everywhere. As Apple describes it, this means SwiftCrypto “delegates all work to the core implementation of CryptoKit, as though SwiftCrypto was not even there.”

The only exception here is that SwiftCrypto doesn’t provide support for using Apple’s Secure Enclave hardware, which is incorporated into devices such as iPhones, Apple Watch, and modern Macs. As the Secure Enclave is only available on Apple hardware, this omission is unlikely to prove problematic.

Try it yourself

SwiftCrypto is available today, so why not give it a t...

]]>