无法使用[self theMethod:]调用类方法 [英] Cannot call a class method with [self theMethod:]

查看:104
本文介绍了无法使用[self theMethod:]调用类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Objective C中编写类方法.当我声明该方法时,项目会正常运行.但是,每当我尝试调用该方法时,构建都会失败.这是我的代码.

I am trying to write a class method in Objective C. The project builds fine when I declare the method. But the build fails whenever I try to call the method. Here is my code.

头文件

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController {
    //Declare Vars
}
- (IBAction) login: (id) sender;
+ (NSString *) md5Hash:(NSString *)str;
@end

源文件

+ (NSString *) md5Hash:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, strlen(cStr), result );

    return [NSString stringWithFormat:
        @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
        result[0], result[1], result[2], result[3], 
        result[4], result[5], result[6], result[7],
        result[8], result[9], result[10], result[11],
        result[12], result[13], result[14], result[15]
        ];
}
- (IBAction) login: (id) sender {
        //Call the class method
        [self md5Hash:@"Test"];
}

推荐答案

您应该这样称呼它:

[LoginViewController md5Hash:@"Test"];

因为它是一个类( LoginViewController )方法,而不是一个实例( self )方法.

Because it's a class (LoginViewController) method and not an instance (self) method.

这篇关于无法使用[self theMethod:]调用类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