Swift Weekly – Issue 02 – The Swift Runtime (Part 1)

In this edition, I wanted to write about arrays and dictionaires and take the easy route. But I thought to myself: wouldn’t be cool if _somebody_ dug deep into the Swift runtime for crying out loud? Then I thought that I cannot wait for somebody to do that so I’m going to have to do that myself. So here, this edition of Swift Weekly is about the Swift runtime. At least the basics.

Please note that I am using a disassembler + dSYM file. I am disassembling the contents of the AppDelegate with some basic code in it and then hooking my disassembler up with the dSYM file to see more details.

Also in this article I am testing the output disassembly of Xcode 6.1 on the x86_64 architecture, not ARM which is available on iOS devices.

Continue reading this article on Swift Weekly’s Github page: https://github.com/vandadnp/swift-weekly/tree/master/issue02

Loading a Nib file, Programmatically (Objective-C)

If you want to load a Nib file at run time by simply allocating and initializing a View object, then you should take a rather strange approach to how you create your class files. Suppose you have a subclass of UIView called MyView and what you want is to allocate and initialize an instance of MyView but have MyView load its outlets and actions from a Nib file. Well, you will need to do two things:

  1. In Interface Builder, change the class name of your View object to MyView.
  2. Then you will have to override MyView’s initWithFrame method like so:

- (id)initWithFrame:(CGRect)paramFrame
{

NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MyView"
owner:nil
options:nil];

if ([arrayOfViews count] < 1){
[self release];
return nil;
}

MyView *newView = [[arrayOfViews objectAtIndex:0] retain];
[newView setFrame:paramFrame];

[self release];
self = newView;

return self;

 

}

Then you can go ahead and initialize your view like this:

MyView *myView = [[MyView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:myView];
[myView release];

And here an instance of MyView will get added to the view of a view controller. That simple. I hope it helps some of you out there 🙂