How to reference property of static library - iphone

I'm referencing another project's target static library. I successfully followed instructions from this site. Below is the project using a class named FileIO from the library.
I create a FileIO object 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?

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.

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.

Related

Objective-C property access: unrecognized selector sent to instance

I am new to objective-c, but coding for many years now. Somehow I don't get warm with Objective-C. I searched google and stackoverflow, but I think my problem is just to simple and stupid that no one has asked this yet.
My Code is based on DateSelectionTitles Example. http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html
I have an NSManagedObject
#interface Event : NSManagedObject
#property (nonatomic, retain) NSDate * date;
...
// Cache
#property (nonatomic, retain) NSString * primitiveSectionIdentifier;
All prperties are defined in my datamodel, except the primitiveSectionIdentifier (as in the apple example)
But when I call
NSString *tmp = [self primitiveSectionIdentifier];
I get the Exception
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Event primitiveSectionIdentifier]: unrecognized selector sent to instance 0x74850c0'
To put it simple:
Event *foo = [[Event alloc] init];
if (foo.primitiveSectionIdentifier) {
NSLog(#"YEAH");
}
throws the same exception. So I basically want to check if primitiveSectionIdentifier is nil. But when I access the property, it throws an exception? Do I need to alloc each property before I can check if it has a value?!
Which of the Objective-C basics am I not getting here?
Thanks a lot for responses!
There is only one way how this can happen without compiler warnings - you must have written #dynamic primitiveSectionIdentifier; in your implementation file. This means that you don't want to define the method because you believe it is already defined somewhere else.
You are using a NSManagedObject, do you know how it works? You declare methods without implementation (putting #dynamic in the implementation) and when the method is called, it is not found and a special handler [NSObject doesNotRecognizeSelector:] is called instead. This handler checks the Core Data model whether an attribute for the given selector exists and if it doesn't, it throws the exception you are seeing.
So, the problem might the caused by the fact that primitiveSectionIdentifier is not declared in your model.
You are using an older example program, which uses a different style of memory management; if you are compiling under the iOS 5 or 6, that may be causing the problem.
Try
NSLog(#"primitiveSectionIdentifier = %#", self.primitiveSectionIdentifier);
If it doesn't give you the string you are looking for then the problem is likely in that the string object was never initialized and is still set to nil. In that situation, the code would compile, but sending a selector to a nil pointer would throw up an exception.

CoreDataGeneratedAccessor method giving "unrecognized selector sent to instance" error

When i call the method:
- (void)removeObjectFromMediaAtIndex:(NSUInteger)idx;
which is one of the default methods in a file created as a core data object, i'm getting an error of unrecognized selector sent to instance. Anybody know why this might be happening?
Ensure that your NSManagedObject sublcass instance was created using an NSManagedObjectContext and not directly. Instead of leveraging #synthesize for properties, NSManagedObject sublcasses leverage the #dynamic keyword which indicates the accessors will be created at runtime - in this case, by the NSManagedObjectContext. They will not be there if you create the object instance using something like alloc]init];
It is a notorious Core Data bug. It is almost 2-year old but sadly it is still there. See this post: Exception thrown in NSOrderedSet generated accessors.
It sounds like you may have altered your data model without altering the classes, or vice-versa. Or perhaps one of your team members did (my team quickly learned about this danger). Another possibility is that the reference you are using is not actually the class you think it is. Sometimes if you overrelease an object, another object will occupy the previous memory space but it will not be the correct class.
However, this doesn't look like a default method. The default methods I am used to seeing are add object, remove object, change to a new NSSet, and one more that I can't quite remember off the top of my head. However, if you got the CoreData object to use an NSArray instead it would make sense.

unrecognized selector sent to class in iPhone

this is my class
#import "Year2011.h"
#implementation Year2011
- (void)Men:(double)speed{
if (ramspeed <= 180000) {
cal = 0;
}
here i have HelloViewController class how can i call Year2011 class and Men mathod.
now i tried like this.
#class Year2010;
IBOutlet Year2010 *Year2010;
calling
double speed=([anualIncome.text doubleValue]);
[Year2010 Men:income];
this one is showing unrecognized selector sent to classerr.guide me i'm new to objective c.
[Year2010 Men:income];
From the code you've shown us, the Year2010 class doesn't have a method Men:. Why would you expect to be able to call it?
Maybe change the type of the variable Year2010 to Year2011?
Also, dear god, don't name your instances the same as your classes. This will be impossible to read and understand when you come back from lunch, let alone months from now.
You are mixing up Class name and Instance name. You also seem to have problems understanding the scope of an instance.
-1- change your instance name to lowercase initial - that is IBOutlet Year2010 *year2010;
-2- change your method names to lowercase initial - that is - (void)men:(double)speed
The compiler currently assumes that Men is a static class method, but you never defined it as such, hence the error.

Terminating app due to uncaught exception 'NSInvalidArgumentException

Ok here is an interesting one... I am receiving the
warning: Characters may not respond to 'addHeroType:atX:atY:fromMobTable'
although I have the functions defined properly as far as I can see so how about another pair of eyes?
Here is the definition in Characters.h
-(void) addHeroType:(int)newMobType atX:(int)x atY:(int)y fromMobTable:(Mobdefs*)newMobTable;
Here is the function in the Characters.m
-(void) addHeroType:(int)newMobType atX:(int)x atY:(int)y fromMobTable:(Mobdefs*)newMobTable
and here is the call I am making in another class call heroFactory (Characters.h is included in heroFactory):
[Characters addHeroType:1 atX:location.x atY:location.y fromMobTable:newMobTable];
But this line causes the app to terminate due to uncaught exception - checking the debugger I see "NSObject Does Not Recognize Selector"
I am convinced that the issue is the wierdness of why I am seeing a warning that the Character class may not respond to the function call as written even though it matches identically to the definition.
Any help greatly appreciated.
-(void) addHeroType:(int)newMobType atX:(int)x atY:(int)y fromMobTable:(Mobdefs*) newMobTable
That is declared as an instance method.
[Characters addHeroType:1 atX:location.x atY:location.y fromMobTable:newMobTable];
But you are calling it on the class as a class method.
So either, declare the method as a class method
+(void) addHeroType:(int)newMobType atX:(int)x atY:(int)y fromMobTable:(Mobdefs*) newMobTable
Or call it on an instance
Characters *instance = [[[Characters alloc] init] autorelease];
[instance addHeroType:1 atX:location.x atY:location.y fromMobTable:newMobTable];
Depending on the scope your method requires, of course.

Iphone Core Data Internal Inconsistency

This question has something to do with the question I posted here: Iphone Core Data crashing on Save
however the error is different so I am making a new question. Now I get this error when trying to insert new objects into my managedObjectContext:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '"MailMessage" is not a subclass of NSManagedObject.'
But clearly it is:
#interface MailMessage : NSManagedObject { ....
And when I run this code:
NSManagedObjectModel *managedObjectModel = [[self.managedObjectContext
persistentStoreCoordinator] managedObjectModel];
NSEntityDescription *entity =[[managedObjectModel entitiesByName]
objectForKey:#"MailMessage"];
NSManagedObject *newObject = [[NSManagedObject alloc] initWithEntity:entity
insertIntoManagedObjectContext:self.managedObjectContext];
It runs fine when I do not present an MFMailComposeViewController, but if I run this code in the
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
method, it throws the above error when creating the newObject variable.
The entity object when I use print object produces the following:
(<NSEntityDescription: 0x1202e0>) name MailMessage, managedObjectClassName MailMessage,
renamingIdentifier MailMessage, isAbstract 0, superentity name (null), properties {
in both cases, so I don't think the managedObjectContext is completely invalid. I have no idea why it would say MailMessage is not a subclass of NSManagedObject at that point, and not at the other.
Any help would be appreciated, thanks in advance.
Class MailMessage could be implemented in a library or somewhere else in the framework. As Objective C does not implement namespaces, one of the two will be used. Which one is undefined. Try giving your class a different name to quickly resolve the issue.
Look for a message like this from the debugger. It will confirm what Benjamin says.
Class MailMesssage is implemented in both /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/System/Library/PrivateFrameworks/Message.framework/Message and /Users/home/Library/Application Support/iPhone Simulator/4.2/Applications/FFFFFFFF-FFFF-0000-0000-AAAAAAAAAAA/Projects.app/Projects. One of the two will be used. Which one is undefined.
Try resetting the Simulator or uninstalling the application from your device. Often the NSInternalInconsistencyException has to do with problems with changing the datamodel and the database not being updated accordingly.
I was able to workaround this by creating the MailMessage object before presenting the modal view controller. Once the MailMessage object was already created, saving changes did not present a problem. A strange workaround and not addressing the actual problem as far as I know, but it works.