Swifty Swift

I’ve been mucking around with swift, Apple’s new programming language, If you ask me – it’s pretty sweet and makes development quicker and easier to understand.

Objective Comparisons.

Defining a variable in ObjC requires type and pointer, swift does not require strict type definition however still allows for it in a more readable fashion.
// ObjC
NSString *name = @"Luke";
// Swift
var name:String = "Luke";

Initialising my object class in ObjC vs Swift – Similar length in code but swift is a clearer solution, no manual memory allocation required and clear coordinate + size definitions.
// ObjC
object *object_instance = [[object alloc] initWithFrame:CGRectMake(0,0,100,100)];
// Swift
var object_instance = object(frame:CGRect(x: 0, y: 0, width: 100, height: 100));

Writing a function in ObjC requires strict scope and return type. Calling the function requires ‘self’. Swift simplifies ‘func’ definition and calling the function is cleaner with params in normal brackets ().
// ObjC
- (void)createObject { //my code } [self createObject];
// Swift
func createObject() { //my code } createObject();


The screenshot below is a simple app I wrote that adds a circle in a random position each time the user taps the screen, I made sure that the circles cannot go outside the bounds of the View Controller by adding a range within my randomisation code.

Screen Shot 2014-08-24 at 11.20.53