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

How can I spread node on 2D domain?

matlab

Is there anyone who know any function (or any suggestion) in MATLAB, that I could spread nodes on a 2D domain?

What is the intended development process in Module::Starter's manifest.t?

perl testing module manifest

When Module::Starter initializes a project, it creates a test called manifest.t.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.