NSString initWithBytes:length:encoding:

Code Snippets and Hints: NSString instance methods


            

NSString initWithBytes:length:encoding instance method. A quick example. Uses a NSData object from the content of my blog’s first page and displays the string in an NSTextView outlet:

- (void)awakeFromNib
{
    ALATest();
    myURL = [NSURL URLWithString:@"https://www.alauda.ro"];
    myData = [NSData dataWithContentsOfURL:myURL];
    myString = [[NSString alloc] initWithBytes:[myData bytes] length:[myData length] encoding:NSASCIIStringEncoding];
#ifdef DEBUG
    NSLog(@"Data is: %@", myString);
#endif

    [_myTextView setString:myString];
}

// memory cleanup
- (void)dealloc
{
    [myString release];
    [super dealloc];
}

The result:

NSString initWithBytes

A variant, with a char array:

- (void)awakeFromNib
{
    //myURL = [NSURL URLWithString:@"https://www.alauda.ro"];
    //myData = [NSData dataWithContentsOfURL:myURL];

    char myChar[] = {"Testing"};

    myString = [[NSString alloc] initWithBytes:myChar length:sizeof(myChar) encoding:NSASCIIStringEncoding];

#ifdef DEBUG
    NSLog(@"Data is: %@", myString);
#endif
    [_myTextView setString:myString];
}

// memory cleanup
- (void)dealloc
{
    [myString release];
    [super dealloc];
}

The result:

Using a char

Using LLDB Commands in XCode 4

The rationale for Apple’s move to LLVM and slowly parting with the aging GCC has a long history and is out of the scope of this brief tutorial on LLVM commands. The primary reason for switching from GCC to Clang — probably — is the incompatibility of GCC’s GPL v3 license with the goals of […]

CSS generators for fast web page styling

While revamping my blog, I ran over several CSS generators that are freely available on the web. These considerably speed up the process of styling your web. So I though would be nice to share some links. Some might be interested. Gradient CSS generators One of the best CSS generators is ColorZilla, available also as […]

CSS3 Generator — CSS generators

Leave a Reply

Your email address will not be published. Required fields are marked *