Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-03-2009, 01:16 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Bhubaneswar(INDIA)
Posts: 39
ajitbiswal is on a distinguished road
Default selectors in objective c

I am very confused regarding selector concept in objective c.Can we mention two methods with same name and different parameters in two different classes .If not how this solve the problem of function overloading.please, answer as it is required very urgent.
ajitbiswal is offline   Reply With Quote
Old 09-03-2009, 11:16 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by ajitbiswal View Post
I am very confused regarding selector concept in objective c.Can we mention two methods with same name and different parameters in two different classes .If not how this solve the problem of function overloading.please, answer as it is required very urgent.
Objective-C doesn't have support for method overloading; methods that take different types should have different names.

You should not declare two methods with the same name but different parameter type - not in the same class, and not in related classes. It will cause a compiler warning and may not work as expected.

This came as a shock to me just this week, after writing Obj-C for a year.
__________________

Free Games!

Last edited by smasher; 09-03-2009 at 01:22 PM.
smasher is offline   Reply With Quote
Old 09-03-2009, 12:21 PM   #3 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by ajitbiswal View Post
I am very confused regarding selector concept in objective c.Can we mention two methods with same name and different parameters in two different classes .If not how this solve the problem of function overloading.please, answer as it is required very urgent.
If it's actually unrelated classes, it's fine. If they're related (subclass or whatever), then you can't do it.

joe
FlyingDiver is offline   Reply With Quote
Old 09-03-2009, 11:10 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Bhubaneswar(INDIA)
Posts: 39
ajitbiswal is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Objective-C doesn't have support for method overloading; methods that take different types should have different names.

You should not declare two methods with the same name but different parameter type - not in the same class, and not in related classes. It will cause a compiler warning and may not work as expected.

This came as a shock to me just this week, after writing Obj-C for a year.
Thank u verymuch... for replying..
ajitbiswal is offline   Reply With Quote
Old 09-07-2009, 02:43 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Bhubaneswar(INDIA)
Posts: 39
ajitbiswal is on a distinguished road
Default

- (id)initWithTagint)tag
{
return [self initWithTag:tag data:NULL];
}
- (id)initWithTagint)tag datastruct info *)data
{
if ((self = [super init. . .])) {
}
return self;




You told me that there is no overloading of methods.Then what abt the above two methods where the two methods are same. This I found in

NSObject Class Reference book from apple.. please explain me.
ajitbiswal is offline   Reply With Quote
Old 09-07-2009, 03:30 AM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The parameter labels are part of the method name - so "initWithTag:" and "initWithTag:data:" are two different method names. If you try get selectors for those methods, then @selector(@"initWithTag:") and @selector(@"initWithTag:data:") would give you two different selectors.

Overloading would be two methods called "initWith:" , but one takes a string and another takes an array. You can't do that.

The code you posted is a good example of what to do instead of overloading - create two methods with two different names to use in different situations.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-07-2009, 04:51 AM   #7 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Bhubaneswar(INDIA)
Posts: 39
ajitbiswal is on a distinguished road
Default

thank u very much for ur quick reply..i got the point..
ajitbiswal is offline   Reply With Quote
Old 09-09-2009, 01:40 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Bhubaneswar(INDIA)
Posts: 39
ajitbiswal is on a distinguished road
Default purpose of init method

We have two init methods, One in NSObject class and another in NSString class .Can you explain the purpose of two of above methods? The init of NSObject class I know as it is initialize the instance variables to 0 but the confusion arises when I came across init method of NSString class .

Also I want to know the purpose of initWithBytes:length:encoding: method of NSString class.
ajitbiswal is offline   Reply With Quote
Old 09-09-2009, 07:21 AM   #9 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by ajitbiswal View Post
We have two init methods, One in NSObject class and another in NSString class .Can you explain the purpose of two of above methods? The init of NSObject class I know as it is initialize the instance variables to 0 but the confusion arises when I came across init method of NSString class .

Also I want to know the purpose of initWithBytes:length:encoding: method of NSString class.
NSString is a subclass of NSObject. NSString's init method IS NSObject's init method. Plus anything additional NSString needs.

The purpose of initWithBytes(etc) is to initialize the string with some specific content based on the contents of the NSData object. (I think the bytes: parameter takes an NSDATA).

joe
FlyingDiver is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 455
13 members and 442 guests
Domele, Duncan C, Feldspar, MacBook MH, Objective Zero, patapple, peterwilli, pipposanta, PixelInteractive, Punkjumper, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:03 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0