A Module
I have a module (A) that has two structs.
- One with a color initiated directly in code.
- One with a color loaded from the assets.
public struct CodeColor {
public init() { }
public let value = SwiftUI.Color(#colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1))
}
public struct AssetColor {
public init() { }
public let value = SwiftUI.Color("Legacy/Material/Gold", bundle: .module)
}
The preview is working like a charm:
B Module
The second module (B) should use the previous one (A) as a dependency to load colors from:
import A
public struct CodeColor {
public init() { }
public var value: SwiftUI.Color { A.CodeColor().value }
}
public struct AssetColor {
public init() { }
public var value: SwiftUI.Color { A.AssetColor().value }
}
But as soon as it touches the assets from module (A), the preview crashes:
🛑 The error:
Can not preview in this file. Failed to update preview.
RemoteHumanReadableError: Failed to update preview.
The preview process appears to have crashed.
Error encountered when sending 'previewInstances' message to agent.
==================================
| RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)
|
| BSServiceConnectionErrorDomain (3):
| ==BSErrorCodeDescription: OperationFailed
So why is that?
Note: The stranger thing is that the exact B preview code is working if it is in an actual app (not another package)
2
Answers
I found a solution in the developer forum:
Link
https://developer.apple.com/forums/thread/664295?login=true#reply-to-this-question
NOTE – LOCAL
The solution is for local package. You define
REMOTE (Packages from GitHub e.g.)
If you have your package on GitHub and fetch it remotely, you can’t define local packages and you have to slightly change it to:
Example
Here is a full implementation example, you have to put it inside your package with the resources:
Define your bundle
Accessing package A resource in package B
You can now access the resource from package A inside package B like:
This workaround worked for me in Xcode 14.2