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 > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-13-2011, 09:39 PM   #1 (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 [TOPIC TUTORIAL] + and -, What they mean?

Hi everyone! I decided to write a quick tutorial about the + and - you see in front of methods. So real quick, what are they? Well they're prefixes to define a method as either a class or an instance method. What's the difference? Well the difference is how you can call the method. Let's make a quick file called by everyone's favorite name SomeClass.h
Code:
@interface SomeClass:NSObject
{
NSString *str;
NSInteger x;
}

/*instance methods*/
-(void)doSomething;
-(void)doSomething:(NSString *)string;
-(void)doSomething:(NSString *)string withInt:(NSInteger)integer;

/*class methods*/
+(void)doSomethingAsWell;
Now in SomeClass.m
Code:
import "SomeClass.h"

@implementation SomeClass
-(void)doSomething
{
str = @"This is a string";
x = 15;
}

-(void)doSomething:(NSString *)string
{
str = string;
x = 15;
}

-(void)doSomething:(NSString *)string withInt:(NSInteger)integer
{
str = string;
x = integer;
}

+(void)doSomethingAsWell
{
str = @"So what if it's a string";
x = 20;
}
Now if you were to import SomeClass, here's what you can do:
Code:
SomeClass *class = [SomeClass alloc];
[class doSomething];
[class doSomething:@"Some text"];
[class doSomething:@"Some text" withInt:20];
[SomeClass doSomethingAsWell];
So you probably notice from that in the 2, 3, and 4 lines I used the variable "class" in the beginning. This is because those methods can only be done if there is a SomeClass variable/instance/object. The 5 line did not need 'class' to work because it is a class method, all you need to do is import the class and just use the class name directly.

Instance methods are normally used when you have a object type that is the same for a bunch of things, but different values within the object. For example, if I had an Employee class with the variables 'hours' and 'wage' for each employee, I could do a getPay instance method for each employee.

Class methods are used when objects have something in common and you don't really need to create an object for it. For example, using the Employee class again, I could make a getOvertimePay: if all employees are paid the same for overtime.

Anyway I hope that this is helpful to you.
__________________
iisword 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
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

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