views:

172

answers:

2

I'm referencing another project's target static library. I successfully followed instructions from this site: http://tinyurl.com/cleuhw. Below is the project using a class named FileIO from the library. I create a FileIO object (fileObj) and assign a string to its name property. Then I get a __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__ on the assignment of name.. In applicationDidFinishLaunching I do this:

fileObj = [[FileIO alloc] init];
fileObj.name = @"test";

and this is in the .h file:

@class FileIO;

@interface Nav1AppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;
UINavigationController *navigationController;
FileIO *fileObj;
}

In the library, FileIO is a simple class with name in it. I have also tried [fileObj setName:@"test"] but get the same results. Here's the stack trace:

2009-04-01 20:37:17.721 NavNew[81425:20b] *** -[FileIO setName:]: unrecognized selector sent to instance 0x5219b0

2009-04-01 20:37:17.723 NavNew[81425:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[FileIO setName:]: unrecognized selector sent to instance 0x5219b0'

2009-04-01 20:37:17.724 NavNew[81425:20b] Stack: (

2454561035,
2461146683,
2454590218,
2454583564,
2454583762,
11275,
816111650,
816149355,
2455110190,
2454063909,
2454064344,
827745792,
827745989,
816114848,
816160924,
11128,
10982

) (gdb)

I have discovered this is a problem only with instance members (property or method). Static methods work fine. I also opened the library .a file in the hosting project. I don't see the instance property anywhere in it.

Any suggestions on what I'm doing wrong?

A: 

For some reason, the FileIO object that you have doesn't respond to the setName: message, so it's throwing an NSInvalidArgumentException when you try to send that message. My best guess is that the shared library isn't being loaded properly for some reason, so the implementation of setName: isn't getting loaded, so the runtime gets confused and thinks that setName: isn't implemented.

I'm not sure how to go about fixing this, but it does appear that the [[FileIO alloc] init] is succeeding (or at least to the point of not throwing an exception), so something is working at least. To get the list of messages that are allowed, you can try the following:

unsigned int methodCount;
Method *methods = class_copyMethodList(object_getClass(fileObj), &methodCount);
for(unsigned int i = 0; i < methodCount; i++)
    NSLog(@"Method %u: %s", i, sel_getName(method_getName(methods[i])));
free(methods);

For information on the various Objective-C runtime methods, see the Objective-C 2.0 Runtime Reference.

Adam Rosenfield
Thanks but can't even get close to running that code. Method is in runtime.h but where is runtime.h?
4thSpace
Another edit. This only happens with instance members. Hang on...isn't this is a "static" library?
4thSpace
+1  A: 

In the instructions you used to "import" the static library, the author describes several cases of strange errors occurring somewhat randomly, which is why I'd suggest a different approach: using an Xcode cross-project reference and shared build output directory. Here's a link to a tutorial with screenshots: http://www.clintharris.net/2009/iphone-app-shared-libraries/

I've been using this strategy for several months with multiple projects and static libraries--it's been great and I haven't experienced any problems. The other really nice perk is that you use an Xcode environment variable to reference the project with the static library (including the header files); this makes the solution really flexible if you have multiple developers working on the same project, need to move directories around, etc.

Clint Harris