Can't run XCTest for a Cocoa Framework with SwiftyJSON dependency - swift

I am building a dynamic Cocoa Framework (let's say MyFrame) using Swift 1.2 and Xcode 6.4.
I am using SwiftyJSON as a linked framework in MyFrame after downloading it using Carthage. It builds and runs perfectly.
I wrote unit tests in a XCTestCase. The testing target is MyFrameTests. It works perfectly as long as MyFrame does not use SwiftyJSON. When I add SwiftyJSON in MyFrame, I get the following error:
2015-08-25 18:22:25.653 xctest[17732:237778] The test bundle at /Users/manonh/try16/MyFrame/DerivedData/MyFrame/Build/Products/Debug-iphonesimulator/MyFrameTests.xctest could not be loaded because an unanticipated error occurred.
2015-08-25 18:22:25.654 xctest[17732:237778] Detailed error information: Error Domain=NSCocoaErrorDomain Code=3587 "The bundle “MyFrameTests” couldn’t be loaded because it is damaged or missing necessary resources." (dlopen_preflight(/Users/manonh/try16/MyFrame/DerivedData/MyFrame/Build/Products/Debug-iphonesimulator/MyFrameTests.xctest/MyFrameTests): Library not loaded: #rpath/SwiftyJSON.framework/SwiftyJSON
Referenced from: /Users/manonh/try16/MyFrame/DerivedData/MyFrame/Build/Products/Debug-iphonesimulator/MyFrameTests.xctest/MyFrameTests
Reason: image not found) UserInfo=0x7f8e13705070 {NSLocalizedFailureReason=The bundle is damaged or missing necessary resources., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Users/manonh/try16/MyFrame/DerivedData/MyFrame/Build/Products/Debug-iphonesimulator/MyFrameTests.xctest/MyFrameTests, NSDebugDescription=dlopen_preflight(/Users/manonh/try16/MyFrame/DerivedData/MyFrame/Build/Products/Debug-iphonesimulator/MyFrameTests.xctest/MyFrameTests): Library not loaded: #rpath/SwiftyJSON.framework/SwiftyJSON
Referenced from: /Users/manonh/try16/MyFrame/DerivedData/MyFrame/Build/Products/Debug-iphonesimulator/MyFrameTests.xctest/MyFrameTests
Reason: image not found, NSBundlePath=/Users/manonh/try16/MyFrame/DerivedData/MyFrame/Build/Products/Debug-iphonesimulator/MyFrameTests.xctest, NSLocalizedDescription=The bundle “MyFrameTests” couldn’t be loaded because it is damaged or missing necessary resources.}
*** Test session exited(1) without checking in. If you believe this error represents a bug, please attach the log file at /var/folders/9g/jxyyssdx0xn9xp_29fg2l9080000gq/T/com.apple.dt.XCTest-status/Session-2015-08-25_18:22:25-2wDwFh.log
I googled this and found some things about defines module and other options in Build Settings > Packaging. Did not work.
I have tried to add import SwiftyJSON inside the test swift file. Did not work
I also tried to put SwiftyJSON in Copy Bundle Resource in MyFrame. Did not work
In MyFrameTests, I tried to add SwiftyJSON in Link Binary With Libraries and Copy Bundle Resource. I tried all the combinations of the above "locations" of SwiftyJSON framework. Did not work
After 2 days of struggling and googling and reading other stackoverflow posts, I haven't found a way to run this test.
Is there anyone that knows how to unit test a cocoa framework that has a dependency?
Thanks in advance for the help!
Below is the very basic class I want to test in MyFrame and the testcase in MyFrameTests, if it can be of any use.
MyFrameClass.swift
import Foundation
import SwiftyJSON
public class MyFrameClass {
public init() {}
public func getFive() -> Int {
return 5
}
}
MyFrameTests.swift
import UIKit
import XCTest
import MyFrame
class test18Tests: XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func testExample() {
XCTAssert(true, "Pass")
}
func testExample2() {
var c = MyFrameClass()
XCTAssertEqual(c.getFive(), 5, "Five test of MyFrameClass")
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
// Put the code you want to measure the time of here.
}
}
}
Just remove import SwiftyJSON from MyFrameClass.swift and it works like a charm.

I had a similar (not exactly the same though) problem. My swiftyJSON was installed using cocoa pods.
I then removed it form cocoa pods and installed the file manually in my project and that solved the problem. I know I have to manually update that file now, but since it hasn't any dependencies on other frameworks that's fairly easy and a small price to pay

Copy the xcodeproj from SwiftyJSON inside your workspace
In the buildPhase/link binary with libraries from the test target, add "SwiftyJSON" clicking + and choose from the workspace
(you must have the cocoa framework there + the SwiftyJSON framework there)
Command B
Command U

