iphone - 初始化我自己的 api 类

标签 iphone objective-c ios xcode ios5

我有一个 API 类,我们称之为 MyClass,我希望它在我的整个应用程序中都可用。因此,我将#import 放入MyProject-Prefix.pch 文件中。

现在,当我(在我的 appDelegate 中)启动类时,我收到错误消息 cannot init a class object.。 我知道我错误地开始了我的类(class),但我不知道我应该怎么做......而且由于这在开发中并不常见(如我所见)所以没有太多信息可以通过谷歌找到(或者我搜索错误;))

所以,我有两个问题:

  1. 是否有名为“为傻瓜创建 Objective-C API”的链接? ;)
  2. 快速查看我的代码,我做错了什么?

这是我的课: 我的类.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"];

编辑
我正在尝试以相同的方式开始我的类(class),例如乱舞。示例:

[FlurryAnalytics startSession:@"1234"];

最佳答案

你需要先分配它:

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

关于iphone - 初始化我自己的 api 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359421/

相关文章:

iphone - sqlite错误数据库被锁定

iphone - 整个应用程序中的静态 UIImage

ios - 在 iOS 上使用 Storyboard显示自定义键盘构建

objective-c - 一个窗口是否可以仅针对另一个窗口以模态方式呈现(作为其自己的窗口,而不是作为工作表)?

ios - 如何修复 AVcapture session 的未解析标识符 'stopRunning' 错误

ios - 委托(delegate)的几个 UIAlertViews

iphone - IOS 8 - 方向更改后,导航栏被状态栏遮挡

iphone - 如何使用嵌入式 HTML 检查 youtube 视频是否存在

ios - 允许在一个部分中仅检查一个 UITableViewCell

objective-c - 为什么plist中的字符串不能在UITableView中正确显示?