Search
Tweets

Entries in Objective-C (2)

Wednesday
Jul062011

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!

Sunday
Jun122011

Names

I haven't written in a while. I've been heads down working on a personal development project. I'll talk about it more at another time.

Objective-c is fun, and writing for a platform as exciting as the iPad and iPhone brings it's own rewards, but thank the stars for perl. I'd never get through all the data I need to wade through without perl.

Time to get back to work. Enjoy this great vid from Adele!