Objective-C Tricks (not really!)
Common tasks need to be done in any programming language. Everything from converting an integer to a string, to printing dates in a certain format. The fun part is figuring out these tricks.
As usual, google is often the answer. Seriously...how did we program before google?!? Well, for those to young to know, we had shelves and shelves of reference books.
Anyway, here are a couple of tips I've had to track down recently. I'll add more as I run across them.
Compare strings for equality
if ([currentString isEqualToString:@"newString"]) {
// Is equal
}
Convert integer to string
NSString *newString = [NSString stringWithFormat:@"%d", myInt];
Concatenate multiple strings
NSString *newString = [stringOne stringByAppendingFormat:
@" - %@ - %@", stringTwo, stringThree];
// returns concatenate as follows:
// stringOne - stringTwo - stringThree
Get decent random numbers
int rndNumber = arc4random() % 100;
// returns a random number between 0 to 99
Check string for nil or blank
if ([myString length] == 0) {
// string is nil or blank
}
String with current date/time
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss aaa"];
NSString *dateSaved = [dateFormatter stringFromDate:aDate];
// returns 2011-07-06 09:30:25 AM
Good for now. Again, as I run across new items that fit with this list, I'll post.
Enjoy!
Objective-C,
Programming,
Technology 