Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-[NSData initWithBytesNoCopy: length: freeWhenDone:] #3

Open
xcvista opened this issue May 29, 2013 · 1 comment
Open

-[NSData initWithBytesNoCopy: length: freeWhenDone:] #3

xcvista opened this issue May 29, 2013 · 1 comment

Comments

@xcvista
Copy link

xcvista commented May 29, 2013

You can use -[NSData initWithBytesNoCopy: length: freeWhenDone:] method to avoid an unneeded memory copy and free(), like the following.

- (id)initWithBase64String:(NSString *)aString
{
    NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding];
    size_t outputLength;
    void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength);
    return [self initWithBytesNoCopy:outputBuffer length:outputLength freeWhenDone:YES];
}
@Antson21
Copy link

Antson21 commented Dec 9, 2014

  • (id)initWithBase64String:(NSString *)aString
    {
    NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding];
    size_t outputLength;
    void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength);
    return [self initWithBytesNoCopy:outputBuffer length:outputLength freeWhenDone:YES];
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants