Jim True
Jim True

Reputation: 1080

Creating a exported method library class in iOS

I'm sure my verbiage is what is stopping me from finding the right answer here. I want to create an "Common" class that all of my other classes can use in xCode. This Common class will contain universal methods i use frequently by several different classes. I know one way that works is to put a "+" sign at the beginning of my method definitions and include my "Common.h" in each class. IS this the best way for memory management, efficiency, good practice? Or do you recommend a different solution.

//Inside Common.h
+(NSString*)SayHello;
//Inside Common.m
+(NSString*)SayHello{
return @"Hi";
}

//Inside AppDelegate.m
#import "Common.h"
//then later
NSLog(@"Common said %@",[Common SayHello]);

Upvotes: 0

Views: 633

Answers (2)

A-Live
A-Live

Reputation: 8944

I prefer to make it C-style for the utility classes:

.h

NSString *ResourcesPath();

.m

NSString *ResourcesPath() {
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    return resourcePath;
}

usage

import .h call with

NSString * resPath = ResourcesPath();

Upvotes: 2

Ryan Poolos
Ryan Poolos

Reputation: 18561

The + sign denotes a class method over an instance method. As far as good practice it definitely depends. As far as memory management if you're using ARC you shouldn't have to worry to much. If you're using MRC you just need to be aware of objects you're creating and returning in the class methods same as you should be for instance methods.

Another thing to consider is whether or not you need a Singleton to do this type of work. Not knowing your exact goals thats hard to determine but chances are you could probably use one.

Upvotes: 1

Related Questions