I have done I custom class method for NSString to md5 a NSString.
This is my code:
NSString+CustomMethod.h
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
@interface NSString (CustomMethod)
+ (NSString*)MD5:(NSString *)string;
@end
NSString+CustomMethod.m
#import "NSString+CustomMethod.h"
@implementation NSString (CustomMethod)
+ (NSString*)MD5:(NSString *)string
{
// Create pointer to the string as UTF8
const char *ptr = [string UTF8String];
// Create byte array of unsigned chars
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
// Create 16 byte MD5 hash value, store in buffer
CC_MD5(ptr, strlen(ptr), md5Buffer);
// Convert MD5 value in the buffer to NSString of hex values
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x",md5Buffer[i]];
return output;
}
@end
This Methodclass works great, but compiler give me a warring:
warning: class method ‘+MD5:’ not found (return type defaults to ‘id’) [3]
How can I remove this warning ??
PS: if a put #import “NSString+CustomMethod.h” no warning is shown, but it’s a workAround, I have created a custom method class not to include my custom class everywhere I need it
Thanks for any helps !!
Put
#import "NSString+CustomMethod.h"in.pchfile or in file where you want to use it.