Unit testing a static library with RestKit - ios

I'm attempting to follow along with the RestKit unit test guide ( https://github.com/RestKit/RestKit/wiki/Unit-Testing-with-RestKit ) except that my project is a static library instead of an app.
Here is the test I've written:
- (void)testMappingOfDate
{
id parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:#"plan.json"];
RKMappingTest *test = [RKMappingTest testForMapping:[self planMapping] object:parsedJSON];
[test expectMappingFromKeyPath:#"plan.date" toKeyPath:#"date"];
STAssertNoThrow([test verify], nil);
}
When I attempt to run the test I receive this error on the first line of the test:
error: testMappingOfDate (api_clientTests) failed: -[NSBundle parsedObjectWithContentsOfResource:withExtension:]: unrecognized selector sent to instance 0x1765c40
It seems like its not finding the NSBundle category defined by RestKit, but my test target header search path is set to "$(BUILT_PRODUCTS_DIR)/../../Headers" and I've verified this path includes NSBundle+RKAdditions.h which contains the supposed "unrecognized selector".
Is there something I'm missing here?

You are trying to include a category within your binary that comes from a library. To get that accomplished you will need to add the following to your (Unit-Test-Target's) build settings.
Other Linker Flags: -ObjC
From Apple's QA:
Objective-C does not define linker symbols for each function (or
method, in Objective-C) - instead, linker symbols are only generated
for each class. If you extend a pre-existing class with categories,
the linker does not know to associate the object code of the core
class implementation and the category implementation. This prevents
objects created in the resulting application from responding to a
selector that is defined in the category.
Solution:
To resolve this issue, the static library should pass the -ObjC option
to the linker. This flag causes the linker to load every object file
in the library that defines an Objective-C class or category. While
this option will typically result in a larger executable (due to
additional object code loaded into the application), it will allow the
successful creation of effective Objective-C static libraries that
contain categories on existing classes.

The error means that the "unrecognized selector" issue is at runtime. The compiler and NSBundle+RKAdditions.h do not give this error they would at compile timr.
The issue is that the code that has #implementation NSBundle(RKAdditions) is not linked into your app. So you need to add this to your build

Related

No known class method for selector 'indexPathForRow:inSection:'

I'm trying to import/use this project in my Swift application, but the compiler throws tons of error with following message:
No known class method for selector 'indexPathForRow:inSection:'
Following is the typical code which generates the above error:
if ([self tileForIndexPath:[NSIndexPath indexPathForRow:row inSection:i]].empty) {
...
}
I can able to run the downloaded project as an standalone application without having any problem, though.
I've also added all the frameworks to my main application - those were used and installed to the said project, but that didn't help. My main project's deployment target is 10.0.
indexPathForRow:inSection: is a static constructor method that creates NSIndexPath instances. It is defined in UIKit:
https://developer.apple.com/documentation/foundation/nsindexpath/1614934-indexpathforrow?language=objc
In this project the author imports UIKit in the pch file:
https://github.com/austinzheng/iOS-2048/blob/7c0840a0f7bd77b01d6a36778a253f8f4b2e6529/NumberTileGame/NumberTileGame/NumberTileGame-Prefix.pch#L14
So you have 2 options: either setup a pch file for Objective-C code in your project (create the file and add to Xcode project build settings), or in each source file where you get this error add #import <UIKit/UIKit.h>.

Installing third party Objective-C library within Swift 3 / Xcode 8 (SharkORM)

Im pretty new to Xcode/Swift and want to install a third party library(SharkORM).
I drag'n'dropped the folder "SharkORM" into XCode and selected "Create groups". Then i created a file "Swift-Bridging-Header.h" and typed in #include “SharkORM.h” as described in the documentation. When i hold CMD and click on it it leads me to the interface declaration(good!?). Now when i try to use it: class MyClass: SRKObject { ... } i get an error: "Use of undeclared type 'SRKObject'". But i can CMD+click on it which leads me to the interface declaration again.
I tried to install with Cocoapod, too, with no success.
As posted on GitHub, it sounds like the header file you created has not been added to the build settings as the chosen bridging header.
That is the most likely scenario leading to the object not being defined in your swift code.
Check, if SharkORM.h contains SRKObject declaration. If not, find header file with it and place it to bridging header too

iOS static library, cannot access some class methods

I'm building a static library in iOS. after importing that library in my project, I added -ObjC in Other linker flags. But when I call the class methods(currently 3 available), 2 of them are being called and executed properly, but the last one is getting this error: "+[RankConferenceLib joinConferenceWithName:]: unrecognized selector sent to class 0x5044dc".
This is my Header file of library
#interface RankConferenceLib : NSObject
+(void)initEnvironment;
+(void)orientationChange;
+(void)joinConferenceWithName:(NSString *)name;
#end
in .m file of library
+ (void)joinConferenceWithName:(NSString *)name
{
//....codes
}
and in my project I'm calling them
- (IBAction)join:(UIButton *)sender {
[RankConferenceLib joinConferenceWithName:#"User"];
}
Please tell me what I'm missing here. This is my first static library. I've searched but could not find any help which is similar as my situation here. Please mention what else you need to know.
Thank you.
I have checked this and for me it's working fine without any linker flags added.
The only one error possibility is something happened inside the + (void)joinConferenceWithName:(NSString *)name
Write a log inside the joinConferenceWithName to printout the parameter name and make sure this is calling and the problem is occurring inside that method.
+ (void)joinConferenceWithName:(NSString *)name
{
NSLog(#"the name is: %#", name);
}
Finally, make sure that you added the latest modified static library into your project.
You can download the working sample from here
Try Running using the -all_load linker flag
Apple Documentation
Stack Overflow Answer

xCode: Objective C: duplicate symbol error

In file CarArray (without any extension) I have the array like that (this is a very simplified version):
NSString *cars[5][3] = {
{#"1A", #"1B", #"1C"},
{#"2A", #"2B", #"2C"},
{#"3A", #"3B", #"3C"},
{#"4A", #"4B", #"4C"},
{#"5A", #"5B", #"5C"}
}
Now I want to read the data from the array in multiple files so I simply use #import "CarArray"
And I use a loop to read the data. Then I get such an error:
duplicate symbol _cars in:
/Users/Adam/Library/Developer/Xcode/DerivedData/Auto_Center-hgjlsqanvyynncgyfzuorxwchqov/Build/Intermediates/Auto Center.build/Debug-iphonesimulator/Auto Center.build/Objects-normal/i386/DetailViewController.o
/Users/Adam/Library/Developer/Xcode/DerivedData/Auto_Center-hgjlsqanvyynncgyfzuorxwchqov/Build/Intermediates/Auto Center.build/Debug-iphonesimulator/Auto Center.build/Objects-normal/i386/ModelListViewController.o
ld: 1 duplicate symbol for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I solve that problem?
Now I want to read the data from the array in multiple files so I simply use #import "CarArray"
This is an incorrect way of accessing array data from multiple places, because it creates multiple definitions in situations when you use the file more than once.
One way of sharing an array would be providing a header with a declaration, and a .m file with an implementation:
CarArray.h:
extern NSString *cars[5][3];
CarArray.m:
#import "CarArray.h"
NSString *cars[5][3] = {
{#"1A", #"1B", #"1C"},
{#"2A", #"2B", #"2C"},
{#"3A", #"3B", #"3C"},
{#"4A", #"4B", #"4C"},
{#"5A", #"5B", #"5C"}
}
Use #import "CarArray.h" in files from which you wish to use cars.
Another alternative would be making a class to wrap your global variable, and providing a class method for accessing the array.
You should declare your array in a header file ("CarArray.h") but define and initialize it with values in a separate implementation file ("CarArray.m").

Using an Obj-C sub-project in a Swift application

My team has a few full Obj-C libraries we like te reuse in our projects. To do so, we usually set up a git submodule, and then add it to the xcode project as a sub-project (Using target dependency, link binary with library and updating the User Header Search Paths)
So far it's only been done in full Obj-C projects, and I'm now trying to use it in a Swift project but with very little success so far. I tried to add the briding-header file, referenced it in the project and filled it like so :
#import "MyLibraryHeader.h"
With the target header being in the User Header Search Paths.
It lets me build, but when using it in my Swift files:
let test = MyLib();
let secondTest = MyLib.classMethod1("some_arguments");
I get an EXC_BAD_INSTRUCTION on secondTest, and the following logs in the debugger:
(lldb) po test
error: <EXPR>:1:1: error: use of unresolved identifier 'test'
(lldb) po secondTest
error: Error in auto-import:
failed to get module 'MyProject' from AST context:
/Users/siegele/Sources/MyProject_iOS/MyProject/Classes/MyProject-Bridging-Header.h:12:9: error: 'MyLibraryHeader.h' file not found
#import "MyLibraryHeader.h"
^
failed to import bridging header '/Users/siegele/Sources/MyProject_iOS/MyProject/Classes/MyProject-Bridging-Header.h'
Found the following question with no answer : Xcode 6 crashing when using Objective-C subproject inside Swift
Any help would be appreciated.
I followed the HockeyApp tutorial that can be found here:
http://support.hockeyapp.net/kb/client-integration-ios-mac-os-x/integrate-hockeyapp-for-ios-as-a-subproject
In the end, I was on the right tracks but messed up the Header Search Paths:
Add subproject xcodeproj to the workspace
On the main project : Link binary with library, and add the subproject product library (Bonus points : add it as a target dependency too)
Update Header Search Paths (not User Header Search Paths) accordingly
Import your Library.h in the main project Bridging-Header.h file
What threw me off for a while is that the Swift Compiler setting for the Objective-C Bridging Header was not set automatically. Check the Build Settings for your target and ensure the "Objective-C Bridging Header" setting is not blank.

Resources