Learn to arrange your codebase. If you’re battling Xcode mission construction, recordsdata, naming conventions, learn this.
Apple has a lot frameworks and APIs that I don’t even know lots of them. We’re additionally residing within the age of software extensions. If you’re attempting to create a model new goal in Xcode, you would possibly find yourself scratching your head. 🤔
That is nice for each for builders and end-users, however after creating just a few targets and platforms (your mission grows and) you would possibly ask the query:
How ought to I organise my codebase?
Don’t fear an excessive amount of about it, I may need the fitting reply for you! 😉
The issue with complicated tasks
You possibly can create apps in Xcode for all the main working techniques: iOS, macOS, tvOS, watchOS. Within the newest model of Xcode it’s also possible to add greater than 20 extension only for iOS, plus there are many app extensions accessible for macOS as nicely. Think about a fancy software with a number of extensions & targets. This case can result in inconsistent bundle identifiers and extra ad-hoc naming options. Oh, by the way in which watchOS purposes are only a particular extensions for iOS targets and don’t overlook about your checks, these are particular person targets as nicely! ⚠️
So far as I can see, if you’re attempting to assist a number of platforms you’re going to have a variety of targets inside your Xcode mission, moreover each new goal will comprise some sort of supply recordsdata and property. Ought to I point out schemes too? 😂
Even Apple eliminated it’s Lister pattern code, that demonstrated one among a hellish Xcode mission with 14 targets, 11 schemes, however the total mission contained solely 71 Swift supply recordsdata. That’s not an excessive amount of code, however you may see the difficulty right here, proper?
It’s time to discover ways to organise your mission! 💡
Xcode mission group
So my primary thought is to have an affordable naming conceptand folder construction contained in the mission. This entails targets, schemes, bundle identifiers, location of supply recordsdata and property on the disk. Let’s begin with a easy instance that incorporates a number of targets to have a greater understanding. 🤓
NOTE: If you’re utilizing the Swift Bundle Supervisor eg. for Swift backends, SPM will generate your Xcode mission recordsdata for you, so that you shoudn’t care an excessive amount of about conventions and namings in any respect. 🤷♂️
Venture identify
Are you creating a brand new software? Be at liberty to call your mission as you need. 😉
Are you going to make a framework? Lengthen your mission identify with the Equipment suffix. Individuals often favor to make use of the ProjectKit type for libraries in order that’s the proper approach to go. If in case you have a killer identify, use that as a substitute of the equipment type! 😛
Out there platforms
All the time use the next platform names:
Goal naming conference
Title your targets like:
[platform] [template name]
Don’t embody mission identify within the targets (that might be only a duplicate)
Use the extension names from the brand new goal window (eg. In the present day Extension)
Use “Software” template identify for the primary software targets
Use “Framework” as template identify for framework targets
Order your targets in a logical means (see the instance)
Scheme names
Merely use goal names for schemes too (prefix with mission identify if required).
[project] - [platform] [template name]
You possibly can prefix schemes together with your mission identify in order for you, however the generic rule is right here to make use of the very same identify as your goal. I additionally wish to separate framework schemes visually from the schems that comprise software logic, that’s why I all the time transfer them to the highest of the checklist. Nonetheless a greater method is to separate frameworks right into a standalone git repository & join them by way of a bundle supervisor. 📦
Bundle identifiers
This one is difficult due to code signing. You possibly can go along with one thing like this:
[reverse domain].[project].[platform].[template name]
Listed below are the principles:
- Begin together with your reverse area identify (com.instance)
- After the area, insert your mission identify
- Embody platform names, aside from iOS, I don’t append that one.
- Use the template identify as a suffix (like .todayextension)
- Don’t add software as a template identify
- Use .watchkitapp, .watchkitextension for legacy watchOS targets
- Don’t use greater than 4 dots (see instance beneath)!
NOTE: If you’re going to use
com.instance.mission.ios.in the present day.extension
that’s not going to work, as a result of it incorporates greater than 4 dots. So you need to merely go along withcom.instance.mission.ios.todayextension
and names like that. 😢
Anyway, simply all the time attempt to signal your app and undergo the shop. Good luck. 🍀
Venture folders
The factor is that I all the time create bodily folders on the disk. If you happen to make a gaggle in Xcode, nicely by default that’s not going to be an precise folder and all of your supply recordsdata and property will likely be situated beneath the mission’s principal listing.
I do know it’s a private desire however I don’t wish to name a large “wasteland” of recordsdata as a mission. I’ve seen many chaotic tasks with out correct file group. 🤐
It doesn’t matter what, however I all the time observe this primary sample:
- Create folders for the targets
- Create a Sources folder for the Swift supply recordsdata
- Create an Belongings folder for the whole lot else (photographs, and so forth).
Beneath the Sources I all the time make extra subfolders for particular person VIPER modules, or just for controllers, fashions, objects, and so forth.
Instance use case
Here’s a fast instance mission in Xcode that makes use of my conventions.
As you may see I adopted the sample from above. Let’s assume that my mission identify is TheSwiftDev. Here’s a fast overview of the complete setup:
Goal & scheme names (with bundle identifiers):
- iOS Software (com.tiborbodecs.theswiftdev)
- iOS Software Unit Checks (n/a)
- iOS Software UI Checks (n/a)
- iOS In the present day Extension (com.tiborbodecs.theswiftdev.todayextension)
- watchOS Software (com.tiborbodecs.theswiftdev.watchos)
- watchOS Software Extension (com.tiborbodecs.theswiftdev.watchos.extension)
- tvOS Software (com.tiborbodecs.theswiftdev.macos)
- macOS Software (com.tiborbodecs.theswiftdev.tvos)
NOTE: If you happen to rename your iOS goal with a WatchKit companion app, watch out!!! You even have to alter the
WKCompanionAppBundleIdentifier
property inside your watch software goal’sData.plist
file by hand. ⚠️
This methodology would possibly seems to be like an overkill at first sight, however belief me it’s value to observe these conventions. As your app grows, finally you’ll face the identical points as I discussed at first. It’s higher to have a plan for the long run.