565Stripping Characters from an NSString

NSString *stripped = [unstripped stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\t "]];

Well, Cocoa, a bit of too much syntactic nutrasweet here. Take a look at PHP and wheep:

$stripped = trim($unstripped);

(Yes, I am aware that’s somewhat of an unfair comparion, but still…)

564NSData to NSString and vice versa

From NSData to NSString:

NSString *s = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];

And the other way round:

NSData *d = [s dataUsingEncoding:NSUTF8StringEncoding]:

484Printing Selectors in NSLog

Using NSStringFromSelector to convert the selectos into a NSString, and the print the object:

NSLog(@"%@", NSStringFromSelector(selector) );

Or print it directly with good, old-fashioned C:

NSLog(@"%s", selector,);

452Padding with Zeros (or other characters)

int x = 11;
 
[NSString stringWithFormat:@"%03i", x];
// @"011"
 
[NSString stringWithFormat:@"%+5i", x];
// @"+++11"
 
[NSString stringWithFormat:@"%+05i", x];
// @"+0011"

And not really like that. 381. I’ll still have to learn a lot of C.

383String By Appending Path Component

NSString *host = @"myhost";
NSString *videoFile = @"myVideoFileName.m4v";
NSURL *videoURL = [NSURL urlWithString[host stringByAppendingPathComponent videoFile];
 
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];

381Zero-Padding in Objective-C

The age-old problem. Files, that are stored with padded zeros 000, 001, 002, 003 – and a counter that doesn’t have this zero-padding. There are probably myriad solution to that problem, here’s my take:

Create a temporary string consisting of a minimum of 3 characters length.

NSString *tempZeros = [NSString stringWithFormat:@"00%i", i];

Get the last 3 characters, shave off the rest.

NSString *zeroPaddedIndex = [tempZeros substringFromIndex:[tempZeros length] - 3];

No if’s, no but’s, no clauses.

Update: Of course, there’s always a better way. 452

376Variable length of accuracy of float in NSString

float f = 1.23456;
 
NSLog(@"%.2f", f);
NSLog(@"%.0f", f);
NSLog(@"%.0f", f);

1.23
1
1.23456

246writeToFile – quick file writing

Works with:

NSDictionary
NSArray
NSData
NSString

For more complicated purposes, NSOutputStream might be the best option, but for simply writing a date or even XML this might be the simplest and fasted way.

212NSString and NSMutableString

Should have been clear, but was not:

NSString *s = @"aString";
s = @"anotherString";
NSLog(@"%@", s);
// anotherString
 
NSMutableString *m = @"aMutableString";
[m appendString:@"andAnAppendedString"];
NSLog(@"%@", m);
// aMutableStringandAnAppendedString

NString allows for the replacement of its whole content by simply assigning a new value.

With NSMutableString it is possible to add/delete/insert strings at arbitrary places in a string.

201Changing NSMutableString into NSString

NSMutableString *s;

-(void)functionA:(NSString *)string {
[self.s appendString: string];
}

-(void)functionB:(NSString *)string {
NSString* c = self.s;
NSString* c = [NSString stringWithString: self.s];
}

“Returns a string created by copying the characters from another given string.”

self.s is a refernce to s, might change over time, stringWithString makes a copy, a “snapshot” of self.s…