PDA

View Full Version : new in objective c (EXC_BAD_ACCESS) problem




kurushetra
Mar 26, 2009, 09:45 AM
hello i am new in objective c and i have a question ¿why i get a (EXC_BAD_ACCESS) when i call the method "primera"?
the code is this:
#import <Cocoa/Cocoa.h>

@interface cont : NSObject {
IBOutlet id text;
}
- (IBAction)boton:(id)sender;
-(void)primera;

@end
and:
#import "cont.h"

@implementation cont
NSString *mensaje;
- (IBAction)boton:(id)sender {
mensaje = (@"hola");
cont *con;
[con primera];
}
-(void)primera{
[text setStringValue:(mensaje)];
}

@end
i know i can run the action from (- (IBAction)boton:(id)sender) but this is only one example. why is not possible to call another method from (-(IBAction)boton:(id)sender) also if the method "primera" is with (+) give me a warning (instance variable "text" accesed in class method ) ¿can somebody tell me how i can call the method "primera" from ((IBAction)boton:(id)sender)?



robbieduncan
Mar 26, 2009, 09:56 AM
What is con supposed to be? In your code it's a pointer to a cont, but it's never initialised so it's just pointing at no-where. It's certainly not pointing at the instance of the class that the method is within if that's what you were hoping.

Go back to basic C. If I declare a pointer and then never set where it points to what happens when I deference that pointer? The program crashes. Same here. In all seriousness you need to ensure you know your C as this is a basic-level failure.

I suggest you read Apple's Introduction to Objective-C (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html). You should have read and understood all of this before trying to write any code. It will tell you how to call methods on the same object as the current method is operating on, specifically in this section (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-BAJHDGAC).

lee1210
Mar 26, 2009, 10:03 AM
hello i am new in objective c and i have a question ¿why i get a (EXC_BAD_ACCESS) when i call the method "primera"?
the code is this:

#import <Cocoa/Cocoa.h>

@interface cont : NSObject {
IBOutlet id text;
}
- (IBAction)boton:(id)sender;
-(void)primera;

@end
and:
#import "cont.h"

@implementation cont
NSString *mensaje;
- (IBAction)boton:(id)sender {
mensaje = (@"hola");
cont *con;
[con primera];
}
-(void)primera{
[text setStringValue:(mensaje)];
}

@end

i know i can run the action from (- (IBAction)boton:(id)sender) but this is only one example. why is not possible to call another method from (-(IBAction)boton:(id)sender) also if the method "primera" is with (+) give me a warning (instance variable "text" accesed in class method ) ¿can somebody tell me how i can call the method "primera" from ((IBAction)boton:(id)sender)?

Just say:
[self primera];

Right now you are declaring a new cont *, not initializing it, then trying to send it the message primera. self is the instance of cont you actually want to send that message to.

-Lee

Edit: I won't erase this since i've already posted it, but robbieduncan's point is good... this is not particularly advanced, and you should understand why things went so wrong, not just use the code above and continue without learning anything.

kurushetra
Mar 26, 2009, 03:41 PM
thanks i will do what you say...