Paul Peelen
Paul Peelen

Reputation: 10329

init my own api class

I have an API class, lets call it MyClass, which I want to be available thruout my whole application. Hence I put the #import into MyProject-Prefix.pch file.

Now, when I (in my appDelegate) initiate the class, I get the error message cannot init a class object.. I understand that I am initiating my class incorrectly, but I have no idea on how I should do it... and since this is not common for development (as I see it) there ain't a lot of information to find thrue google (or I am searching wrong ;) )

So, I have two questions:

  1. Any links for called "Creating Objective-C API's for Dummys"? ;)
  2. Having a quick look at my code, what am I doing wrong?

This is my class: MyClass.h

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>

#import "NSStringAdditions.h"
#import "XMLRPCResponse.h"
#import "XMLRPCRequest.h"
#import "XMLRPCConnection.h"
/**
 * END required libs
 */

extern int EID;
extern NSString * SHARED_SECRET;
extern NSString * MODE;

@interface MyClass : NSObject

+ (id)initWithEid:(int)eid secret:(NSString*)secret mode:(NSString*)mode;
+ (NSArray*)web_invoice_infoWithOCR:(NSString*)ocr pno:(NSString*)pno;
+ (NSString*)the_digest:(NSString*)source;

@end

MyClass.m

#import "MyClass.h"

@implementation MyClass

int EID;
NSString * SHARED_SECRET;
NSString * MODE;

NSString * URL_LIVE;
NSString * URL_BETA;

#pragma mark init / dealloc

+ (id)init {
    self = [super init];
    if (self)
    {

    }
    return self;
}

+ (id)initWithEid:(int)eid secret:(NSString*)secret mode:(NSString*)mode
{
    self = [super init];
    if (self)
    {
        if (![[mode lowercaseString] isEqualToString:@"beta"] && ![[mode lowercaseString] isEqualToString:@"live"])
        {
            @throw ([NSException exceptionWithName:@"Invalid mode" reason:[NSString stringWithFormat:@"Invalid mode '%@' selected. Should either be 'beta' or 'live'", mode] userInfo:nil]);
        }

        EID = eid;
        SHARED_SECRET = secret;
        MODE = [mode lowercaseString];
    }

    return self;
}

+ (NSArray*)web_invoice_infoWithOCR:(NSString*)ocr pno:(NSString*)pno {    
    NSArray *params = [NSArray arrayWithObjects:ocr, EID, pno, [MyClass the_digest:[NSString stringWithFormat:@"%d:%@:%@", EID, ocr, SHARED_SECRET]], nil];

    NSLog(@"Array: %@", params);

    return params;
}

- (id)xmrpc_call_function:(NSString*)method parameters:(NSArray*)params
{
    // Not finished yet, Warning exists
}
[...]

Having a look at my code, you'll notice the +(id)init function. I have tried -(id)init, -(id)initialize, +(id)initialize, +(void)initialize, -(void)initialize.

This is how I "load" my class in my AppDelegate:

[MyClass initWithEid:1234 secret:@"1234" mode:@"BETA"];

EDIT
I am trying to initiate my class the same way e.g. Flurry does. Example:

[FlurryAnalytics startSession:@"1234"];

Upvotes: 0

Views: 797

Answers (1)

rckoenes
rckoenes

Reputation: 69469

You need to alloc it first:

MyClass *myClass = [[MyClass alloc] initWithEid:1234 secret:@"1234" mode:@"BETA"];

Upvotes: 3

Related Questions