I start a new project in swift with a workspace. I add a new package in the project called ApplicationCore by using File - New - Package. After i add the new package i see a a file in the package which is automatically created called ApplicationCore.swift with the following code.
//ApplicationCore.swift
public struct ApplicationCore {
public private(set) var text = "Hello, World!"
public init() {
}
}
Then i add another folder called Colors and inside this folder i create a new swift file called AppColors.swift. Here is the code...
//AppColors.swift
import Foundation
public struct AppColors {
public let greenColor = Color.green
}
So i need to access the struct AppColors from within the struct ApplicationCore which exists in the ApplicationCore.swift file. I use this code inside ApplicationCore
public private(set) var AppColorGreen = AppColors.greenColor
I get a message saying
Cannot find 'AppColors' in scope
What am i missing? I am new to swift so any help appreciated.
Actual Path: Sources/ApplicationCore/ApplicationCore.swift and Sources/Colors/AppColors.swift
CodePudding user response:
The problem seems to be that you've put your Colors
folder in the wrong place.
For a package that provides a single product (library, executable, etc...), if that product is called X
, then you want all of the source code for X
to be inside of Source/X
. This is how SPM sets up a package initially.
In your case, you have a package, which I suppose is called ApplicationCore
, which provides a product/target that is also called ApplicationCore
. So all of your source code should go into Source/ApplicationCore
.
You don't have to use the same name for the product as for the package, but that is the default for the Package.swift
file SPM creates. To change it, you'd need to edit the products
and targets
arrays in Package.swift
, and name the directory in Source
to be whatever name you pick for the product.