Duplicate declaration of method objective-c - iphone

This question may be duplicate of the Objective C - "Duplicate declaration of method" compilation error
But i am still confuse for why objective C dose not support the function Overloading / method overloading
Can any one tell me the proper reason for why this error occur?

objective-C does not support method overloading, so you have to use different method names.

Simply because Objective-C doesn't support overloading. And besides it is highly recommended to include the types of the arguments in the function's name, whenever possible. Try getTextFromTextView: and getTextFromTextField: instead.

I want to tell you please check the "How to define method ?", You can found in above link about multiple input method also. and this one is for naming conventions of method in Objective C?
As per above we can know that your method have same name(Signature) as getText:
In your case there are duplicated method you define which is not supported by objective C Compiler.
Hope this one helpful to you.

read this thread that why objective c does not suport method overloading
Why Objective-C doesn't support method overloading?
and this is apple discussion form
https://discussions.apple.com/thread/512569?start=0&tstart=0

Related

Naming methods in Swift versus Objective-C

All:
In Objective-C, the name is constructed by concatenating the signature keywords, e.g., application:didFinishLaunchingWithOptions:
How do people refer to the equivalent method in Swift, when discussing it with other programmers (including students)? For example, would you refer to verbally as application(), or application(_ didFinishLaunchingWithOptions:)?
Thanks,
Michael
Apple's style is to include all the parts of the declaration that make its unique function signature (and omit things that matter only to the implementor, like local parameter names). If you set the view on one of Apple's reference pages to Swift-only, you'll see this style in the method listings:
application(_:willFinishLaunchingWithOptions:)
application(_:didFinishLaunchingWithOptions:)
applicationDidBecomeActive(_:)
applicationWillResignActive(_:)
Obviously, when speaking aloud one can omit the punctuation and people will still know what you're talking about.
(Notice you can't call either of the first two just application(), because there are two distinct methods with that base name and different external parameter labels.)

preventing init and forcing initWithOption

I know you can have #private member variables but is there someway to prevent code from calling the default init: method of your class and forcing code that creates and uses your objects to only use another initializer such as initWithOptions:
I have had a flick through some Objective-C books and didn't immediately see anything.
I have seen the stack overflow topic suggesting to throw exception, or assert/abort/whatever in how to prevent usage of other init methods other than my custom method in objective-c
and the use of the depreciated keyword
How do I flag a function as being deprecated in an iPhone Objective C header file?
Both of these seem somewhat less than elegant solutions, there is really no language orientated elegant way to say doesNotImplmentSelector in Objective-C...?
I come from a C++ background and just kind of expected something like the ability to hide the default constructor...
R.
I think the standard thing to do is to have an implementation of init which calls initWithOptions: with a default set of options.

Text length in Objective-C

Please tell me the difference between test.length and [test length]?
Which is more usefull for iOS developement?
No difference in meaning, they both access length property.
Their only difference is syntactic.
Check Apple documentation about sending a message to an object
test.length is just a convenience syntax introduced in Objective C 2.0. The two expressions you list are totally equivalent and a matter of preference more than anything else.
They are the same. Sometimes it may (!) be better to use one or the other for readability of your code.

Why does objective C does not support overloading? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Why Objective-C doesn't support method overloading?
I was going through this link which has the answer to my question. But I was not able to understand what the right guy is trying to say. It will be really great if someone can simplify and provide an explanation.
In C++ the compiler has type information and can choose between several methods based on the types. To do the same in Objective C it would have to be done at run-time, because the compiler knows little about object types due to the dynamic nature of the language (i.e. all objects are of type id). While this seems possible, it would be very inefficient in practice.
I think it's a historical artifact. Objective-C is derived from C and Smalltalk, and neither of them support overloading.
If you want overloading, you can use Objective-C++ instead. Just name your sources with the ".mm" extension instead of just ".m".
Just be careful to be sure you know what you are doing if you mix C++ and Objective-C idioms. For example Objective-C exceptions and C++ exceptions are two completely different animals and cannot be used interchangeable.
What he is trying to say is that method overloading is not possible with dynamic typed languages since in dynamic typed languages the information about each of the object is not known until run time. In a statically typed during compile time the overloading can be resolved. You will just create the same functions with same names but the compiler would have enough information to resolve the ambiguity between various calls which it gets. But in dynamic typed languages since the objects are resolved only during run time it is not possible to resolve between the various calls.

Multiple parts of methods in Objective C

I am learning Objective C and noticed this funky quirk while reading up on methods.
Like Java and C++, Obj.C can take in multiple parameters, which is fine, however it states that objective C methods can have multiple names which does not seem to register to well with me.
For instance:
-(NSArray *)shipsAtPoint:(CGPoint)bombLocation withDamage:(BOOL)damaged;
In the above example, there are two parameters, bombLocation (return type CGPoint) and damaged (return type BOOL) and alongside the method name seems to be split as shipsatpoint:withDamage
I don't understand what's up with this...
What does it signify when it states that a method can have multiple names?
Is this applicable only for methods that require multiple parameters? Alternately, say I want to name my method with a single name but provide it with multiple parameters, is that possible or I must provide it with multiple names each of which correspond to a parameter? If yes, then why?
Thanks for jumping in with my confusion!!! :)
The reason is to make it easier to understand.
With your example, the method would be something like this in C++:
int shipsAtPointWithDamage (CGPoint bomb, BOOL damage) //I don't really know C++
OK, so the first parameter is the ship's point, and the damage is the second. It's easy enough to figure out, but that's the thing, you have to FIGURE it out, you have to look at the method to try and figure out what each thing is.
In Objective-C you have
-(NSArray *)shipsAtPoint:(CGPoint)bombLocation withDamage:(BOOL)damaged;
Each parameter is clearly defined, the first is the ship's point, the second is damage. It reads like a sentence, whereas with C++ (and almost every other language) it doesn't.
If you want a method to have multiple parameters in Obj-C you have to write it this way:
-(returnType)paraOne:(type*)name paraTwo:(type*)name
It's something that just takes getting used to, every language is different. Once you get used to the way Objective-C does things, you'll think it's absolutely fantastic.
EDIT: and as filipe pointed out, because the method as multiple parameters it doesn't mean it has multiple names, in the example I gave above, the method name would be paraOne:paraTwo, NOT paraOne:
Objective-C uses a system of message passing based on selectors. This is not quite the same thing as method calling. When you see code like this:
[world shipsAtPoint:point withDamage:YES];
That is converted into the following C call (in the most common case):
objc_msgSend(world, #selector(shipsAtPoint:withDamage:), point, YES);
The #selector() construct returns a unique identifier. The exact format of that identifier is an internal implementation detail.
objc_msgSend includes quite a lot of logic in it's few dozen bytes of assembler. But in simplest case, it looks up the class for world, walks through a table of selectors until it finds the one that matches shipsAtPoint:withDamage: and then grabs the function pointer at that slot. It then jumps to that function pointer, leaving the rest of the parameters alone (in registers or on the stack as appropriate for the processor). The function at that location is your method, and it knows the order and types of its parameters based on your declaration.
What's important in all this for you is that the selector is shipsAtPoint:withDamage:. This is generally the one-and-only name of the method. There are not "multiple names" as you suggest. (Usually.... the Objective-C runtime is very powerful and it's possible to point multiple selectors to the same implementation.)
As Joe points out, a selector can be in the form foo::. This would represent a method that took two parameters and would be called like [world foo:point :YES]. You should never do this. It's incredibly confusing to read. But it's legal.
Here is the best explanation i've ever seen. It includes comparisons with C++/C as well as lots of other good info.
I think you are confused. A method cannot have multiple names, but the argument may be named differently in the header then they are in the implementation.
The name of that method is shipsAtPoint:withDamage:. This is also known as a selector.
This method returns an instance of NSArray, and accepts a CGPoint as the first argument, and a BOOL as the second argument.
The names of the arguments may differ, however. This is totally valid:
// .h file
-(NSArray *)shipsAtPoint:(CGPoint)bombLocation withDamage:(BOOL)damaged;
// .m file
-(NSArray *)shipsAtPoint:(CGPoint)loc withDamage:(BOOL)dmg {
// ...
}
Lastly, ObjC is mainly some nice syntax sugar. You should know that any method invocation really just boils down to some C that looks more or less like this:
objc_msgSend(receiverObj, #selector(shipsAtPoint:withDamage:), point, damage);
So at the end of the day, you have a receiver, a selector, and your arguments. But the ObjC syntax is much nicer than that.
It is possible provide a method without labeled parameters but it is obviously discouraged.
-(void)badmethod:(id)obj1:(id)obj2:(id)obj3
{
}
//...
//Usage
[self badmethod:nil :nil :nil];
SEL sel = #selector(badmethod:::);