Swift and Backend – Simple Tutorial

Since Apple released Swift’s source code, there’s been a big variety of interesting projects making good use of it’s openness. One category that has became pretty popular is application servers.

Why backend? Well, I believe most iOS developers would be pretty happy to use the same language and logic that it’s being used on their apps on the backend as well. There are some alternatives for Objective-C, but they never really took off.

Like I said, there’s a lot of options for an application server written in swift, so it begs the question : “Which one should I use?”. My personal opinion is: “It doesn’t really matter right now”. This is a brand new category for a brand new language, and dare to say, not one of them are production ready. I’d say to just pick one you think it’s easy to use, has a nice set of APIs and you think you can learn from it, and hopefully, contribute to it. I believe we’re still learning what Swift can do, and moving away from the iOS ecosystem is a very interesting way to expand your knowledge about it’s possibilities.

Swift Express

Swift Express is the one I’ve chosen to take a look at. My reasons:

  • I was able to make it up and running faster than heating my Pizza ( =~ 15min );
  • I found a bug on it and the developers were really fast in fixing it.

Hello World Tutorial

Their GitHub page is very simple to understand, and this tutorial is literally taken from their page, but I want to write here as well just to show how simple is to get it up and running with a few set of commands.


  • First, install Homebrew if you haven’t already.
  • Open your terminal and type the following commands:

brew tap crossroadlabs/tap
brew install swift-express

  • That’s it, it’s installed.

First Project

  • On your terminal, type the following commands:

swift-express init HelloExpress
cd HelloExpress
swift-express bootstrap

  • This is going to generate a Xcode project for you, everything ready to go just waiting for your code.
  • Open the Xcode project (HelloExpress.xcodeproj in this case), and add a method on your main.swift file


