Conventions for Xcode – The.Swift.Dev.

Conventions for Xcode – The.Swift.Dev.


Learn to manage your codebase. If you’re scuffling with Xcode undertaking construction, information, naming conventions, learn this.

Apple has a lot frameworks and APIs that I don’t even know lots of them. We’re additionally dwelling within the age of utility extensions. If you’re making an attempt to create a model new goal in Xcode, you may find yourself scratching your head. 🤔

Conventions for Xcode – The.Swift.Dev.

That is nice for each for builders and end-users, however after creating a couple of targets and platforms (your undertaking grows and) you may ask the query:

How ought to I organise my codebase?

Don’t fear an excessive amount of about it, I might need the correct reply for you! 😉

The issue with advanced initiatives

You’ll be able to create apps in Xcode for all the most important working techniques: iOS, macOS, tvOS, watchOS. Within the newest model of Xcode you too can add greater than 20 extension only for iOS, plus there are many app extensions out there for macOS as effectively. Think about a posh utility 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 assessments, these are particular person targets as effectively! ⚠️

So far as I can see, in case you are making an attempt to help a number of platforms you will have plenty of targets inside your Xcode undertaking, moreover each new goal will comprise some sort of supply information and belongings. Ought to I point out schemes too? 😂

Even Apple eliminated it’s Lister pattern code, that demonstrated one in all a hellish Xcode undertaking with 14 targets, 11 schemes, however the total undertaking contained solely 71 Swift supply information. That’s not an excessive amount of code, however you possibly can see the problem right here, proper?

It’s time to discover ways to organise your undertaking! 💡

Xcode undertaking group

So my fundamental concept is to have an affordable naming conceptand folder construction contained in the undertaking. This includes targets, schemes, bundle identifiers, location of supply information and belongings on the disk. Let’s begin with a easy instance that accommodates 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 undertaking information for you, so that you shoudn’t care an excessive amount of about conventions and namings in any respect. 🤷‍♂️

Challenge identify

Are you creating a brand new utility? Be happy to call your undertaking as you need. 😉

Are you going to make a framework? Prolong your undertaking identify with the Equipment suffix. Folks often choose to make use of the ProjectKit type for libraries in order that’s the right method to go. When you’ve got a killer identify, use that as an alternative of the equipment type! 😛

Accessible platforms

At all times use the next platform names:

Goal naming conference

Title your targets like:

[platform] [template name]

Don’t embody undertaking identify within the targets (that will be only a duplicate)
Use the extension names from the brand new goal window (eg. In the present day Extension)
Use “Utility” template identify for the primary utility targets
Use “Framework” as template identify for framework targets
Order your targets in a logical method (see the instance)

Scheme names

Merely use goal names for schemes too (prefix with undertaking identify if required).

[project] - [platform] [template name]

You’ll be able to prefix schemes along with your undertaking 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 prefer to separate framework schemes visually from the schems that comprise utility logic, that’s why I at all times transfer them to the highest of the record. Nevertheless a greater strategy is to separate frameworks right into a standalone git repository & join them via a bundle supervisor. 📦

Bundle identifiers

This one is difficult due to code signing. You’ll be able to go along with one thing like this:

[reverse domain].[project].[platform].[template name]

Listed here are the foundations:

  • Begin along with your reverse area identify (com.instance)
  • After the area, insert your undertaking identify
  • Embrace platform names, aside from iOS, I don’t append that one.
  • Use the template identify as a suffix (like .todayextension)
  • Don’t add utility as a template identify
  • Use .watchkitapp, .watchkitextension for legacy watchOS targets
  • Don’t use greater than 4 dots (see instance beneath)!

NOTE: If you will use com.instance.undertaking.ios.right this moment.extension that’s not going to work, as a result of it accommodates greater than 4 dots. So you must merely go along with com.instance.undertaking.ios.todayextension and names like that. 😢

Anyway, simply at all times attempt to signal your app and undergo the shop. Good luck. 🍀

Challenge folders

The factor is that I at all times create bodily folders on the disk. For those who make a bunch in Xcode, effectively by default that’s not going to be an precise folder and all of your supply information and belongings will probably be situated below the undertaking’s foremost listing.

I do know it’s a private desire however I don’t prefer to name an enormous “wasteland” of information as a undertaking. I’ve seen many chaotic initiatives with out correct file group. 🤐

It doesn’t matter what, however I at all times observe this fundamental sample:

  • Create folders for the targets
  • Create a Sources folder for the Swift supply information
  • Create an Property folder for all the things else (photos, and so on).

Below the Sources I at all times make extra subfolders for particular person VIPER modules, or just for controllers, fashions, objects, and so on.

Instance use case

Here’s a fast instance undertaking in Xcode that makes use of my conventions.

Xcode naming conventions

As you possibly can see I adopted the sample from above. Let’s assume that my undertaking identify is TheSwiftDev. Here’s a fast overview of the complete setup:

Goal & scheme names (with bundle identifiers):

  • iOS Utility (com.tiborbodecs.theswiftdev)
  • iOS Utility Unit Exams (n/a)
  • iOS Utility UI Exams (n/a)
  • iOS In the present day Extension (com.tiborbodecs.theswiftdev.todayextension)
  • watchOS Utility (com.tiborbodecs.theswiftdev.watchos)
  • watchOS Utility Extension (com.tiborbodecs.theswiftdev.watchos.extension)
  • tvOS Utility (com.tiborbodecs.theswiftdev.macos)
  • macOS Utility (com.tiborbodecs.theswiftdev.tvos)

NOTE: For those who rename your iOS goal with a WatchKit companion app, watch out!!! You even have to vary the WKCompanionAppBundleIdentifier property inside your watch utility goal’s Information.plist file by hand. ⚠️

This technique may seems to be like an overkill at first sight, however belief me it’s price to observe these conventions. As your app grows, ultimately you’ll face the identical points as I discussed to start with. It’s higher to have a plan for the longer term.

Leave a Reply

Your email address will not be published. Required fields are marked *