我有一个API类,我们称之为MyClass,我想在整个应用程序中使用它。因此我将#import放入MyProject-Prefix.pch
文件中。
现在,当我(在我的appDelegate中)启动该类时,我收到错误消息cannot init a class object.
。
我明白我正在错误地开始我的课程,但我不知道我应该怎么做...因为这对于开发来说并不常见(正如我所看到的)没有很多信息可以找到thrue google (或者我错了;))
所以,我有两个问题:
这是我的班级: 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
}
[...]
看一下我的代码,你会注意到+(id)init函数。我试过了-(id)init, -(id)initialize, +(id)initialize, +(void)initialize, -(void)initialize
。
这就是我在AppDelegate中“加载”我的课程的方式:
[MyClass initWithEid:1234 secret:@"1234" mode:@"BETA"];
修改
我试图以同样的方式发起我的课程,例如乱舞了。例如:
[FlurryAnalytics startSession:@"1234"];
答案 0 :(得分:3)
您需要先分配它:
MyClass *myClass = [[MyClass alloc] initWithEid:1234 secret:@"1234" mode:@"BETA"];