Tuesday, May 14, 2013

NSString initWithCharactersNoCopy length freeWhenDone example ios


[NSString initWithCharactersNoCopy length freeWhenDone]

Returns an initialized NSString object that contains a given number of characters from a given C array of Unicode characters.
- (id)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)flag
Parameters of [NSString initWithCharactersNoCopy length freeWhenDone]
characters
A C array of Unicode characters.
length
The number of characters to use from characters.
flag
If YES, the receiver will free the memory when it no longer needs the characters; if NO it won’t.
Return Value of [NSString initWithCharactersNoCopy length freeWhenDone]
An initialized NSString object that contains length characters from characters. The returned object may be different from the original receiver.
Special Considerations of [NSString initWithCharactersNoCopy length freeWhenDone]
If an error occurs during the creation of the string, then bytes is not freed even if flag is YES. In this case, the caller is responsible for freeing the buffer. This allows the caller to continue trying to create a string with the buffer, without having the buffer deallocated.
Example of [NSString initWithCharactersNoCopy length freeWhenDone]
@implementation NSData (Hex)
- (NSString*)hexString {
    unichar* hexChars = (unichar*)malloc(sizeof(unichar) * (self.length*2));
    unsigned char* bytes = (unsigned char*)self.bytes;
    for (NSUInteger i = 0; i < self.length; i++) {
        unichar c = bytes[i] / 16;
        if (c < 10) c += '0';
        else c += 'A' - 10;
        hexChars[i*2] = c;
        c = bytes[i] % 16;
        if (c < 10) c += '0';
        else c += 'A' - 10;
        hexChars[i*2+1] = c;
    }
    NSString* retVal = [[NSString alloc] initWithCharactersNoCopy:hexChars
                                                           length:self.length*2 
                                                     freeWhenDone:YES];
    return [retVal autorelease];
}
@end
Example of [NSString initWithCharactersNoCopy length freeWhenDone]
NSString *CreateHexStringWithData(NSData *data)
{
    NSUInteger inLength = [data length];
    unichar *outCharacters = malloc(sizeof(unichar) * (inLength * 2));

    UInt8 *inBytes = (UInt8 *)[data bytes];
    static const char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    NSUInteger i, o = 0;
    for (i = 0; i < inLength; i++) {
        UInt8 inByte = inBytes[i];
        outCharacters[o++] = lookup[(inByte & 0xF0) >> 4];
        outCharacters[o++] = lookup[(inByte & 0x0F)];
    }

    return [[NSString alloc] initWithCharactersNoCopy:outCharacters length:o freeWhenDone:YES];
}
Example of [NSString initWithCharactersNoCopy length freeWhenDone]
- (NSString*)correctOCRErrors:(NSString*)string
{
    BOOL hasError = NO;
    for (int i = 0; i < [string length]; ++ i)
    {
        if (isIncorrect([string characterAtIndex:i]))
        {
            hasError = YES;
            break;
        }
    }

    if (hasError)
    {
        unichar* buffer = (unichar*)malloc([string length]);
        for (int i = 0; i < [string length]; ++ i)
        {
            unichar chr = [string characterAtIndex:i];
            if (isIncorrect(chr))
                chr = correctChar(chr);
            buffer[i] = chr;
        }
        string = [[[NSString alloc] initWithCharactersNoCopy:buffer length:[string length] freeWhenDone:YES] autorelease];
    }

    return string;
}