Related

Xcode iOS app build fails due to MXSendReplyEventStringLocalizer protocol

I'm an Xcode newbie. I'm using non-modified Swift source code from the Github project that is 100% working itself as it's released to public.
I installed pods for it.
When trying to build this non-modified testing app in xCode, it fails with:
MXKSendReplyEventStringLocalizer.swift:19:7 Type 'MXKSendReplyEventStringLocalizer' does not conform to protocol 'MXSendReplyEventStringLocalizerProtocol'
As the code itself should be working out of the box, I suppose the issue is caused by my Mac's environment.
if I select "Fix" in the problematic code block (adding protocol stubs offered by Xcode), this error disappears on the next build attempt, but new ones appear, I. believe because the placeholder was set by selecting "Fix" previously.
Command SwiftEmitModule failed with a nonzero exit code
About this code:
_class MXKSendReplyEventStringLocalizer: NSObject, MXSendReplyEventStringLocalizerProtocol {
func replyToEndedPoll() -> String {
<#code#>
}
Any advise what direction to investigate is appreciated. Searching other threads related to MXSendReplyEventStringLocalizer didn't help.
Here is the problematic code block** that is highlighted by Xcode on the initial build:
import Foundation
class MXKSendReplyEventStringLocalizer: NSObject, MXSendReplyEventStringLocalizerProtocol {
func senderSentAnImage() -> String {
VectorL10n.messageReplyToSenderSentAnImage
}
func senderSentAVideo() -> String {
VectorL10n.messageReplyToSenderSentAVideo
}

How to resolve Abort trap: 6 ERROR - xcode 12

We have faced the issue of "abort trap 6" in Xcode 12. Due to this reason app not running using Xcode 12. We are using the swift 5 versions and jsqmessageviewcontroller objective c library.
Below errors getting in Xcode 12.
<unknown>:0: error: fatal error encountered while reading from module 'wwww'; please file a bug report with your project and the crash log
<unknown>:0: note: module 'wwww' full misc version is '5.3.2(5.3.2)/Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)'
top-level value not found
Cross-reference to module 'JSQMessagesViewController'
... JSQMessageMediaData
error: Abort trap: 6 (in target 'zapwww' from project 'zapwww')
If anyone has a solution please help us.
I had the same error in Xcode 12.5.1 and it seems to be a bug that has been fixed in the next beta. However there seem to be several issues that could cause this error. So my solution might not work.
For me the problem was very specific and only happened in the following scenario:
A method that returns an optional RealmObject (might be different in your case) is called.
The returned RealmObject has been assigned a variable.
Trying to unwrap the variable with guard let or if let using the same name for the new safely unwrapped variable.
The easiest fix is using different variable names or
Safely unwrapping the returned object directly without assigning it a variable.
Example that causes the error in my case:
class MyClass {
func returnObject() -> Object? {
return nil
}
func anyMethod() {
let myObject = returnObject()
guard let myObject = myObject else { return } // <-- works anywhere else but here.
}
}
Same example that fixed the error in my case:
class MyClass {
func returnObject() -> Object? {
return nil
}
func anyMethod() {
let myObject = returnObject()
guard let myNewObject = myObject else { return } // <-- Changed name of new variable here
}
}
I've seen people had this issue with other types, so it's not limited to the RealmObject type. But going through all guard let or if let with the same variable name is a good start.
I've also seen other people use fix it by cleaning the build folder or removing packages and reinstalling them. That didn't help for me though.
Problem: Abort Trap (In my case my code is working perfectly but when I trying to make an Archive file I got the "Abort Trap")
Solution:- Just Select Your Project from project Navegater (most Left Pane) Select Project > Select Targets > Build Settings > Swift Compiler - Code Genration > Optimization Level > Debug and Realease make "No Optimization [-Onone]"enter image description here
Flutter Specific
I had to set Optimization Level to No Optimization [-Onone] for Pods target.
Just Select Your Project from project Navigater (most Left Pane) Select
Pods > Build Settings > Swift Compiler - Code Generation > Optimization Level > Debug and Realease make No Optimization [-Onone]
for me i have just remove the library which cause the problem from pods file , then installed again will fix the problem
just had to run: 'pod update' to update my Realm pods and fixed it for me.

Swift - zip/unzip Data in memory without any file system interaction

My code needs to parse heaps of JSON files, and those files are hosted on GitHub and only available bundled as 1 single ZIP file. Because the ZIP file is only about 80 MB, I want to keep the entire unzipping operation in memory.
I'm able to load the ZIP file into memory as a Data? variable, but I'm not able to find a way to unzip a Data variable in memory and then assign the unzipped file/data to some other variables. I've tried using ZIP Foundation, but its Archive type's initializers take only file URLs. I didn't try Zip, but its documentation shows that it takes file URLs as well.
Here is my code:
import Cocoa
import Alamofire
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let zipURL = URL(string: "https://github.com/KSP-CKAN/CKAN-meta/archive/master.zip")!
AF.request(zipURL).validate().responseData { response in
var zipData: Data? = response.data
// Here I want to unzip `zipData` after unwrapping it.
}
}
}
I also looked into passing a Data variable off as a file, but failed to find a way to do it.
UPDATE (2019-12-01 05:00)
According to this pull request thread on ZIPFoundation, the feature I'm looking for will be included in the next release. I tried to use the feature's contributor's fork, but somehow Swift Package Manager wouldn't allow it.
Before finding this, I tried using Python's zipfile library through Swift-Python interoperability provided by PythonKit, but it didn't work out, because Foundation's Data in Swift can not be cast into a PythonObject type.
Apple's Compression framework also looked promising, but it seems to have a soft limit of 1 MB on compressed files. The compressed file I need is about 80 MB, way larger than 1 MB.
So far, ZIPFoundation is my most hopeful solution.
UPDATE (2019-12-01 06:00)
On another try, I was able to install microtherion's fork through Swift Package Manager. The following code should work:
import Cocoa
import Alamofire
import ZIPFoundation
... // ignoring irrelevant parts of the code
let zipURL = URL(string: "https://github.com/KSP-CKAN/CKAN-meta/archive/master.zip")!
AF.request(zipURL).validate().responseData { response in
// a Data variable that holds the raw bytes
var zipData: Data? = response.data
// an Archive instance created with the Data variable
var zipArchive = Archive(data: zipData!, accessMode: .read)
// iterate over the entries in the Archive instance, and extract each entry into a Data variable
for entry in zipArchive! {
var unzippedData: Data
do {
_ = try zipArchive?.extract(entry) {unzippedData($0)}
} catch {
...
}
...
}
}

