Swift Packages

RSS for tag

Create reusable code, organize it in a lightweight way, and share it across Xcode projects and with other developers using Swift Packages.

Swift Packages Documentation

Posts under Swift Packages tag

260 Posts
Sort by:
Post marked as solved
8 Replies
5.8k Views
Hi, I've got a Swift Framework with a bunch of Metal files. Currently users have to manually include a Metal Lib in their bundle provided separately, to use the Swift Package. First question; Is there a way to make a Metal Lib target in a Swift Package, and just include the .metal files? (without a binary asset) Second question; If not, Swift 5.3 has resource support, how would you recommend to bundle a Metal Lib in a Swift Package?
Posted
by
Post not yet marked as solved
9 Replies
6.3k Views
Our project is split up into multiple Pods with CocoaPods and I'm currently trying to convert all of our podspecs to SPM. One of our projects has XCFramework dependencies that I'm trying to include from a local source, but my project files are complaining that the modules couldn't be found when I try to import them. Is there something I'm missing here? My Package.swift file looks something like this: // swift-tools-version:5.3 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package(     name: "MyPackage",     defaultLocalization: "en_us",     platforms: [         .iOS(.v12),         .watchOS(.v6),         .macOS(.v10_15)     ],     products: [         .library(             name: "MyPackage",             targets: [                 "MyPackage"             ]),     ],     dependencies: [     ],     targets: [         .target(             name: "MyPackage",             dependencies: [                 .byName(name: "LocalXCFramework")             ],             path: "Sources/"         ),                  .binaryTarget(             name: "LocalXCFramework",             path: "Frameworks/LocalXCFramework.xcframework"         )              ] ) In this example, if I try to import the modules from LocalXCFramework, I get an error saying it couldn't be found.
Posted
by
Post marked as solved
4 Replies
7.2k Views
In the talk, it was explained how to use a binaryTarget to add a .xcframework from a url, but what about a local path? Take the following Package and file structure, is this the correct way to structure and refer to the .xcFramework? | SamplePackage | - | Package.swift | - | README.swift | - | Sources | - | - | Sample | - | - | - | file.swift | - | - | SampleFramework | - | - | - | framework.xcframework | - | - | Tests | - | - | - | LinuxMain.swift | - | - | - | SampleTexts | - | - | - | - | sampleTests.swift // swift-tools-version:5.3 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package(     name: "Sample",     platforms: [         .iOS(.v13),         .macOS(.v10_12)     ],     products: [         // Products define the executables and libraries produced by a package, and make them visible to other packages.         .library(             name: "Sample",             targets: ["Sample", "SampleFramework"]),     ],     targets: [         // Targets are the basic building blocks of a package. A target can define a module or a test suite.         // Targets can depend on other targets in this package, and on products in packages which this package depends on.         .target( name: "Sample"),         .testTarget(             name: "SampleTests",             dependencies: ["Sample"]),         .binaryTarget(             name: "SampleFramework",             path: "framework.xcframework")     ] ) I am getting the following error(s): invalid custom path 'framework.xcframework' for target 'SampleFramework' target path '/framework.xcframework' is not supported; it should be relative to package root
Posted
by
Post marked as solved
30 Replies
17k Views
I keep getting a Xcode Previews error in Xcode 12 from a package that imports other packages. As soon as I remove the dependency I'm able to see the SwiftUI preview. I'm only able to use previews in packages that have no dependencies The error I get is: "LoadingError: failed to load library at path...Library not loaded " then it point to the dependency that it could not load in the current package. How can I access Xcode Previews from a package that depends on other packages?
Posted
by
Post not yet marked as solved
7 Replies
8.2k Views
I have been able to ship some image and asset catalogs in a Swift package with success using Xcode 12b1 and Swift 5.3. I am not having so much luck with using a custom .ttf file in a Swift Package. I am loading a .ttf file in the manifest like so: .target( 	name: "BestPackage", 	dependencies: [], 	resources: [ 		.copy("Resources/Fonts/CustomFont.ttf"), 		.process("Resources/Colors.xcassets") 	] ), And I noticed that there's no initializer on the Font type in SwiftUI to include an asset from a module. For example, this works: static var PrimaryButtonBackgroundColor: SwiftUI.Color { 	Color("Components/Button/Background", bundle: .module) } However, there's no way to specify where a font is coming from. I was hoping that loading it into the module would emit it into the target for use, but no such luck: static var PrimaryButtonFont: Font {   Font.custom("CustomFont", size: 34) } This does not load the font as expected. I'm investigating using a CoreText api to try and trick it into loading, but I feel like there should be an easier way. Any advice?
Posted
by
Post not yet marked as solved
5 Replies
5.1k Views
Hey people, I'm using (or rather trying to use) Swift Packages for my project. This is not about the countless other bumps I found along the road. (Xcode/SPM is still very beta with Xcode 12.0.1 but I'll reserve complaining about for another day). But one I did not even something vaguely useful when googling it. So I started out using file paths for my dependencies, like: .package(name: "BlaBla", path: "/Users/me/GitDev/BlaBla") for some reason that does not work when you want to chain dependencies (Another bug to fix, Apple!). So I wanted to change to versions using a git repo. I gave my repo a tag and put in: .package(url: "ssh://me@server.local/usr/home/me/GitDev/BlaBla", from: "0.0.1"), On the command line SPM has no problem resolving this:  % swift package resolve Fetching ssh://me@… Cloning ssh://me@… Resolving ssh://me@… at 0.0.1 In Xcode though, I get the line from the title: An unknown error occurred. username does not match previous request (-1) (by the way I have copied this by hand as the message could not even be copy & pasted) Any ideas how to solve this? Or even debug this any further? best Roddi
Posted
by
Post marked as solved
6 Replies
2k Views
I have an iOS app by using a swift package to hold most of the logic. However, the previews of SwiftUI views often fail with following error: HumanReadableSwiftError BuildError: failedToGenerateThunkInfo(Error Domain=com.apple.xcbuild Code=19 "could not generate preview info: noTargetBuildGraph" UserInfo={NSLocalizedDescription=could not generate preview info: noTargetBuildGraph}) To reproduce this, please clone https://github.com/pointfreeco/isowords and check for preview of ChangelogView.
Posted
by
Post not yet marked as solved
4 Replies
11k Views
I'm working on a simple iOS app and am trying to add a Swift Package dependency to my project (in my case, Alamofire) and I'm getting an error when I paste their GitHub URL into the box asking for it. It reads The operation couldn't be completed. (PackageCollections.JSONPackageCollectionsProviderError error 3.). I also tried to see if the error was because Alamofire isn't updated for Xcode 13, but a package generated locally for development and pushed to GitHub also has the same error (That url is: here on Github). Any help would be great!
Posted
by
Post not yet marked as solved
2 Replies
2.4k Views
In regular projects, there is an "Export Localizations" option in the Product menu, but for Swift Packages, this is not available. Adding local Swift Packages as dependencies of a regular project and using Export Localizations on the regular project doesn't include the Localizable strings from the Packages' sources either. The command-line version (xcodebuild -exportLocalizations) has the same restrictions. Is there a way to export NSLocalizedString occurrences to an xliff for Swift Packages? Or are we supposed to do things manually for now?
Posted
by
Post marked as solved
14 Replies
58k Views
I have a project I've been working off of for a few years now that makes use of local Swift Packages. In my workspace, I have a "Kit" project that generates a framework using several local Swift Packages as dependencies. In Xcode 12, the local Swift packages resolve correctly and compiles, but each beta of Xcode 13 gives me the error: "[ProjectName].xcodeproj Missing package product '[LocalSwiftPackage]'" Clearing the package cache, derived data, or restarting the application does not resolve the issue. Only Swift Packages that are remotely loaded from a URL are compiled correctly. Using local packages to separate module code, while still being able to edit in the same project, has been essential to my workflow and would hate to see Xcode 13 release with these breaking changes, and I have not seen this issue listed in the known issues for the Xcode 13 release notes. Has anyone else experienced this issue or found a workaround?
Posted
by
Post not yet marked as solved
2 Replies
3.8k Views
I have a Swift package that i've been working on for a while. I upgraded to Xcode 13 a month or two ago. I have a couple of large JS files in my package (third party NPM libraries that I can't re-write). Randomly, Xcode will throw an error (but not a real error because I can continue as normal) that It can't load the folder containing the JS files This doesn't prevent me from doing anything, but its irritating to have this error constantly showing up. Is there anything I can do to solve this or is this an Xcode bug?
Posted
by
Post not yet marked as solved
5 Replies
1.9k Views
Hi, I'm trying out Xcode Cloud but can't get it to build in my scenario. The use case is the following: My project has multiple swift packages that depend on each other. "App" has a local package dependency called "SPMKit". "SPMKit" has a local package dependency called "SPMKit2". The issue I run into is if "SPMKit2" has any dependencies, it doesn't resolve in Xcode Cloud. Are local dependencies like this not supported in Xcode Cloud? All the packages are in the same directory as my app. I'd like to not have to put each package in it's own git..
Posted
by
Post marked as solved
11 Replies
7.8k Views
Hello everyone, I have a problem with my Xcode, when I try to add package dependency it stuck on verifying screen, at the beginning of the preparing to validate part. anyone has the same problem?
Posted
by
Post not yet marked as solved
8 Replies
4.3k Views
When I try to build my iOS app using Xcode Cloud, it encounters an error when trying to resolve packages: an out-of-date resolved file was detected at [path to package.resolved], which is not allowed when automatic dependency resolution is disabled; please make sure to update the file to reflect the changes in dependencies Looking at my package.resolved file, it all seems to be in order. What can I do to fix it?
Posted
by
Post not yet marked as solved
2 Replies
852 Views
My Swift app accesses the USB serial port via a C++ API built as a universal dylib. When the dylib is added as a framework via "Frameworks, Libraries, and Embedded Content", it works fine. When instead the dylib is included as a binary target within an xcframework inside a Package, I get an error indicating a permissions issue. The package looks like this: // swift-tools-version:5.3 import PackageDescription let package = Package( name: "BrainFlow", platforms: [ .macOS(.v10_14) ], products: [ .library( name: "BrainFlow", targets: ["BrainFlow", "BoardController", "BrainBitLib", "DataHandler", "GanglionLib", "MLModule", "MuseLib"]) ], dependencies: [ // Dependencies declare other packages that this package depends on. ], targets: [ .target( name: "BrainFlow" ), .binaryTarget( name: "BoardController", path: "BoardController.xcframework" ), .binaryTarget( name: "BrainBitLib", path: "BrainBitLib.xcframework" ), .binaryTarget( name: "DataHandler", path: "DataHandler.xcframework" ), .binaryTarget( name: "GanglionLib", path: "GanglionLib.xcframework" ), .binaryTarget( name: "MLModule", path: "MLModule.xcframework" ), .binaryTarget( name: "MuseLib", path: "MuseLib.xcframework" ), .testTarget( name: "BrainFlowTests", dependencies: ["BrainFlow"] ) ] )
Posted
by
Post not yet marked as solved
2 Replies
2.3k Views
Hello, i am currently trying to implement a pre build script execution in a package. I am using this tutorial to implement it. But it only shows how to use the pre build feature with an already existing feature. I want to execute a skript on my pc every time i build the project. I can't use apples prebuild skript feature in the build settings because it is a package. Right now my code looks like this: import Foundation import OSLog import PackagePlugin @main struct GenerateLocalizer: BuildToolPlugin {     do {       let commandlineOutput = try shell("./testScript.sh")     } catch {     }     return [.prebuildCommand(displayName: "Generate Strings", executable: PackagePlugin.Path("./scrips/generate_strings.sh"), arguments: [], outputFilesDirectory: target.directory)]   }   func shell(_ command: String) throws -> String {     let task = Process()     let pipe = Pipe()     task.standardOutput = pipe     task.standardError = pipe     task.arguments = [command]     os_log("%{public}@", log: log, task.arguments?.joined(separator: " ") ?? "not found")     task.launchPath = "/bin/zsh"     task.standardInput = nil     try task.run()     let data = pipe.fileHandleForReading.readDataToEndOfFile()     let output = String(data: data, encoding: .utf8)!     return output   } } and my Package file looks something like that       name: "CustomSdk",       dependencies: ["CustomSdk-Objc"],       path: "CustomSdk",       exclude: ["Info.plist", "CustomSdk.podspec"],       plugins: [         .plugin(name: "GenerateLocalizer")       ]     ),     .plugin(name: "GenerateLocalizer",         capability: .buildTool(),         dependencies: ["CustomSdk-Objc"]     ) It gets called properly but when want to write files in my "testScript.sh" it only says: Operation not permitted. Anyone got any ideas how to fix this or is there another way to utitlize custom scripts with custom packages? Greetings Ben
Posted
by
Post not yet marked as solved
1 Replies
837 Views
There is a package that I want to use in my iOS app project. Although it lacked certain functionality, the GitHub community is great and it was added by some kind soul. However, as it was not yet merged with original repository, this functionality lives only in the fork. And that's when there came the trouble. Although I'm supplying the Package Manager with the link to the fork, it does a great job in correcting me and points me to the original repo. I've seen that one can lock onto a specific commit hash in this article: https://www.swiftbysundell.com/articles/managing-dependencies-using-the-swift-package-manager/#adding-remote-dependencies, but it was only shown for Packages, not Projects in Xcode. Is there a way out of it? Is there a file I could edit to bring me the functionality I crave before I grow old?
Posted
by
Post not yet marked as solved
3 Replies
2.2k Views
Lately we have added swift code to our SDK. (especially swiftUI library). The SDK deployment target is ios9. While importing the SDK to the hosting app with the same deployment target and building via Xcode (13.4) everything works fine. When we try to build it via xcodebuild (xcodebuild -workspace UIKitCatalog.xcworkspace -scheme UIKitCatalog -sdk "iphonesimulator") the build is done successfully but the the app is crashing on app lunch (sim and device) . Increasing the deployment target of the hosting app to iOS 13 fixes the issue (unfortunately we cant force our customers to do the same) CrashReporter Key: 74B25560-EFCE-769F-F0B4-E4DD4C6B09A4 Hardware Model: MacBookPro15,1 Process: UIKitCatalog [63980] Path: /Users/USER/Library/Developer/CoreSimulator/Devices/F044CBD3-80D6-49FB-BAC9-FA48FA3CFF7E/data/Containers/Bundle/Application/72290230-2DB3-4F75-8327-75B6E4E9947C/UIKitCatalog.app/UIKitCatalog Identifier: ba.UIKitCatalog Version: 2.0.0.23386 (23386) Code Type: X86-64 (Native) Role: Foreground Parent Process: launchd_sim [56632] Coalition: com.apple.CoreSimulator.SimDevice.F044CBD3-80D6-49FB-BAC9-FA48FA3CFF7E [10638] Responsible Process: SimulatorTrampoline [2048] Date/Time: 2022-08-29 11:59:21.3026 +0300 Launch Time: 2022-08-29 11:59:19.9381 +0300 OS Version: macOS 12.5.1 (21G83) Release Type: User Report Version: 104 Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Triggered by Thread: 0 Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 libsystem_kernel.dylib 0x7fff7017300e __pthread_kill + 10 1 libsystem_pthread.dylib 0x7fff701cb1ff pthread_kill + 263 2 libsystem_c.dylib 0x7fff2010b6b7 abort + 130 3 libswiftCore.dylib 0x10e7c0af5 swift::fatalError(unsigned int, char const*, ...) + 149 4 libswiftCore.dylib 0x10e7b86ec checkVersion() + 44 5 dyld_sim 0x10d0689f7 invocation function for block in dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const + 182 6 dyld_sim 0x10d0850f5 invocation function for block in dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 129 7 dyld_sim 0x10d07e58c invocation function for block in dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const + 555 8 dyld_sim 0x10d07d6e9 dyld3::MachOFile::forEachLoadCommand(Diagnostics&, void (load_command const*, bool&) block_pointer) const + 129 9 dyld_sim 0x10d07e31f dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const + 179 10 dyld_sim 0x10d084b56 dyld3::MachOAnalyzer::forEachInitializerPointerSection(Diagnostics&, void (unsigned int, unsigned int, unsigned char const*, bool&) block_pointer) const + 118 11 dyld_sim 0x10d084d98 dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 386 12 dyld_sim 0x10d06892a dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const + 144 13 dyld_sim 0x10d068ac2 dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 190 14 dyld_sim 0x10d068aa5 dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 161 15 dyld_sim 0x10d068aa5 dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 161 16 dyld_sim 0x10d068b60 dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const + 102 17 dyld_sim 0x10d077b5a dyld4::APIs::runAllInitializersForMain() + 222 18 dyld_sim 0x10d05b9fe dyld4::prepare(dyld4::APIs&, dyld3::MachOAnalyzer const*) + 2528 19 dyld_sim 0x10d05be42 _dyld_sim_prepare + 379 20 dyld 0x110e6eb78 dyld4::prepareSim(dyld4::RuntimeState&, char const*) + 1265 21 dyld 0x110e6d70e dyld4::prepare(dyld4::APIs&, dyld3::MachOAnalyzer const*) + 244 22 dyld 0x110e6d4e4 start + 388 How can we overcome this issue? What Xcode is doing under the hood that xcodebuild doesnt do? Is it possible to release an SDK which import swiftUI but will have deployment target lower than 13. (I have wrapped all the the swift code with @available() and #if canImport(SwiftUI) - less relevant as its compile time )
Post not yet marked as solved
1 Replies
901 Views
Hello all. I am trying to work on some packages, but if I go to the project navigation and do anything from starting to edit a file, to creating a file, folder etc, Xcode immediately crashes with segmentation fault. I am not running anything. This only happens with the packages, if I open a regular project or workspace and work on it, it's fine. Other people I am working with do not seem to have this issue. I will post the full log here, I tried putting a snippet here but it says I'm not allowed to CrashReport.txt Thank you very much Hardware: MacBook Pro, Chip M1 Pro, macOS Monterey v12.5.1
Posted
by