I'm trying to declare a function within another function. So here's part of my code:
ViewController.m
- (void)updatedisplay{
[_displayText setText:[NSString stringWithFormat:#"%d", counter]];
}
- (IBAction)minus1:(id)sender {
counter--;
updatedisplay();
}
ViewController.h
- (IBAction)minus1:(id)sender;
- (void)updatedisplay;
Which returned me the error of "Implicit declaration of function "..." is invalid in C99".
Result: http://i.imgur.com/rsIt6r2.png
I've found that people have encountered similar problem, but as a newbie I didn't really know what to do next. Thanks for your help! :)
Implicit declaration of function '...' is invalid on C99
You are not declaring a function; but a instance method, so to call it you must send it as a message to self;
[self updatedisplay];
EDIT
As #rmaddy pointed out (thanks for that) it is declared as instance method not class method. To make the things clear;
- (return_type)instance_method_name.... is called via 'self' or pointer to object instance.
+ (return_type)class_method_name.... is called directly on the class (static).
Problem
updatedisplay();
solution
[self updatedisplay];
cause
- (void)updatedisplay;
is a class method available for that class.So you have to call from the class to have the method available for you.
That is because you defined your function as a instance method, not a function.
So use it like
- (IBAction)minus1:(id)sender {
counter--;
[self updatedisplay]; // Change this line
}
write this way :
[self updatedisplay];
Related
Mehul has defined this method.
+(BOOL)isCameraDeviceAvailable
{
BOOL isCameraAvailable=NO;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] || [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
isCameraAvailable = YES;
}
return isCameraAvailable;
}
But I cannot declare it properly and am getting the error
Instance method '-isCameraDeviceAvailable' not found (return type defaults to 'id')
when I declare it as follows.
+ (BOOL)isCameraDeviceAvailable;
and then use it this way
if([self isCameraDeviceAvailable]){
}
I suppose I need to declare it in a different place or in a different way. Can you tell me how to do this?
[self isCameraDeviceAvailable]
is an instance method call, not a class method call.
You have to use it like this:
[MyClass isCameraDeviceAvailable]
+(BOOL)isCameraDeviceAvailable
The + at the beginning of the line makes this a class method, which means that you can only send isCameraDeviceAvailable to the class. If you want to use the method with an instance of that class, you'll need to declare it that way by using a - instead of +:
-(BOOL)isCameraDeviceAvailable
Or, as Kashiv explains, you can use the method as you have it by sending isCameraDeviceAvailable to the class instead. See Objective-C Classes Are also Objects for more information.
Can you spot the error, please? Why does the compiler think my function is undeclared? Thanks. -Rob
In .h file
-(int) getW:(int)xPosition;
In .m file
-(int) getW:(int)xPosition {
return (xPosition-58)/48;
}
in another procedure, the call to the function:
whichTile=[getW: xPosition] ; <----ERROR getW undeclared (first use in this function)
(xPosition and whichTile have been declared as integers, and in use earlier in the procedure). I tried it with (NSInteger), too (and a million other permutations!). Thanks for you help.-Rob
you have declared an instance method and called it without specifying the instance.
for example:
whichTile = [self getW:xPosition];
-or-
whichTile = [anObject getW:xPosition];
unlike other languages, self is not implicit when messaging.
So I am doing this to initialize my selector:
//In .h
SEL selectors[3];
//In .m
selectors[0] = #selector(rotate);
selectors[1] = #selector(discharge);
And here is the problem:
When I call this in my init method in Cocos2d like this:
[self performSelector:selectors[0]];
it works fine, but when I call this line of code in a method called moveThings which is invoked through the schedule ([self schedule:#selector(moveThings:)]) at the end of my init method in Cocos2d it gives EXC_BAD_ACCESS. What is the problem with scheduling things?
UPDATE:
I have found there is a problem with the rotate function (the function being stored in selector[0]). Here it is:
-(void)rotate:(ccTime)delta {
if (((CCSprite *)[creature objectAtIndex:0]).rotation < 360) {
((CCSprite *)[creature objectAtIndex:0]).rotation++;
}
else {
((CCSprite *)[creature objectAtIndex:0]).rotation++;
}
}
If I comment the contents of the method out it works fine when called through moveThings and init.
If I change the methods contents with:
((CCSprite *)[creature objectAtIndex:0]).rotation++;
It fails... But, again, I would like to state that all of these things do work if I call it in my init method, even call it twice in a row, but it will not work (except when I take out the contents of the rotate method) if I call it through the moveThings: method which is being invoke through the schedule method it fails.
Further update:
If I call:
((CCSprite *)[creature objectAtIndex:0]).rotation++;
In moveThings (which is being, as I've said before, invoked by the schedule:(SEL) method) it fails. Where as long as it is not invoked through a method that is the called by schedule it works.
The problem is that when you call performSelector there are only two options:
have your selector take no arguments and leave the ":" off the #selector(foo) definition.
have your selector take either one or two arguments which both must be an NSObject or subclass
it is the latter that is messing you up here I suspect.
Here are the three forms of performSelector:
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
You'll note that the withObject arguments are all of type id which is an Objective C object.
The selector you're trying to use takes a ccTime which is a float and not an Objective C object as it's param and that is why things are crashing:
-(void)rotate:(ccTime)delta;
One option is to make a wrapper method that takes a wrapped ccTime and unwraps it and calls the rotate:(ccTime) method:
- (void) wrappedRotate: (NSNumber*) inDelta
{
[self rotate: [inDelta floatValue]];
}
then use
selectors[0] = #selector(wrappedRotate:);
and then call via:
[self schedule: #selector(moveThings:)]); // cocos2d schedule
...
- (void) moveThings: (ccTime) dt
{
[self performSelector: selectors[0] withObject: [NSNumber numberWithFloat: dt]];
...
}
One reason you are getting confused is because Cocos2d is using #selector in somewhat more complicated ways (see CCScheduler.m in the CCTimer::initWithTarget:selector:interval: and CCTimer::update: method in particular).
disclaimer: code typed into SO so not checked with a compiler, but the essence of what you need should be here.
One problem for sure is that you are using a variable declared inside a .h while initializing it inside the relative .m. According to the linking I'm not sure that just one variable selectors will exist (so that different files that include .h will have different versions).
First of all I suggest you to try adding the keyword extern to have
extern SEL selectors[3];
to tell your linker that it is initialized inside the relative .m and to use just that one.
I think your problem stems from your method definition which is - (void)rotate; and not - (void)rotate:(ccTime)dt;
You should adjust your selectors likewise.
If your method does not have any arguments then do not use a colon in your selector call.
// Requires #selector(foo:)
- (void) foo:(id)sender;
// Requires #selector(foo)
- (void) foo;
i've got this method:
-(void)reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent
now I want to call this method in another method like:
[self thisMethod];
But how can I do this with a method that has local declarations in it?
thank you
It doesn't matter if a method has local declarations in it, that's completely normal. Have you tried calling [self thisMethod];? Does it crash?
I am attempting to simply call a method within the same class, e.g.
-(void) createRequest: (NSString*)urlFormatted {
...
}
-(void) sendData {
...
[createRequest request]; <- Error occurs here.
}
Error: 'createRequest undeclared'
Whether is was necessary or not, I also defined createRequest in associated header file, e.g.
-(void) createRequest: (NSString*)urlFormatted;
I'm very new to objective-c. Where am I going wrong?
You call the function like this
[self createRequest:request];
You can call it with:
[self createRequest:request];
It helps to think in terms of objects sending each other messages. In this case, your object is sending itself a message to perform a request on an NSString.