Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - how do I create a "utility" class that all my controllers can call

I am new to iOS and obective-c so I am not too sure how to best accomplish this seemingly simple task.

What I want is to make a class that looks like this in pseudocode:

class UtilityClass
{
    // Have a method that I can pass parameters to 
    String doCalculation ( String art1 , String arg2 )
    {
        return arg1 + arg2;
    }
}

My uncertainty is:

1) xCode seems to be inclined to lay out my file structure in a relatively flat way. So should I make a utils directory and have this file be under utils/fileName ? Usually I am kind of used to having at least some src directory, but so far I have not been prompted by anything to create one. 2) How do I import and call this class/function from my controllers?

Thanks, Alex

like image 219
GeekedOut Avatar asked Jul 13 '12 23:07

GeekedOut


3 Answers

Just create a new group called Utilities, and then create your class inside it. Like,

utils.h 
utils.m

Later in your ViewController's header file just add.

#import "utils.h"

if this utils class is used by many controllers in very fat project then, find a file called, Should be inside supporting files group.

YourAppName-Prefix.pch

In that file you have a code block like this,

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
#endif

Just edit this block and add your utils.h reference here, In this way your entire project can create utils object without explicitly importing into their own header.

Edit like this.,

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
    #import "utils.h"
#endif
like image 200
TeaCupApp Avatar answered Nov 18 '22 21:11

TeaCupApp


First of all create a new File in Xcode and uncheck xib file. Name the Project as you like . and extend it from NSObject .

for creating static method you have to replace function starting - to + like

interface. class

@interface Utility : NSObject
+ (int)getNumber;
+ (void)setNumber:(int)number;
@end

.m class

#import "Utility.h"

@implementation Utility

static int number = 1;

+ (int)getNumber {
  return number;
}

+ (void)setNumber:(int)newNumber {
  number = newNumber;
}

+ (id)alloc {
  [NSException raise:@"Cannot be instantiated!" format:@"Static class 'ClassName' cannot be instantiated!"];
  return nil;
}

@end

call it in any other ViewController like

 NSLog(@"number = %d", [Utility getNumber]);
    [Utility setNumber:3];
    NSLog(@"number = %d", [Utility getNumber]);

for details..

like image 33
Zar E Ahmer Avatar answered Nov 18 '22 21:11

Zar E Ahmer


Where you store the files is up to you, just make sure XCode knows where to find them. The class itself should be made like any other Objective C class, just make it inherit from NSObject instead of one of the graphical classes:

// MyClass.h
@interface MyClass : NSObject {
    int instanceVar;
}
@property (nonatomic, assign) int property;
@end

// MyClass.m
#import "MyClass.h"
@implementation MyClass
@synthesize property;
-(id) init {
    ...
}
-(int) function  {
    ...
}
@end

To use the class in another file, just import the header like any other class

#import "MyClass.h"
like image 28
Kevin Avatar answered Nov 18 '22 21:11

Kevin