Added Online Resources Page

I’ve just added a new page to this site: Online Resources for iOS developers. If you are not already familiar with them, the videos posted by Stanford University are worth a look. They are available on iTunes and YouTube. I only have the iTunes links up there at the moment. I will add the YouTube ones soon.

Posted in News | Leave a comment

Quick Performance Measurements

A quick and easy way to measure the performance of a piece of iOS code is to dig down below all the Cocoa Touch stuff and use the low-level mach_absolute_time. This call returns a tick count that can be converted into nanoseconds using information obtained from the mach_timebase_info call. The following code sketches out the technique:

#import <mach/mach_time.h> // for mach_absolute_time

double MachTimeToSecs(uint64_t time)
{
    mach_timebase_info_data_t timebase;
    mach_timebase_info(&timebase);
    return (double)time * (double)timebase.numer / 
                (double)timebase.denom / 1e9;
}
    
- (void)profileDoSomething
{  
    uint64_t begin = mach_absolute_time();
    [self doSomething];
    uint64_t end = mach_absolute_time();
    NSLog(@"Time taken to doSomething %g s", 
                MachTimeToSecs(end - begin));
}

The timebase values on the simulator, at least on my MacBook Air, are 1/1, however on the iPad and iPhone 4 they are 125/3; so don’t be lazy and hard code them.

Never assume that because something runs quickly on the simulator it will also run quickly on a target device. On two jobs last year I encountered code that took 100x longer to run an iPad than in the simulator.

Reference
Apple Technical Q&A QA1398

Posted in Code Snippets | Tagged , , , , | 1 Comment

Handy UIKit Debugging Functions

I don’t know how many times I had typed something like

NSLog(@"self.bounds (%g,%g,%g,%g)"
        self.bounds.origin.x,
        self.bounds.origin.y,
        self.bounds.size.width,
        self.bounds.size.height);

before I discovered that UIKit has the following handy string conversions functions

NSString* NSStringFromCGAffineTransform (
                    CGAffineTransform transform);


NSString* NSStringFromCGPoint (
                    CGPoint point);


NSString* NSStringFromCGRect(
                    CGRect rect);


NSString* NSStringFromCGSize(
                    CGSize size);


NSString* NSStringFromUIEdgeInsets(
                    UIEdgeInsets insets);

Reference
UIKit Function Reference

Posted in Code Snippets | Tagged , , , | Leave a comment