Quais os erros do Palmeiras em 2017?

Para espanto de muitos e confirmação de poucos, o ano do Palmeiras está praticamente perdido, sem a conquista de qualquer campeonato, justamente em um ano no qual a torcida esperava a conquista senão…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Introducing AppFolder

Folder of your iOS app — friendly and strongly-typed

Each one of your iOS apps has an associated container with it, where you, as a developer, can store basically anything you need. This is often referred to as a “sandbox”. Inside this sandbox, you can place files that serve different purposes — it could be user-visible documents, or database files, or caches, or some kind of metadata — anything. Basically, every persistence technique on iOS uses your app’s container to store info there. Thus, understanding how your app’s folder looks, how it’s structured and what files your put there is crucial for every iOS developer. But, here lies the problem:

So, if you need to get, say, a “Documents” folder URL for your app, you need to write this code:

Which may not look that bad… if you know exactly what that code is doing.

But for someone who’s not accustomed to this API, it may as well look scary. Because when you’re trying to use autocompletion to discover your options, you get this:

Giant list of FileManager.SearchPathDirectory options, most of which are useless on iOS

In this situation, anyone will wonder “what the heck is going on here?” .adminApplicationDirectory? .applicationScriptsDirectory? .moviesDirectory, .trashDirectory?! There’s a lot of stuff, and most of it makes absolutely no sense for an iOS app.

And then there’s Search Path Domain Mask:

…umm, yeah, sure, I know exactly which one to choose.

This API can be justified for macOS, but on iOS it’s plain misleading:

Well, imagine if we could trade the code above

For something like this

Also harnessing the power of Xcode autocompletion:

Wouldn’t it be nice?

Well, that’s exactly what AppFolder is made for:

AppFolder is a really small library that visualizes your app’s folder structure in code. You can easily access system-defined directories like this:

Everything is laid out exactly the way it is on disk. This way, disk-related operations feels much more intuitive:

AppFolder was designed to let developers describe their full folder structure inside their app’s container, including the directories they create themselves:

And just like that, the newly created folder can now be treated like any other

The name for this directory will be generated automatically, so the subpath will look like “Documents/Photos/”

AppFolder was built using the Swift feature that is often overlooked nowadays — inheritance. As you might noticed, every directory is a strongly-typed sublcass of Directory. Swift’s nested types and extensions allow us to structure our code in the most natural way. Here’s, for example, how Library is implemented:

And Documents implementation is, literally, a single line of code

I tried many different approaches, including protocols and phantom types — but none of them allowed this level of customization and convenience. For this task, subclassing was just the right choice.

Which draws an important conclusion: no matter what you feel about certain techniques or patterns, you shouldn’t be afraid to use them when they fit your needs. Subclassing is just another tool that Swift gives us — and embracing it or renouncing it is a matter of our choice.

AppFolder is a piece of code that changed the way I think about an iOS app. I love having the whole container structure available to me from any place in my code. I hope it will be useful for you as well — let me know if it is 😉

Add a comment

Related posts:

Technology Mindfulness Project

Ever since I was little, technology has always been there, whether it be in the way of cameras, flip phones, video game consoles, or smartphones. I remember using my mom’s flip phone to take pictures…

Cashe Memories

Often I think of the time that was devoid of miseries and problems, but another line of thought gives me glimpses that are way opposite to the former. My brain lingers on and cherishies school days…

Twitter Anniversaries

It may be hard to believe that Twitter has been around for more than 11 years, but that may not come as much of a shock to our customers. The amount of time folks have spent on the platform is…