1

I have been building an iOS app and one of my ViewControllers had become full of functions like:

CGPoint randomPoint()
{
  //Code goes here
}

I now want to move them to class A (or protocol, I am not sure what will work for me), import it in the VC and call the just like before:

p=randomPoint(); //NOT A.randomPoint(), [A randomPoint] or whatever

I tried using the C++ class template, but it has problems with CGPoint, CGRect and so long.

How can I accomplish that?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Hristo
  • 6,382
  • 4
  • 24
  • 38
  • why don't you make `static inline` directives? they are enough happy in any header `.h` file. – holex Sep 04 '12 at 17:32

2 Answers2

3

If you are wondering were to put C functions like the one you are describing, the best practice is to move them into a separate .h file that has a meaningful name. For instance MyGeometry.h

Make sure you give a descriptive name to your function, such as:

static inline CGPoint CGPointMakeRandom() {
    // your code
    return point;
}
Pedro Mancheno
  • 5,237
  • 4
  • 24
  • 31
  • Thanks a lot! This works like a charm. So I do not need a separate implementation file and I can write everything in the .h file? – Hristo Sep 05 '12 at 05:46
  • You could add an implementation file, but this functions tend to be so simple that it's much more convenient to define them inline. As their complexity increases you might want to move the implementation to an .m to not clutter your .h – Pedro Mancheno Sep 05 '12 at 13:37
  • Also: http://stackoverflow.com/questions/7762731/whats-the-difference-between-static-and-static-inline-function – Pedro Mancheno Sep 05 '12 at 13:51
0

You could make a separate objective c class with class methods.

In the header file declare the method like this (let's say you want to call it

#import <UIKit/UIKit.h>

@interface pointHelper : UIViewController

     +(CGPoint) randomPoint;

And then in the .m file

 @implementation pointHelper
     +(CGPoint) randomPoint{
         //// implementation
     }

When you want to evoke the method in another file.

#import "pointerHelper.h"

You will then be able to access the method like this...

CGPoint thePoint = [pointHelper randomPoint];

Or if you had an object of the class..

CGPoint thePoint = [[pointHelperObject class] randomPoint];

This is a better way of doing it, since it makes your code much clearer. [pointHelper randomPoint] tells you why you are evoking the method and what it is doing. You are using a class that has utilities for points, and you are using it to grab a random point. You don't need an object to evoke this method, because it is controlled abstractly by the class. Just be careful not to try to access properties of the class within the class method.

AdamG
  • 3,718
  • 2
  • 18
  • 28
  • Or, if you are trying to do it the c way you can use pmd's answer. Although I recommend you do it the objective-c way, as it is clearer. – AdamG Sep 04 '12 at 17:45
  • Cocoa Touch uses C functions to handle CGPoint creation instead of class methods, probably because points have such a simple structure. I think it's best to stay in line with that practice. For reference check their Geometry.h file – Pedro Mancheno Sep 04 '12 at 19:02
  • Yeah I guess it is a matter of personal preference and efficiency. – AdamG Sep 04 '12 at 20:58