Codec ░ NSString

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 484Printing Selectors in NSLog 452Padding with Zeros (or other characters) 383String By Appending Path Component 381Zero-Padding

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]:

565Stripping Characters from an NSString 484Printing Selectors in NSLog 452Padding with Zeros (or other characters) 383String By Appending Path Component 381Zero-Padding

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,);

565Stripping Characters from an NSString 564NSData to NSString and vice versa 452Padding with Zeros (or other characters) 447Stripping out NSLog() from the Release Build 383String By Appending Path Component

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.

632Inline Functions in C 565Stripping Characters from an NSString 564NSData to NSString and vice versa 484Printing Selectors in NSLog 447Stripping out NSLog() from the Release Build

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];

565Stripping Characters from an NSString 564NSData to NSString and vice versa 484Printing Selectors in NSLog 452Padding with Zeros (or other characters) 381Zero-Padding

381Zero-Padding

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 a better way. 452

565Stripping Characters from an NSString 564NSData to NSString and vice versa 484Printing Selectors in NSLog 452Padding with Zeros (or other characters) 383String By Appending Path Component

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

565Stripping Characters from an NSString 564NSData to NSString and vice versa 484Printing Selectors in NSLog 452Padding with Zeros (or other characters) 447Stripping out NSLog() from the Release Build

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.

576NSDictionary and NSArray plist examples 565Stripping Characters from an NSString 564NSData to NSString and vice versa 484Printing Selectors in NSLog 452Padding with Zeros (or other characters)

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.

569Layering one UIImage onto of another UIImage 565Stripping Characters from an NSString 564NSData to NSString and vice versa 560#import vs #include in Objective C – A quick reminder 484Printing Selectors in NSLog

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…

565Stripping Characters from an NSString 564NSData to NSString and vice versa 484Printing Selectors in NSLog 452Padding with Zeros (or other characters) 383String By Appending Path Component