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 03-30-2011, 06:30 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 152
mrtubby is on a distinguished road
Default + vs -? confused....again

ok im a little confused (still learning objective c) on when you use + or -

like i think +(void) vs -(void)

what are the difrfences?

thanks,

mr total noob
mrtubby is offline   Reply With Quote
Old 03-30-2011, 07:41 PM   #2 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

+ means class method, - means instance method. Class methods means this
Code:
[SomeClass doSomething]
Instance methods require you have an object:
Code:
SomeClass *thisClass = [[SomeClass alloc] init];
[thisClass doSomething];
__________________
iisword is offline   Reply With Quote
Old 04-15-2011, 05:27 PM   #3 (permalink)
Registered Member
 
TheFlyingDutchman's Avatar
 
Join Date: Feb 2011
Location: NYC
Posts: 81
TheFlyingDutchman is on a distinguished road
Default

But that's it? There should be another reason. Please explain better.
TheFlyingDutchman is offline   Reply With Quote
Old 04-16-2011, 12:47 PM   #4 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

Nothing else really. Here's a link: Objective-C: Class vs Instance Methods? - Stack Overflow
__________________
iisword is offline   Reply With Quote
Old 04-16-2011, 03:09 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
lgehrig1 is on a distinguished road
Default

Quote:
Originally Posted by TheFlyingDutchman View Post
But that's it? There should be another reason. Please explain better.
Do you mean like when you'd use one over the other?

It all boils down to object oriented programming, which is a topic you're going to need to review. Google is your friend - my bookshelf is more than 12 years old at this point

In a nutshell, a class method is something that you would not need a specific instantiation of an object in order to invoke. The most useful examples are in NSString - you can call, for example, stringWithFormat to allocate, initialize, and auto-release a string object for you in one fell swoop. They tend to be "meta" methods - you're not doing something to the object, you're doing something about or with the object in a more general sense.

An instance method performs some action on a specific instance (see how that works!) of an object. Using NSString again, the method lowercaseString converts a string to its lowercase form. It doesn't convert every string everywhere in the universe - it only acts on that one specific instance of a string.

You'll find class methods used frequently with singletons - such as the currentDevice method of UIDevice. There's only one device - the one your app is running on! You can't initialize a new UIDevice object because that would be completely silly. Lookup "singleton pattern" on Google.

Because class methods are called without a specific instance attached to them you can't reference any data held by that object. Unless it's held by ALL instances (aka static member), but if you're having trouble with +/- I'd save static members for another day.

Most of the time you'll be creating instance methods. If you have a CKittyCat object and create a method called meow, you'd want that method an instance method (-). Then you could direct an individual kitty cat object to meow for you. I can't think of anything on a CKittyCat that would make sense to have as a class method (alloc, etc. don't count!).

Note: this is a rough sketch only. There's plenty of stuff left out (like an entire discussion of vtables, Obj-C messaging, polymorphism, inheritance, etc.). Hit your bookstores & libraries for object-oriented programming books - it won't really matter if they're Java, C++, C#, Objective-C, Smalltalk, or what have you. Try to find ones that are more overview than language specific, which is why I mention looking at actual paper vs. Amazon searching.
lgehrig1 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: 481
16 members and 465 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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