Better Programming

Advice for programmers.

Follow publication

Member-only story

Moving SwiftUI Views and Models Into Separate Swift Packages

Write modular code

Millie Dev
Better Programming
Published in
6 min readApr 25, 2022
Image by Mediamodifier from Pixabay

Swift packages are a great way to separate our code into modules that we can use across multiple projects.

This is a simple guide to using them for SwiftUI, but any Swift code can be distributed this way.

Creating The MyViews Package

Open Xcode and click File > New > Package… or alternatively press Command, Control, Shift, and N. It’s useful to create a new folder for this project, which you can do with the New folder button when you’ve decided where you want to put your files. I called my new folder PackagesExample, but the name doesn’t matter. Call your package MyViews and make sure it is not added to any projects or workspaces.

The Sources folder in your package will have separate folders for each target.

Create two new folders with Swift files inside like this:

  • MyViews/Sources/MyViews/MyViews.swift
  • MyViews/Sources/HelloButton/HelloButton.swift

Open up HelloButton.swift and add this:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Millie Dev
Millie Dev

Written by Millie Dev

An iOS developer who writes about gadgets, startups and blockchains. Swift programming tutorials are at typesafely.co.Uk

No responses yet

Write a response