Russell Broadbent

Yorkshire based web developer

Objective C – part 1

A great Video Tutorial series on YouTube from My Bring Back demonstrating how basic Objective C code works.

The first 5 videos show how to assign variable integers and floats and also demonstrate how to round these variables when outputted to the NSLog.

Example code from Tutorial 5

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        float x = 4.52343;
        int y = 2.89345;
        float sum = x + y;

        NSLog(@"%.f + %i equals %.2f", x, y, sum);

    }
    return 0;
}

The ‘%f’ indicates that a float will be substituted into the outputted string, a single period ‘.’ will round the float to the nearest whole number and a ‘.2′ will round the float to 1 decimal places.

Also note that the substituted pointers are ‘%f’ for a float, ‘%i’ for an integer.

Comments are closed.