Codec ░ NSString

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.

376Variable length of accuracy of float in NSString 246writeToFile – quick file writing 212NSString and NSMutableString 201Changing NSMutableString into NSString 73Concatenating Strings @”string1″ @”string2″ @”stringN”

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

381Zero-Padding 305Converting float to string %4.2f 287[^//]NSLog 246writeToFile – quick file writing 218Obj-C, Shortcut: Boolean return value to String

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.

381Zero-Padding 376Variable length of accuracy of float in NSString 347UIImage → pixelData → UIImage Rountrip 338Accessing RGBA Pixel Data 243NSEnumerator: Stepping over an Array (or Dictionary)

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.

381Zero-Padding 376Variable length of accuracy of float in NSString 289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache

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…

381Zero-Padding 376Variable length of accuracy of float in NSString 246writeToFile – quick file writing 212NSString and NSMutableString 73Concatenating Strings @”string1″ @”string2″ @”stringN”

73Concatenating Strings @”string1″ @”string2″ @”stringN”

That’s basically it.

No more
[@"string1" stringByAppendingString:@"string2"];

or
[NSString stringWithFormat:@"string1%@", @"string1"];

just
@"string1" @"string2" @"stringN" -> @"string1string2stringN"

The Objective-C 2.0 Programming Language.pdf, p131

381Zero-Padding 376Variable length of accuracy of float in NSString 334Combining Images with UIImage & CGContext – (Offscreen drawing) 305Converting float to string %4.2f 246writeToFile – quick file writing