Objective-C performSelector when to use colon? - iphone

Do you only postfix the method name with a : if you are calling a foreign object?
For some reason
[self performSelector:#selector(myMethod:) withObject:nil afterDelay:5];
Does not work but
[self performSelector:#selector(myMethod) withObject:nil afterDelay:5];
Does!
EDIT:
Declared in the implementation of a class but not the interface.
- (void)myMethod
{
// Some stuff
}

The colon represents a method argument. Since myMethod takes no arguments its selector can't have a colon. If you had multiple arguments like this...
- (void)myMethod:(id)method object:(id)object enabled:(BOOL)bool {
// Some Stuff
}
... the selector would be #selector(myMethod:object:enabled:)

In Objective-C the colons are part of the method name. That is, myMethod and myMethod: are distinct selectors (and in your case, only the latter exists).
For instance, for a method declared like:
-(void)doSomethingWithFoo:(int)foo andBar:(int)bar;
The selector is doSomethingWithFoo:andBar:.

Related

How to call a non-void function? Xcode

How do I call a non-void function? Normal [self methodName]; works. But how do I do this for a method that returns an NSString. I keep getting an error. For example:
+ (NSString *)formulateYQLRequestFor:(NSArray *)tickers
How do I call this? [self formulateYQLRequestFor]; gives me an error.
Sorry about the formatting, for some reason safari won't let me indent.
Thanks!
+ designates a class function. You call it with the class name, not an instance.
Instead of:
[self formulateYQLRequestFor:myArray];
Do this:
[MyClassName formulateYQLRequestFor:myArray];
Alternatively, you can do this:
[[self class] formulateYQLRequestFor:myArray];
You don't have to do anything with the return value if you don't want to. At least with ARC, the return value will be automatically released. However, since it's unlikely that the function does anything on its own, you probably should do something with the return value:
NSString *returnValue = [[self class] formulateYQLRequestFor:myArray];
// Do something with returnValue
Finally, if you want to call the function without passing in an array, you still need the array parameter, but perhaps the function will accept nil for the array:
NSString *returnValue = [[self class] formulateYQLRequestFor:nil];
There are two problems with your call to [self formulateYQLRequestFor];
Firstly, the method takes a parameter, which you haven't provided. Because of this, the compiler is looking for the method called formulateYQLRequestFor instead of formulateYQLRequestFor: This is significant, because the : is part of the method name in Objective-C. So you are trying to call a method that doesn't exist.
Secondly, self is sending a message to an instance of your class. The + in the method signature indicates that you have a class method, and so self does not respond to the method you are trying to call.
The correct way to do this is:
NSString *resultString = [[self class] formulateYQLRequestFor:someArray];
where someArray is a valid NSArray parameter.
I don't know what - (NSString *)formulateYQLRequestFor: does with the NSArray, but if it isn't necessary you can just call [self formulateYQLRequestFor:nil];. Alternatively you can call it with an empty array [self formulateYQLRequestFor:[NSArray array]];.

How can I use [self performSelector: withObject: afterDelay:] method in +(void)classMethod

How can I use [self performSelector: withObject: afterDelay:] method in +(void)classMethod?
I've got a sprite defined in the following:
+(void)classMethod
{
CCSprite * sprite = [CCSprite spriteWithFile:#"sprite.png"];
//and hope afterDelay3.0second remove this sprite
[self performSelector:#select(clean:) withObject:sprite afterDelay:3.0];
}
+(void)clean:(CCSprite *)sprite
{
[sprite removeFromSuperView];
}
Technically self refers to the Object of given class, In your case you are not creating an Object so you won't be able to call Object methods from static method.
Visual
A quick test shows that, If you method is declared instance level then you will not be able to access it from your class method.
In your case,
+(void)classMethod
{
}
is a class method but,
[self performSelector:#selector(clean:) withObject:sprite afterDelay:3.0];
is an instance method of NSObject, That is why you are not able to call from your class method.
Apple Doc,
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
That means you are accessing instance method from class method, which is not allowed.
I don't think you can use this Instance method in another Class method.
If you want to do some delay action, try NSTimer timerWithTimeInterval:target:selector:userInfo:repeats: instead.

Difference between the use of withObject:self and withObject:nil

I have been wondering about the following lines of code
[self performSelector:#selector(myMethod) withObject:self afterDelay:1.0];
[self performSelector:#selector(myMethod) withObject:nil afterDelay:1.0];
Whats the difference between the above 2 lines of code.
When should we set object as nil and when should we set object as self?
In most cases I have noticed the object to be set as nil.
In the example you listed you won't experience any different behavior because your method myMethod takes no arguments. Where this is useful, is when you have a method that takes arguments.
Let's say we declared a method, squareRootMethod: that takes a NSNumber and returns the squareRoot. Then you would call [self performSelector:#selector(squareRootMethod:) withObject:numberToRoot afterDelay:1.0]
There are also methods like performSelector:withObject:withObject: for selectors that take more than one argument.
Notice the difference between these two:
#selector(myMethod)
#selector(myMethod:)
The first one is a method that doesn't take any parameters, the second is a method that takes one parameter. The withObject: part of the performSelector: method you're using allows you to pass an object into the method when it is called. However, in the case where the method doesn't take any parameters it doesn't matter because it won't be used.
The difference is whether or not you are passing an object to the selector. All the selector is doing is describing a method.
[self performSelector:#selector(myMethod) withObject:nil afterDelay:1.0];
is different from:
[self performSelector:#selector(myMethod:usingThis:) withObject:nil afterDelay:1.0];
Now if you want the selector (i.e. method) to work on some object that you pass in, say an Array, Dictionary, or class. You use withObject. As in:
[self performSelector:#selector(myMethod:) withObject:myDictionary afterDelay:1.0];
-(void)myMethod:(NSDictionary*)dictionary
{
// Do something with object
}
You could pass in anything including a reference to the current class (e.g. self), as you did in your example.
In the first example, you passed self as the argument to pass to the method when it is invoked. But your method takes no arguments, so it is unnecessary fluff.
In the second example, you passed nil, so the method is passed nil to it's non-existent arguments and then terminates. This is more "efficient" in the sense that because your method takes no arguments, and `nil. Is the object equivalent of NULL, then you pass less fluff through that is ignored anyhow.

How to call a method from another method with arguments

I want to call another method from the updateButtonPressed method.
This is what I tried:
-(IBAction) updateButtonPressed{
[self loadScrollViewWithPage];
}
But the problem is that the loadScrollViewWithPage method has arguments. That method is like this:
- (void)loadScrollViewWithPage:(int)page {
}
How can I call this method?
If I understand correctly, you are wondering how to pass arguments along with messages to objects, is that right? Try:
-(IBAction) updateButtonPressed{
int foo = 4;
[self loadScrollViewWithPage:foo]; // a colon, followed by the argument
}
I suggest you read up on the Objective-C language in general, though.
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/introduction/introobjectivec.html
- (IBAction) updateButtonPressed{
int tempValue=5;
[self loadScrollViewWithPage:tempValue];
}

How to call a method from another method in objective C?

-(void) callme {
//statements
here I call another method "callmeagain"
}
}
But it is not working. Is there another way to do it?
To call an ObjC method, use the syntax [foo methodName:param andAlso:param2 …] In your case, try
-(void)callme {
[self callmeagain];
}
Another Method could be
[self performSelector:#selector(callmeagain)];
It is basically the same thing as Kenny's Suggestion