app.get("/myecho") { request in
    return Action.ok(request.query["message"]?.first)
  • Now run the commands on your terminal:

swift-express build
swift-express run

That’s it, your server should be up and running, to check if everything is fine, open your browser with the link : http://localhost:9999/myecho?message=Hello


Is Swift on the backend something that you’ll add to your project right away? Probably not, but all these projects are on their infancy and are pretty interesting already. I really recommend taking a look in at least one of them to broaden your Swift knowledge, it’s a nice way to start learning swift and maybe even give back to the community helping on the project development.


Android emulator is too slow. And here’s the solution…

Why can’t we have nice things?

I’m an iOS developer, and anyone else who worked with iOS knows that iOS Simulator is awesome. Fast, reliable and pretty. Sure, sometimes it doesn’t work very well, but it’s pretty close to perfect. I would add some other features, such as, using your Mac’s iSight as a camera for the simulator. -Apple? Please?-

Needless to say that to someone who it’s used to work with iOS Simulator, the Android Emulator is a *very* big disappointment. But why is the Android Emulator so bad compared to the iOS Simulator?

Emulator vs Simulator

The terminology is: Simulator is a system that behaves similar to something else, but the implementation it’s not necessary the same. An Emulator in the other hand, is a system that behaves exactly like something else. Basically:

– iOS Simulator: Runs native code on you Mac, compiled for x86 architecture.

– Android Emulator: uses QEMU to run ARM, emulating the hardware.

Click here for an interesting reading about the subject.

Enters AndroVM

You can improve the experience using AndroVM and VirtualBox. In a nutshell, AndroVM is a VirtualBox Android image ready to use, and works pretty well 😉

– First open AndroVM download page clicking here and download androVM_vbox86p_4.1.1_r6.1-20130222-gapps-houdini-flash.ova file. (if you want the tablet version, download the vboxtp version.

– Download and install VirtualBox for your system clicking here.

– On VirtualBox, go to File -> Import and select the .ova file you just downloaded.

Import .ova

– I like to clone the image, so I can mess around with the system without any problems, so, right-click on the virtual machine and select “clone” and when prompted, select “full clone”.


– Right click on the image, click on Settings. Open the Network tab, click on Adapter 1, check the “Enable Network Adapter” and attach to your adapter. This is for “adb” to connect to your virtual box image.


– Now just run your machine and play around with the device, compare it with the standard emulator and see how fast it is 🙂


How to debug your application

Now that you have your VirtualBox setup, it’s time to actually use it to debug your apps. First, on your Android Emulator, open the AndroVM app. It should be the lower left icon on your device screen. You should see the device network IP there.


– Open your eclipse project, for this example, I just created a standard “Hello World” project.

– Open the “Run Configurations” panel, click on “Target” tab. Select to “Always prompt to pick device”.

– Run your project, you should see this screen


– Now, open your terminal, and type “adb connect THE_IP_OF_YOUR_DEVICE”

– After doing that, note that the Eclipse prompt now shows your device on the list.


– Select your device and click “OK”.

– That’s it, you should see your app running on your brand new emulator 😉



This is definitely not as fast as the iOS Simulator but it’s a huge step forward. With this setup you don’t need to always have thousands of devices with you neither wait hours to have the emulator to boot up.


– If you want to use the Android back physical button, hit “Esc”

– If you want to use the Android menu physical button, hit “F1”

Enjoy 🙂

UIScrollView and Zoom

UIScrollView and Zoom
As I wrote on my last post, this one would be a simple tutorial with UIScrollView and Zoom, right? OK, let’s create a new View-based Xcode project and name it Zoom.
Open the ZoomViewController, and create your UIScrollView in the loadView method.
– (void)loadView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scroll.backgroundColor = [UIColor blackColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”image.JPG”]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
self.view = scroll;
[scroll release];
What’s being done here is:
  • Creating a UIScrollView with the same size as the main screen
  • Setting the background color to black
  • Assigning the delegate to self
  • Creating an UIImageView with  a image that’s already added on the project, don’t forget to declare the UIImageView on your .h file.
  • Setting the contentsize to the full size of the image
  • Adding the image to the UIScrollView
  • Setting the minimum zoom scale to fit the iPhone screen horizontally, so the image with the lowest zoom level, will fit the screen
  • Setting the maximum zoom to be 2 times the image size
  • Setting the UIScrollView to start with the minimum zoom scale
  • Assigning the UIScrollView to the UIViewController’s UIView
  • Releasing the UIScrollView
OK, with that ready there’s only one thing missing here, we have to implement a UIScrollView delegate method called
– (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
And what does it do? Simple, just go to the documentation and you’ll see : “Asks the delegate for the view to scale when zooming is about to occur in the scroll view.” So, all we need to do here is return our image, like so:
– (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return image;
pretty simple, huh?
That’s it, that’s all you need to do to have a simple and functional UIScrollView with zooming 🙂 But wait, there’s one more thing…

One More Thing

If you run your project, you may realize that the image is not on the center of your screen, even if you zoom, and here’s how you can center your view on UIScrollView while zooming. But no worries, I will help you out, just copy this method:

– (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {
CGSize boundsSize = scroll.bounds.size;
CGRect frameToCenter = rView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width – frameToCenter.size.width) / 2;
else {
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = (boundsSize.height – frameToCenter.size.height) / 2;
else {
frameToCenter.origin.y = 0;
return frameToCenter;
This method will receive your UIScrollView and the UIView that you are zooming and calculate the correct frame for your UIView and return a CGRect. All you have to do is call this method on your scrollViewDidZoom delegate, that’s get called everytime that there’s some zooming, and you are ready 🙂
– (void)scrollViewDidZoom:(UIScrollView *)scrollView {
image.frame = [self centeredFrameForScrollView:scrollView andUIView:image];;
Now enjoy your centralized UIView in your UIScrollView

NSLog: Logging Tips

Sometimes it’s way better to just use a simple log then debugging your app step by step. That’s when NSLog comes in handy.
There’s a lot of ways to use NSLog in your code, I’ll show some of my favorites.
int myInt = 1;
float myFloat = 2.3;
NSString *myString = @”Hello Log”;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
NSLog(@”Int [%i]”, myInt);
NSLog(@”Float [%f]”, myFloat);
NSLog(@”String [%@]”, myString);
NSLog(@”Object [%@]”, myView);
This is how the log will look like:
Int [1]
Float [2.300000]
String [Hello Log]
Object [<UIView: 0x5c07950; frame = (0 0; 20 40); layer = <CALayer: 0x5c07920>>]
What I’m doing there is, logging an Int, Float, NSString and an UIView. As you can see I always add some brackets in the log, and why do I do this? That’s only to know exactly what’s being logged. Imagine that I have a NSString with some white spaces, I will not know that it has the white spaces unless I add the brackets. Here’s an example :
NSString *test = @”Test   “;
NSLog(@”log: %@”, test);
Result : log: Test

NSString *test = @”Test   “;
NSLog(@”log: [%@]”, test);
Result : log: [Test   ]

Logging CGGeometry

Now, let’s say that you want to log some CGRect, CGPoint or CGSize, just transform them into NSString with :

Log Macro

Here’s a cool tip, although I’m not a macro fan, there’s one that’ great for logging:
#define MYLOG(fmt, …) NSLog((@”File [%s] Method [%s] Line [%d]: ” fmt),__FILE__, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
and you can use exactly like an NSLog, like so :
MYLOG(@”Hello Log”);
What this will do is:
  • __FILE__ : file path and name.
  • __PRETTY_FUNCTION__ : name of the method.
  • __LINE__ : the line of the method
You can edit this macro as you please, remove or reorder the parameters. That’s how the output will look like :
File [/Users/user/Desktop/Log/Classes/LogAppDelegate.m] Method [-[LogAppDelegate application:didFinishLaunchingWithOptions:]] Line [25]: Hello Log
That’s some simple tips that I have to share about the NSLog, if anyone has anything else to add, please share it in the comments.
That’s it, thanks for all the fishes 🙂