I have been looking everywhere for an answer to this question - perhaps I'm looking in the wrong places. Also, I'm brand new to Objective C although I have around 10 years of experience as a developer.
for this code:
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
what would the method definition look like?
- (void)makeGroup:(Group *)g, (NSString *)memberOne, ...?
Thanks for any help you can provide. I know this is probably very simple...
Thanks,
R
It looks like you have a method that can take a variable number of arguments. If that's the case, the definition would look something like:
- (void)makeGroup:(Group *)g, ...;
Check out NSString's stringWithFormat: or NSArray's arrayWithObjects: methods for examples.
Edit: Upon further documentation reading, it seems that you are looking at the exact example that's in the Objective-C 2.0 documentation. The declaration you're looking for is right at the bottom of page 36.
You can receive an infinte number of arguments with an ellipsis (...). Check this for further details!
It would make more sense to have the members as a separate array argument, like -(void)makeGroup:(Group *)g members:(NSArray *)members. If you must do varargs (which is a pain), it should be written like -(void)makeGroup:(Group *)g members:(NSString *)firstMember, ....
Since I this is trying to figure out how an example method from the documentation would be declared, it would be like this:
- (void)makeGroup:(id)group, ...
Then you would start up the varags machinery with the group argument and use it to find the other arguments.
either you're looking for MrHen's answer if you're seeking to do your own class method or if you want to do them separately you write the following into your header file:
-(void)makeGroup:(Group *)g;
-(NSString *)memberOne;
EDIT: I answered the wrong question. Ignore this.
The correct way to do this is:
-(void)makeGroup:(Group *)g memberOne:(NSString *)memberOne memberTwo:(NSString *)memberTwo memberThree:(NSString *)memberThree {
...
}
The call will look like this:
[receiver makeGroup:group memberOne:memberOne memberTwo:memberTwo memberThree:memberThree];
Related
Let's say I get two instances in my code and I don't know their types. How to check it?
If in Java, I can use this code:
a.getClass() == b.getClass()
But in Dart, I can't find similar methods. Although there is the dart:mirrors providing reflect(instance) function, which may let me do it, but I'm not sure if that's a correct solution since it looks complicated.
a.runtimeType == b.runtimeType
I think dart:mirrors (reflection) API helps you. Look at this page :
http://blog.dartwatch.com/2012/06/dartmirrors-reflection-api-is-on-way.html
Also you can look this question(with runtime solution)
How do I get the qualified name from a Type instance, in Dart?
if you want to compare a and b you can use
if(a.runtimeType == b.runtimeType);
but if you want to confirm that a is the type you want you need to do this
if(a.runtimeType.toString()=="DivElement");//a is a div for instance
because runtimeType's value is not a string
I have seen in many iOS header's that (^) is utilized, and I have never come across the reasoning as of why that sign is being used. Would anyone might like to enlighten into this?
Thanks.
Those often indicate "blocks". See the Blocks Programming Topics.
Alternatively, if you watch the beginning of WWDC 2012 session 712, they also walk you through blocks with a touch of historical context.
It signifies a block. A block is a syntax that allows you to create a callback function, and pass it into a method as a parameter. In other languages this is similar to a closure, a lambda, or an anonymous class.
For example a parameter that lists:
void(^)(NSString *myStr)
is expecting you to pass in a block/function that returns void, and takes in an NSString pointer.
You can create a block, based on the expected parameters declared in the method, with this syntax:
^(<Parameters>) { <Body> }
For example, a method that is expecting a block parameter might look like this:
-(void)doSomething:(void(^)(NSString *myStr))theBlock;
...and could be called like this:
[self doSomething:^(NSString *myStr) { NSLog(#"The String is: %#", myStr); }];
Your block will be called back from doSomething: just like a function, using the parameter name:
-(void)doSomething:(void(^)(NSString *myStr))theBlock {
theBlock(#"Hello!");
}
...which would display:
The String is: Hello!
The ^ character is used for blocks, in particular, block parameters.
If you're asking why the character '^' for use in blocks, it's because there's relatively few characters left that:
Are available on all typical keyboards.
Could be used at all - i.e. aren't already significant in the language and would conflict.
Don't look stupid.
That actually narrows it down to only two or three, and of those '^' was chosen because, well, because.
There's probably a record of this on the llvm.org mailing lists and so forth, if you want to pore over the discussion in detail.
You could also look at the minutes from the C++11 committee meetings on lambdas, which went through basically the same process.
I want to to math operations with some kind of prepared formula that would look like tan(%f)*1000 or %f+%f where the %f has to be replaced by an argument.
Is there a function in Objective-C that I can pass the format of my formula and the required numbers to execute this prepared operation?
I hope the problem is described understandable, if not, leave a comment.
Thanks in advance.
Edit 1: Thanks for your answers so far, but I'm looking for something more dynamic. The block and inline function is great, but to static. I also understand that this may be something hard to achieve out of the box.
You may be interested in DDMathParser, found here. I believe it will do everything you're looking for.
There is nothing that would do it this way, however what you could do is rewrite your "format" into a function, and just pass the arguments it needs to have, much faster and much easier.
inline float add(float p_x,float p_y)
{ return p_x+p_y; }
inline is a compiler feature that you can use to speed things up. It will replace the function call with the code it executes when you compile. This will result in a lager binary though.
If I understand your question correctly, Objective-C Blocks are great for this.
typedef double (^CalcBlock)(double);
CalcBlock myBlock = ^(double input) {
return (tan(input) * 1000);
};
NSLog(#"Result: %f", myBlock(M_PI_2));
You can pass the block that contains your algorithm to other objects or methods.
Example: I want to do this:
METHODNAME(5) {
// do something
}
which results in:
- (void)animationStep5 {
// do something
}
Is there any way to do this? Basically, what I need is a way to generate a real source code string before the program is compiled, so the compiler does see - (void)animationStep5...
Or maybe there's something different than a macro, which can help here to auto-generate method names (not at run-time)?
As was already answered here, the objective-C preprocessor is very close to the C one.
You should have a look at the examples posted there, and have a look at C proprocessor. You will simply have to use the ## syntax of the preprocessor, to concatenate the method name, and the number you want.
You can use the concatenation operator
#define METHODNAME(i) -(void)animationStep##i
you can call it like
METHODNAME(5){}
This expands to
-(void)animationStep5{}
Assuming the objective-c preprocessor behaves the same as the standard C one, you can use something like:
#define PASTE(a, b) a##b
#define METHODNAME(n) PASTE(animationStep,n)
to join the required bits together. This means that
METHODNAME(5)
gets translated to
animationStep5
(you may need to add the "void" from your question to the macro definitino depending on exactly what it is you need to do).
Is there any reason why the asterisk is next to the object type in this code? I'm a little confused by the way I see this used. Some times it looks like this:
NSString* stringBefore;
and sometimes like this:
NSString *stringBefore;
Is there a difference? Or a right or wrong way to do this?
Thanks
I use the * near the variable name and not the type, since if you declare something like:
int *i, j;
i will be a pointer to int, and j will be a int.
If you used the other syntax:
int* i, j;
you may think that both i and j are pointers when they are not.
That said, I don't use nor recommend declaring a pointer and a non-pointer variable in the same line, as in this sample.
It makes no difference.
It just just an indicator to how well versed the author is in writing and reading Objective-C. The traditional standard is to write it as:
NSString *stringBefore;
There is no difference.