iOS Different types of method signatures

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()

Answers


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.


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
 );

Need Your Help

Ruby on Rails: Entering data from another database into a new database

ruby-on-rails ruby devise submit

I have two databases in my app. One is Projects, that holds data on projects created in the app and another is User that I made when installing the Devise gem that holds the usernames and passwords...

Corebluetooth connection how to save battery level while it is connected

iphone ios objective-c bluetooth battery

Am going to use core bluetooth framework to my application.But i would like to know when i am connecting with another BLE device with long period of time in this situation how do i handle battery l...

How to shutdown JBOSS from jmx-console

java jboss

Is it possible to shutdown JBOSS from jmx-console?