Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
is it generally good practice to declare all functions of the implementation file in the header file? it seems i can get away with not declaring some -(void) or -(id), etc. functions, so i'm assuming it's not mandatory to do so, but should i do this regardless of the app already working properly?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You only need to declare methods that are:

a) Not inherited from the super class
b) Intended for use by other objects

So init and dealloc do not need to be delcared: they are declared in NSObject.

If you have purely internal methods you don't need to declare them. A convenient way to declare them but not clutter your main .h file is to declare them in a category called Private. This will prevent compiler errors if you use them before the method declaration as you can import the Private category header in your .m file.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
ok makes sense...

i've called them functions, you've called the methods... can they be called either?

Not absolutely sure either is 100% correct in Obj-C.

Methods are an Object-Oriented term that means, roughly, a function associated with a class. Methods implement the functionality of a class along with it's encapsulated data.

Functions are normally procedural. I.e. they are not part of a class but stand alone code. In Obj-C we can use functions outside of classes (for example NSLog).

So we should not be calling class functionality "functions", rather class or instance methods (depending if they are + or -). I have a nagging thought that method is a Java term and they are called something else (not function) in Obj-C though...
 

MonkeyCookie

macrumors newbie
Apr 18, 2008
28
0
ok makes sense...

i've called them functions, you've called the methods... can they be called either?

In Objective-C, C#, and Java, functions that are part of a class are usually called methods. C++ uses the term "member functions". Functions that aren't part of a class (possible in C++ and Objective-C) are generally referred to as functions. In C, from which Objective-C derives, there are no classes, so they are all called functions. In some other languages, they are referred to as procedures.

It's not very important whether you call them methods or functions: everyone will know what you're talking about.
 

ChrisA

macrumors G5
Jan 5, 2006
12,917
2,169
Redondo Beach, California
ok makes sense...

i've called them functions, you've called the methods... can they be called either?

Some of us learned all this before the term "method" was invented and will continue to say "function". Also in C++ methods are implemented as functions. Same for "Memory" and I sometimes slip and say "core" because that was the technology back when many of us were in school. Even today dumps are called "core files". I would not worry about what is "correct" as long as the idea gets across.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
The problem with calling methods functions in Obj-C is that you can still define and use pure C functions. If people use the correct terminology it's easy to tell what they are talking about. If they don't it can get confusing.

Note that there can be very good reasons to use C functions: calling one is much faster than passing a message to an object in Obj-C.
 

devman

macrumors 65816
Apr 19, 2004
1,242
8
AU
I have a nagging thought that method is a Java term

method is a term that long predates Java. The data of a class is called attributes and the behavior is called methods. These two names were de facto standards for a long time. Then the UML came along and decided to call methods operations.

and they are called something else (not function) in Obj-C though...

do you mean selector?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.