Cannot call a class method with [self theMethod:]

▼魔方 西西 提交于 2019-12-03 05:24:00
MByD

You should call it like this:

[LoginViewController md5Hash:@"Test"];

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

Or you could do:

- (IBAction) login: (id) sender {
        //Call the static method
        [[self class] md5Hash:@"Test"];
}

which should be exactly the same as calling [LoginViewController md5Hash:@"Test"] directly with the class name. Remember that md5Hash is a CLASS method, not an instance one, so you can't call it in objects (instances of the class), but from the class itself.

you call static methods on the class, and not on the instance. So should be

- (IBAction) login: (id) sender {
        //Call the static method
        [LoginViewController md5Hash:@"Test"];
}
Abhigyan

The + symbol indicates that you are declaring a class method. You should replace it with -. The minus sign denotes the instance method. After that you can call it with selfobject.

- (NSString *) md5Hash:(NSString *)str;

and

- (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;...... source code continued
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!