1

In iOS I know we can define methods with

 1.) -(void)sum i.e. a instance method.

 2.) +(void)sum i.e. a static method.

But what about method written as

 3.) (void)sum i.e without -(hyphen) and +(plus) sign 

Now the question is:

  1. What we call method written in point 3?
  2. When and how do we use them?

A link where I have seen this type of method is

Exception Handling in iOS.

Check method

void InstallUncaughtExceptionHandler()
Federico Zancan
  • 4,846
  • 4
  • 44
  • 60
Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33

3 Answers3

3

It's a C based function. Because Objective-C is a superset of C, you can still use and declare C functions.

CoreGraphics is written in C, an example is the CGRectMake function:

 CGRect CGRectMake (
   CGFloat x,
   CGFloat y,
   CGFloat width,
   CGFloat height
 );
danielbeard
  • 9,120
  • 3
  • 44
  • 58
  • It's a function, not a method. The distinction is important as methods are associated with classes while functions aren't. –  May 02 '13 at 07:29
  • @GrahamLee Yeah, I realised after I wrote it, you are just too quick! I have updated my answer. – danielbeard May 02 '13 at 07:30
  • Ok but I am not clear about this. Why do we need that i.e. c's functions in OBJ-C. I think if in OOPS say C is a super class and obj-c is a child class it must have access to all the methods and members of C class. then why not just use OBJ-C all the places and don't use C code. – Avtar Guleria May 02 '13 at 07:36
  • I just want to know why to mix C and Obj-C code. Is there any any thing that we can't achieve by obj-c and also Can we also show UI components i.e. UIAlertView from C code. – Avtar Guleria May 02 '13 at 07:38
  • Lots of Apple's frameworks are still written in C, E.g `CoreGraphics`, `CFNetwork`, etc. It's so that we can use these frameworks and other C based frameworks, as well as the fact that Objective-C is built *on top* of C so everything C works anyway. – danielbeard May 02 '13 at 07:45
  • ok fine but also my next comment regarding what obj-c can't acheive and C achieve in respect of IOS apps. and also can we show UIComponents from C code. – Avtar Guleria May 02 '13 at 07:47
  • Plain C is faster since it is not dynamic. And actually every tool is good only in appropriate places. So is OOP. There are situations when you just don't need a separate class, you need a function itself. And C gives you such opportunity. And yes, you can call Obj-C methods from C-functions, if you tell the compiler that it is an Objective-C or Objective-C++ file (it has '*.m' or '*.mm' extension). – FreeNickname May 02 '13 at 08:42
3

To answer your first question: your point number 3) is not a method, it is a plain C function prototype (even if to be correctly validated by compilers it should be followed at least by an empty couple of parenthesis so that the statement would be void sum()).

And, concerning your second question: you could use plain C function declaration syntax wherever you want in your .h/.m files, and as Graham pointed out you could declare them even within @interface and @implementation sections; this way you could access ivars also.

As other answers correctly state in this thread, Objective-C is a superset of C, object-oriented, so mixing plain C declaration syntax and Objective-C syntax is ok at some extent, under the conditions mentioned above.

Generally speaking, one could make use of C-style functions in Objective-C (outside @interface and @implementation) when, for instance, that particular function is kind of general purpose and not strictly related to a specific class. The classical example in this direction is the already mentioned

CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)

As you may see it deals with coordinates and size, but not strictly related to something in particular. It is general. Additionally, this approach particularly fits when your function is not only general purpose, but it has to deal with C data types or C data structures also (like, in our case - CGFloat - basic type redefinition - and CGRect - C-style data structure).

Declaring C functions within @interface and @implementation sections is a way to implement functionalities accessing ivars without constituting a method, i.e. you could invoke those functions only from within the class where they belong.

Virtually, there's nothing of C-related you couldn't do from Objective-C methods, provided correct inclusions are made (e.g. stdio.h or fcntl.h); the programmer has just to question the merits of the functionalities he/she has to implement and, dependently on that, understand if writing a C-style function could be a good fit or if a class/instance method is more appropriated.

Federico Zancan
  • 4,846
  • 4
  • 44
  • 60
  • But just want to know why (or say when) to mix C and Obj-C code. Is there any any thing that we can't achieve by obj-c and also Can we also show UI components i.e. UIAlertView from C code. – Avtar Guleria May 02 '13 at 07:42
  • @AvtarGuleria I'll try to clarify that. – Federico Zancan May 02 '13 at 07:44
  • You can declare and define functions inside @interface/@implementation. It's a common way to write a function that has access to an object's ivars. –  May 02 '13 at 08:17
  • @FedericoZancan :) thanks for your time and explaination. This will really help me in my day to day implementation and will now think of implementing this wherever i think it should be. – Avtar Guleria May 02 '13 at 10:42
0

As you know Objective-C is a thin layer on top of C, and moreover is a strict superset of C. So you can also use C and C++ code in Objective-C. The method written in point 3 is a C method(or function, whatever). If you need to use C or C++ methods (or functions) in Objective-C then you should follow this style.

x4h1d
  • 6,042
  • 1
  • 31
  • 46