Xcode. Image resources added to a test target are not copied into the tests bundle

I wrote some extension to UIImage class and want to cover it by Unit Tests. I added some image to test target and checked that it presents in the Copy Bundle Resources list.
In the test code I use a model object which pvovides me test data.
class TestConstants {
private static var bundle: Bundle {
return Bundle(for: UIImageExtensionsTests.self)
}
private static var birdImageURL: URL {
let path = self.bundle.url(forResource: "birds", withExtension: "png")
return path!
}
static var birdImageData: Data {
return try! Data(contentsOf: self.birdImageURL)
}
}
Unfortunately birds.png image is not located in test bundle, but another resource drm.txt presents.
I'm a bit confused is it the bug in Xcode?
By the way I downloaded Xcode 9.4 beta and there are the same behavior - images do not copy into the test bundle.
#UPDATE:
UIImageExtensionsTests is a test class presents in the same target with drm.txt and birds.png file
To get the appropriate Bundle use :
let bundle = Bundle.init(for: TestConstants.classForCoder())
My bad that I didn't read log carefully.
The issue was happen with "wrong" image founded in internet. Xcode couldn't proceed image, so it didn't copy it to test bundle.
While reading
/Users/igork/developer/gitlab/ios.gym-master/GymMasterTests/Resources/birds.png
pngcrush caught libpng error: Not a PNG file..
And this error doesn't fail building and running the target. So in my case the tests were starting :(

Error when migrating from swift 2.3 to swift 3

I just want to try Swift 3.0 in one of my projects and It is working fine in Swift 2.Xcode Version 8.3.1
Everything I fixed but getting error in FXForm library.Below is the error I am getting at the time of project compilation:
"_OBJC_CLASS_$__TtCC13Test11AppDelegate15FXFormVariables", referenced from:
objc-class-ref in FXForms.o
error: linker command failed with exit code 1 (use -v to see invocation)
I had passed the logged in user validation from App delegate like below:
//objective C class
#objc class loggedinUser : NSObject {
class func isUserLoggedIn() -> Bool { return userLoggedin! }
}
and used it in Objective-C class of FXForm like below:
if([loggedinUser isUserLoggedIn] == true){
// default to bottom
return CGPointMake(self.bounds.size.width/2, (self.bounds.size.height - (toast.frame.size.height / 2)) - style.verticalPadding - 120);
}
Please help in fixing the above error and thanks in advance.
This isn't a Swift 2 to Swift 3 refactoring problem.
It's a problem where you need to make sure the FXForms .m file is being compiled in your project. Do you include the .h & .m files in your project?
Go to the file inspector (the list of files in the left side) of your Xcode window, click on the FXForms.m file and make sure "Target Membership" is checked, like in the right red circle of this screenshot: