I develop a static library and build my lib.a.
When I use this library in a iOS project (iPhone app built with -ObjC and -all_load flags for linker), I get this error at runtime :
unrecognized selector sent to instance
This error occurs when I try to call a class method.
+ (MyObject *) GetSingleton;
For information, I don't get error when I call an instance method.
- (void) Log;
Have you got an idea of the problem ?
When you create your singleton, try this:
+ (MyObject *)GetSingleton {
static MyObject* singletonInstance;
#synchronized(self) {
if (!singletonInstance)
singletonInstance = [[MyObject alloc] init];
}
return singletonInstance;
}
Hope that helps.
Hum, I fixed the problem re-creating my project !
I don't know why, my project made bad linking for class method, and not for instance method.
Now, with new project and linkg to my static library, all is OK at runtime.
Maybe it was a problem because of multiples static libraries I built, with probably a bad cache or dependencies...
Thanks for your answers developers !
Related
So, I made a framework in swift and at first I wanted to use a singleton class. I built it and put the .Framework file into a new project to test it. Than I got this error:
'getInstance' is inaccessible due to 'internal' protection level
. I tried looking for anyone with the same problem, but nothing I found worked. It might be because its a framework. After hours of meaningless searching, I gave up on the singleton and I got almost the same error with a normal class.
'mySDK' initializer is inaccessible due to 'private' protection level
I tried making the class public, the initializer public, but nothing seems to change. Anyone experienced any problem like this? I never worked on frameworks before, so maybe its the obj-c header that have to be modified. If you need any more information, please just ask.
Thank you all, in advance.
Edit:
This is the getInstance func. I wrote it only, because the mySDK.myInstance seemed to give the same error.
static let myInstance = mySDK()
public static func getInstance() -> mySDK {
return myInstance
}
I don't know what caused the error, but I managed to fix it by creating a new project, than copy pasting the code from the old to the new.
I found the source of the problem. If I turned off the build active architecture only option in the build settings of the framework, It gave me this error.
You can create a singleton class in a framework like this. Please make the public Class and public static getInstance function.
public class ClassName {
// Singleton instance
private static var instance : ClassName?
/********************* Singleton Instance ************************************/
public static func getInstance() -> ClassName {
if (instance != nil) {
return instance!
}
// Initialize instance.
instance = ClassName()
return instance!
}
}
I have an NSString category class (NSString+URLEncoding.h).
I am running into and unknown selector crash, because the string I am calling the category method has been optimized into an NSCFConstantString by iOS.
-[__NSCFConstantString URLEncodedString]: unrecognized selector sent to instance 0x290174
I learned of the NSCFConstantString vs. NSCFString optimizations in iOS 5 from:
http://www.cocoanetics.com/2012/03/beware-of-nsstring-optimizations/
Is anyone aware of how I can get the NSString category to include the Constant strings or even force the var to be an NSString/NSCFString and not an NSCFConstantString?
Cheers,
Z
-edit-
Linker flags -ObjC -all_load are both already implemented
NSString+URLEncoding.m is included in the targets compile sources
NSString+URLEncoding.m implements the URLEncodedString method.
Checked for zombies.
I am adding a sharing service to ShareKit 2.0
header:
#interface NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString;
implementation:
#implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString
{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:#&=+$,/?%#[]"),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
There's an issue with the linker that can cause its dead-code stripping to completely omit any object files that only contain obj-c categories (or that are otherwise unreferenced). Theoretically passing the -ObjC flag to the linker should fix this, but that doesn't seem to always work. You can work around this issue by providing the -all_load linker flag, which will cause the linker to always link in all object files.
Note that you might have to set -all_load on the parent project if your category is part of a sub-project or library that you-re including somewhere.
Update: I believe -ObjC is reliable now and has been for years so you can stop using -all_load for this issue.
Just spent 30 minutes figuring out exactly the same issue. After fiddling with linker I found out that the category wasn't present in Compile Sources list in my target's Build Phases. Be sure to check it's there.
__NSCFConstantString is a subclass of NSString, so any categories on NSString apply to __NSCFConstantString too.
Either you're not linking in your category, or your category doesn't define a URLEncodedString method in its #implementation.
I'm trying to getting started with Typhoon Framework and I have a problem.
My code is:
#interface CustomAssembly : TyphoonAssembly
the CustomAssembly don't have more code.
When I run
[CustomAssembly new] activate];
the app crash with:
-[CustomAssembly typhoonPropertiesUpToParentClass:]: unrecognized selector sent to instance 0x15e86610
How can I fix it?
What's the problem with this code?
Finally I get the answer.
The problem was that I needed to add Obj-c in "Other linker flags"
I removed this flag when I added Parse framework.
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
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