I have some proprietary code I'm not able to share. But I need to import the code into another project. We typically use cocoapods for this sort of things but this project is behaving differently, and I can't figure out why. In the first project, let's call it FooKit, there are a number of dependencies which have been imported as pods, lets call them BarKit, BazKit, and ZotKit. I ran the create podspec command and built it all out correctly (I think), linting the podspec isn't throwing anything unexpected.
To test this out, I create a new project, which is just an empty project. I did the whole pod init. I setup the podfile and ran pod install, and it ran successfully. In the workspace, I now have the App and Pods projects. If I tap on the Pods project, I can see the list of targets. All of the dependencies of the FooKit have been installed, and they are all yellow toolbox icons. FooKit is also in the list of targets, but its icon is a red bullseye.
In the App project, if I go into the view controller and do
import Foundation
import FooKit
class viewController: UIViewController {
// View Controller boiler plate code
}
I get the error No such module 'FooKit'. But I can import any of the dependencies of FooKit, like this
import Foundation
import BarKit
import BazKit
import ZotKit
// View controller code from above
That doesn't throw any errors. What am I doing wrong? I need to access the functions built out in FooKit.
Thanks all
The issue ended up being that in the new podspec I had made, I hadn't specified some of the other dependencies for BazKit. This made it so that certain .swift files in both the new project I had made and the FooKit project were showing different versions. When I had specified in the podspec for FooKit all the correct dependencies versions, then everything resolved itself.
Related
I've been using Cocoapods to manage dependencies for my Swift project. I came across this package which is not listed at Cocoapods. Instead it suggests using Swift Package Manager. However, whenever I try to use Swift Package Manager to do basically anything, it ends up completely destroying my entire project.
So, in order to figure out how to actually use Swift Package Manager, I'm playing with it in a test project.
Here's what I've tried:
Create a new project from Xcode
File -> New -> Project -> Cocoa App
Product Name: basic-ssh-test
This creates a basic application which loads a blank window when I hit "Run". Just for fun I added print("test") to the applicationDidFinishLaunching function in my AppDelegate so the debug window says "test" when I run the program.
Now I want to add the Shout package as a dependency.
swift package init --type executable
This creates the following files:
Creating executable package: basic-ssh-test
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/basic-ssh-test/main.swift
Creating Tests/
Now I'm going to add the "Shout" dependency to my new Package.swift file:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "basic-ssh-test",
dependencies: [
.package(url: "https://github.com/jakeheis/Shout", from: "0.2.0")
],
targets: [
.target(
name: "basic-ssh-test",
dependencies: ["Shout"]),
]
)
And I'll pull in the dependencies:
swift package resolve
This results in the dependencies being pulled into the .build directory:
Fetching https://github.com/jakeheis/Shout
Fetching https://github.com/jakeheis/CSSH
Fetching https://github.com/IBM-Swift/BlueSocket
Cloning https://github.com/IBM-Swift/BlueSocket
Resolving https://github.com/IBM-Swift/BlueSocket at 0.12.91
Cloning https://github.com/jakeheis/CSSH
Resolving https://github.com/jakeheis/CSSH at 1.0.3
Cloning https://github.com/jakeheis/Shout
Resolving https://github.com/jakeheis/Shout at 0.3.0
Now I regenerate the xcodeproj file:
swift package generate-xcodeproj
Now when I open the xcodeproj file there is a new group called Sources that has a single directory basic-ssh-test with a single swift file in it main.swift with print("Hello, world!").
The code that I'm actually interested in running is now in a blue folder called basic-ssh-test. All of the necessary files are still in there, but instead of running my application, Xcode is running the main.swift file. I can tell because the debug output is "Hello, world!" instead of "test".
I've read a couple of tutorials that claim that Swift Package Manager will move my source files and continue to build the same as before, but that's clearly not the case.
There's also no longer a "Main Interface" option in my Build Settings, so I can't select "MainMenu.xib" as my application starting point.
This is essentially the same thing that happens when I try to use Swift Project Manager with my existing project. It pulls in the dependencies, but it basically ignores my entire existing project and just runs the "hello world" code.
How do I use Swift Package Manager to add a dependency to an existing project without ignoring the existing codebase?
I think SPM is only compatible with Mac command line executables or libraries. This explicitly states SPM doesn't support iOS, watchOS, or tvOS platforms at all. But, since macOS AppKit/Cocoa application targets are very similar to iOS or tvOS Xcode targets, I would say this statement implies that SPM can't be used with macOS Cocoa applications out of the box either, which is what I think you're hoping for.
It looks like there is some work here on how to use it with iOS application targets which should largely translate to a macOS Cocoa target.
Until the time of this writing answer, Swift Package Manager do not support the iOS, tvOS, and watchOS. Instead, you will have to add the files from the package directly to your project.
I would suggest you creating a Dependencies group in your project and a group below that with the package name, like this answer:
So, first you will add the files dependency that you want to import into your project to the name of the package name that included in Dependencies group that we make before.
After you add the files then you can access the code as you usually would when you write it yourself like the image below. No need imports. You can see more details in here. hope it helps.
You can create a windowed application using SPM (I have a whole project sitting waiting for ABI that does this) you just need some boiler plate in swift.main. I’ll dig it out.
However the method others are suggesting is never going to work (with current versions of SPM).
However, what I am seeing is that you really want to use SPM to manage dependencies (keeping up to date etc). So I think you could do that much more easily.
Rather than creating a executable as your default target, instead create a library (perhaps call it basic-ssh-dependencies). Generate your Xcode project and drag THAT into your primary Xcode project and configure your target to to depend on it. When you update the library from SPM for changes in dependencies you should re-gen your Xcode-proj.
Let me know your mileage and any wrinkles, or if I’ve misunderstood what you are trying to achieve.
Update for XCode 11
This is A LOT easier now with XCode 11. As of this writing the current version of XCode is 11.3.1.
For an existing XCode project simply:
go to File->Swift Packages->Add Package Dependency...
enter the URL of the hosted package you want to add
choose the rules about which versions your app will use
click Finish
So I received a project from a client to perform a code review on. I can't get it to work though.
I am getting an error on an extension class for Alamofire, which is included using CocoaPods. The class contains an import Alamofire statement, so it is included. Cmd clicking the name of the class navigates to the Alamofire pod and the relevant class (DataRequest), so that works.
I have tried building the pods by themselves, to ensure they are built. I have also tried removing my DerivedData folder, but now I come up short with ideas.
How can I fix this?
Edit: It seems I get other errors about classes not being found, from other Pods and other simple references (not extensions), it looks like my pods are not being integrated in the compile process.
Edit 2: Think I found it:
Target 'Pods-Project' of project 'Pods' was rejected as an implicit dependency for 'Pods_Project.framework' because its architectures 'arm64' didn't contain all required architectures 'armv7 arm64'
I have removed the Pods folder, deleted the Podfile.lock file, cleared DerivedData and set the build settings of the pod project to build all archs, not only the selected ones, now it works!
I want to use the SpriteKit-Spring Extension in my Swift SpriteKit project, but I do not get it working.
The GitHub repro is here, although there is an instruction, I do not get it working in my project.
So what I did is:
I downloaded the file 'SpriteKit-Spring.swift'
dragged it into my Xcode project
added 'import SpriteKit_Spring' at the top of my GameScene.swift file.
And then I got the error from the compiler, that there is no such module 'SpriteKit_Spring'.
What did I do wrong? I thought I followed the instructions. And I did not get the pods install right unfortunately, thats why I added this extension manually.
when I try to import MBCalendarKit in my project, it shows no such module error and linker command error. I tried all the solution given in stackoverflow under this topic and no success. help me.
Thanks in advance.
I've just imported into a test project, and using the latest version of Xcode (7.2.1) it's slightly different than the installation instructions.
Assuming you're using pods, install the pod and make sure to open the .xcworkspace file.
Next add a bridging header to the project, and import the main calendar kit file, like so
#import <MBCalendarKit/CalendarKit.h>
Inside your UIViewController subclass, import MBCalendarKit like so:
import MBCalendarKit
Celebrate!
I am sharing the realm between containing app and extension (custom Keyboard), and it works ok on simulator, but when I'm trying to run this app on the real device I have the following error:
Cannot load underlying module for 'RealmSwift'
It's kinda strange that it is not an issue with simulator but ok. Let's add pods.framework to Link Binary With Libraries for my extension.
.../Pods/Bolts/Bolts/Common/BFCancellationTokenRegistration.h:19:1:
Duplicate interface definition for class
'BFCancellationTokenRegistration'
OK. Let's remove it.
Cannot load underlying module for 'RealmSwift'
Can't argue that. Let's try to add RealmSwift.framework to the extension.
.../Pods/Bolts/Bolts/Common/BFCancellationTokenRegistration.h:19:1:
Duplicate interface definition for class
'BFCancellationTokenRegistration'
OK. After several iterations there are several Pods.framework in my project folder and RealmSwift.framework too. Let's remove them all except one Pods.framework. Let's change target membership for this framework to the app and the extension.
.../Pods/Bolts/Bolts/Common/BFCancellationTokenRegistration.h:19:1:
Duplicate interface definition for class
'BFCancellationTokenRegistration'
and
Cannot load underlying module for 'RealmSwift'
together.
How could this be fixed?
Update: I am using Realm and Parse cocoapods.
It should just be a matter of creating a second section in your Podfile to let you specify the dependencies for your app extension as well as your main app.
target 'MyApp' do
pod 'RealmSwift'
end
target 'MyAppExtension' do
pod 'RealmSwift'
end
CocoaPods does a lot of extra steps in setting up the dependencies and installing them into your target at compile time, so I don't think simply copying Pods.framework to the extensions build steps will work.
If you've got multiple references to the same files floating around, then it may be a good idea to completely delete every CocoaPods-related file/folder from your project, and running pod install again to make sure it's all set up again properly.
Let me know how you go!