Swift. dyld: Library not loaded - swift

I've added a custom library (attach project) to my project.
If I use it in main.swift all is good.
But, if I try to make it like:
import ARISockets
class MyClass {
var listenSocket : PassiveSocketIPv4?
}
then it gives an error
dyld: Library not loaded
PS: I make Command Line Tool for a mac
PSS: Add library https://github.com/AlwaysRightInstitute/SwiftSockets

Related

Mac: Bundle not found in embedded Helper app

I have a Swift Package with shared code.
With Xcode's SPM added to my Xcode project.
Works great in the main app.
Doesn't work in the helper app when exported. Running the helper app in Xcode itself works fine. But when running as an exported and signed app it doesn't work.
This error message was found in console:
Fatal error: unable to find bundle named MySwiftPackageName: file MySwiftPackageName/resource_bundle_accessor.swift, line 27
I double checked, the package is really added at
Build Phase -> Link Binary With Libraries
When I inspect the exported main app and helper app I found the package in the main resources folder but not in the helper app resources folder.
How can I fix this?
In the Swift Package Manager the use of Bundle changes a bit, when you compile the source or open up Xcode a autogenerated code is available to you, which is Bundle.module.
The Bundle.module code is fairly simple and should resolve the issue, it is used like this.
SwiftUI
Image("ImageName", bundle: Bundle.module)
UIKit
UIImage(named: "ImageName", in: .module, with: nil)

Swift Package Manager (package successfully added, but Module not found)

I'm new in Swift. I want to create iOS app that can connect to PostgreSQL database. First I found library https://github.com/vapor/postgresql.git that should be added to my project via Swift Package Manager. Using tutorial I added required library to my project successfully (File -> Swift Packages -> Add Package Dependency):
list of added packages from SPM
But when I try to import this module into my view controller, Xcode shows error that module is not found:
not found
I tried several times to rebuild my project, created new project just for testing this issue. Also I found information about build phases and added this lib as a dependency:
build phases
But I still get error: "No such module PostgreSQL".
Can anyone help me?
I found solution by myself. I compiled C static library "libpq" (can be found in PostgreSQL sources) and added it to my swift project. Included this library by adding special bridging header file. Finally I got what I wanted.
P.S. If someone what to repeat, he or she should know: static C library must be compiled for iOS device architecture (and also in iOS simulator architecture which differs from iOS device arch.).

How to test a class in a Swift package?

I'm currently trying to understand the mechanism of importing dependencies in a Swift package and I've run into an issue with tests. Hope someone can explain what I'm doing wrong. I'm going to describe the problem step-by-step so that you could easily reproduce it.
So I'm creating a new Swift package using swift package init --type executable. This command creates the basic Swift package structure:
Artems-MacBook-Pro:SwiftExample artem$ swift package init --type executable
Creating executable package: SwiftExample
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/SwiftExample/main.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/SwiftExampleTests/
Creating Tests/SwiftExampleTests/SwiftExampleTests.swift
Creating Tests/SwiftExampleTests/XCTestManifests.swift
The package itself is called SwiftExample. As you can see the command also creates an example of a unit test case (SwiftExampleTests.swift).
Then I create a simple class called Car.swift and put it into the Sources/SwiftExample/Classes/ directory:
// Sources/SwiftExample/Classes/Car.swift
class Car {
init() {
print("I'm a car!")
}
}
In the main.swift file I can create an instance of the Car class and everything works pretty much fine:
// Sources/SwiftExample/main.swift
print("Hello, world!")
let car = Car()
The output would be:
Hello, world!
I'm a car!
But the problem is I cannot use this class in my test file. For example, I'm trying to create an instance of the Car class in the testExample() function of the SwiftExampleTests.swift file:
import XCTest
import class Foundation.Bundle
#testable import SwiftExample
final class SwiftExampleTests: XCTestCase {
func testExample() throws {
let car = Car()
<other code goes here>
}
<other code goes here>
}
As you can see I've imported the module itself using the keyword #testable. But when I run swift test command I'm getting this weird error:
Compile Swift Module 'SwiftExample' (2 sources)
Compile Swift Module 'SwiftExampleTests' (2 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/SwiftExample
/Users/artem/Playgrounds/SwiftExample/Tests/SwiftExampleTests/SwiftExampleTests.swift:9:13: warning: initialization of immutable value 'car' was never used; consider replacing with assignment to '_' or removing it
let car = Car()
~~~~^~~
_
Linking ./.build/x86_64-apple-macosx10.10/debug/SwiftExamplePackageTests.xctest/Contents/MacOS/SwiftExamplePackageTests
Undefined symbols for architecture x86_64:
"_$S12SwiftExample3CarCACycfC", referenced from:
_$S17SwiftExampleTestsAAC04testB0yyKF in SwiftExampleTests.swift.o
"_$S12SwiftExample3CarCMa", referenced from:
_$S17SwiftExampleTestsAAC04testB0yyKF in SwiftExampleTests.swift.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/artem/Playgrounds/SwiftExample/.build/debug.yaml test output:
I'm certainly doing something wrong here but I can't find any information on the matter in the official docs. Does somebody know what's happening here and how to fix that?
Apparently, the issue won't reproduce anymore in the latest Swift version. The Car class can be imported and the swift test command won't fail.
For the record, my current swift --version output is:
swift-driver version: 1.45.2 Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
Target: arm64-apple-macosx12.0

Importing a local swift module

Swift newbie here, using swift 3 on Linux with the package manager.
I have a package Regulate, executable, and a sibling package Utils, intended to be a library. Utils/Sources has a TextReader.swift file, with class TextReader and its init function both public. The Utils directory is a git repo, described in Regulate/Package.swift:
dependencies: [.Package(url: "../Utils", "1.0.0")]
I've tried 3 ways to instantiate a TextReader object in the Regulate program and gotten 3 error messages:
import Utils
...
let reader = TextReader(filename: name)
error: use of unresolved identifier 'TextReader'
import Utils
...
let reader = Utils.TextReader(filename: name)
error: module 'Utils' has no member named 'TextReader'
import class Utils.TextReader
error: no such decl in module
It looks like the library module needs some additional structure to declare its exports, perhaps.
What do I need to do here? Thanks!
D'oh! This looks like a name conflict.
When I use Utils2 instead of Utils, it works fine. With Utils it cloned and built the other module, but apparently went to some system module instead when I referenced it.

Importing other libraries when using Swift as a scripting language

Heyo. I'm using Swift to make a simple web crawler for fun and practice. I made an Project.swift file and added it to a folder on my desktop. I now want to add SwiftyJSON to my project. I tried putting SwiftyJSON.swift in the same folder and adding import SwiftyJSON on top, but this did nothing. No import statement does not work at all. Is there any way to do this except pasting the whole file to the bottom of my project.swift file, or should I just stick to Python?
You have to start from the file named main.swift - that's your application entry point:
// main.swift
import Foundation
let data = "{\"message\" : \"Hello World\"}".dataUsingEncoding(NSUTF8StringEncoding)
let myJson = JSON(data:data!)
print(myJson["message"])
So you don't have to use include, but you'll need to specify all external dependencies when compiling:
swiftc SwiftyJSON.swift main.swift
./main
If you're using XCode Command Line Tool template, main.swift will be created for you by default, and you'd be able add more .swift files to your project and just use them, no need to use import.