This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Method overloading in Objective-C?
Is method overloading not possible.
I have two functions with the same name.
When declared like the below i'm etting errors.
-(RS232Msg*)CreateMessage:(REMOTE_MESSAGE_ID) nMessageNumber;
-(RS232Msg*)CreateMessage:(const uint8_t*) szMessageName;
when declared -(RS232Msg*)CreateMessage:(const uint8_t*) szMessageName; i'm not getting any errors.
I also have two functions as the same name with different return type and argument.But its working fine and there is no error in its declaration.
Why is it so?
No, method overloading is not possible in C, and therefore not possible in Objective-C (since Objective-C is a superset of C). If you'd like to use those two methods, you'll have to change their names. I would suggest the following:
- (RS232Msg *)createMessageWithMessageID:(REMOTE_MESSAGE_ID)nMessageNumber;
- (RS232Msg *)createMessageWithName:(const uint8_t*)szMessageName;
Related
This question already has answers here:
ArrayList<String>() vs arrayListOf<String>()
(5 answers)
Closed 3 years ago.
I searched the difference between arrayListOf and ArrayList.
so I understand arrayListOf is function, and ArrayList is class.
but I don't understand the exact difference using them.
As described in the documentation arrayListOf is a function that creates an ArrayList instance. If I understand it correctly it serves the purpose of determining the generic type from the passed input values and it's a convenience function.
This question already has answers here:
In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them
(4 answers)
Closed 4 years ago.
I sometimes see statements like somevariable.value?.add() What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
Kotlin treats null as something more than the source of null-pointer exceptions.
In your code snippet, somevariable.value is of a "nullable type", such as MutableList? or Axolotl?. A MutableList cannot be null, but a MutableList? might be null.
Normally, to call a function on an object, you use a ..
One option for calling a function on a variable, parameter, or property that is
of a nullable type is to use ?.. Then, one of two things will happen:
If the value is null, your function call is ignored, and null is the
result
If the value is not null, your function call is made as normal
So, in your case:
If somevariable.value is null, the add() call is skipped
If somevariable.value is not null, the add() call is made on whatever somevariable.value is
This question already has answers here:
Confused about Swift Array Declarations
(2 answers)
Closed 5 years ago.
I was wondering what is the better way to declare variables?
private var arrayOfStrings: [String] = []
or
private var arrayOfStrings = [String]()
What are the advantages/disadvantages of either solution?
Thanks
As noted here there are little differences in the 2 ways.
Using type annotation you declare which type the variable is, while not using it you let Swift infer the type of the variable.
Almost always Swift will infer the correct type. Be careful:
As you can encounter situations like this in which Swift will have a decision to take. Read the full documentation here.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
When is it appropriate to write a method with a variable number of arguments (like NSString's +stringWithFormat:)?
A brief search of variadic methods from Apple seems to include only two classes: when creating a data structure (NSArray's +arrayWithObjects:, NSSet's +setWithObjects:), or when formatting a string (NSString's +stringWithFormat:, NSPredicate's +predicateWithFormat:).
Apple's documentation for variadic methods includes an example that is subtly different from the previously mentioned data structure methods, but still in the same camp.
Is it appropriate to use variadic methods in any other context? Does Apple?
When is it appropriate to write a method with a variable number of arguments (like NSString's +stringWithFormat:)?
This method uses a printf-like attribute which checks the format string against the additional parameters. You should always add an attribute to your methods/functions when using a format string.
A brief search of variadic methods from Apple seems to include only two classes: when creating a data structure (NSArray's +arrayWithObjects:, NSSet's +setWithObjects:), or when formatting a string (NSString's +stringWithFormat:, NSPredicate's +predicateWithFormat:).
This form uses a nil sentinel. It's actually a bit relaxed because an unexpected nil can silently truncate your parameters without error or warning. As an example, the (new-ish) Objective-C Literals do not use this form; they use implementations which also take count as a parameter, and it is an error to pass nil elements.
Is it appropriate to use variadic methods in any other context?
It's used in many places in C, and there are also variadic macros. Generally, you should look for alternatives because variadics are a less safe API.
One place I will use them is if I need to wrap an API. In that case, I only forward the parameters to the API which takes the va_list.
You can find safer alternatives for almost every use case, especially since the introduction of blocks.
To my knowledge, Cocoa's uses of functions with variable number of arguments is limited to the two categories that you have mentioned. For example, NSLog and NSAssert can be considered functions from the second category, because it formats a string.
However, functions with variable number of arguments can be very useful when in other situations.
For example, you can define an API for evaluating expressions that looks like this:
NSNumber *res = [Evaluator evalExpression:#"%1 + %2 * %3", #10, #20, #5];
// returns #110
Another example could be an API for composing XML, like this:
MyXmlTree *tree = [MyXmlTree addElementWithTag:#"root" andChildren:
[MyXmlTree elementWithTag:#"hello"]
, [MyXmlTree elementWithTag:#"world"]
, nil];
The second example is a more complex case of composing a data structure (i.e. composing a tree, rather than defining a linear structure).
This question already has answers here:
Is the 1st argument of an Objective C variadic function mandatory?
(3 answers)
Closed 8 years ago.
Typically, methods (and functions) that use variadic arguments seem to have it as the 2nd parameter like so:
- (void)setContentsWithFormat:(NSString *)formatString, ...;
Is it possible to declare this method so the variadic arguments are the first and sole parameter to this method? If so, what's the syntax for it?
I tried: - (void)setContentsWithArguments:(...) but that didn't work.
No, it isn't possible. The first parameter is effectively the reference point which tells the variadic functions how to access the additional parameters. Hence the va_start function takes the first method parameter (or, rather, the parameter just before the veridic) as its parameter:
- (void)doSomethingWithStrings:(NSString *)firstString, ...
{
va_list vList;
va_start(vList, firstString);
... blah blah
}
It seems that GCC supported it with the syntax:
- (void)setContentsWithArguments:...
But LLVM does not.