Setting bool, int and other properties to a Parse object

Working with Parse is great. It’s quick, easy, scalable and for the most part, the documentation and community support is fantastic. I just want to share (more as a reference than anything else) a quick list detailing how to set certain property types like bool and ints to a PFObject in Objective-C. I’m also including how to set a Pointer to another PFObject, both automatically and manually. It’s really easy, but not immediately obvious the first time you have to do it. See below:


Declaring a block as a method parameter in Objective-C

Just a quick post as I came across an extremely helpful website dedicated to those of us who consistently need to declare Objective-C blocks in our applications. Most often, I require them when writing network operations and want to provide a callback once the operation has been completed (you could use NSOperation for this as well). Take the example below:

In the code above, we declare a callback block which takes one argument of type NSString. I think you’ll agree calling it is also quite easy.

Check this website out for more resource on objective-c block declarations.

Reading and writing NSArray into Core Data with Transformable types

Core Data is great, but coming to grips with it can be a bit of a headache for first time users. Out of the box, iOS allows you to declare model properties with a variety of types: String, Float, Date, Boolean, Int 16/32/64, but annoyingly, not arrays. Enter Transformable types.

With Transformable types, you can store anything as NSData and load it back to a format that’s useable for you. This includes, NSArrays.

This brief post assumes you already know how to get setup with Core Data, and how to create an NSManagedObject subclass of your own Entity.

Take the example below:

Above, we create a new Car entity which has an attribute named “wheels” of the Transformable type. We set this attribute to be the wheelsArray we declared earlier, and save.

How do we get this data back? All we do is initialise a new NSArray with the contents of car.wheels and we’re good to go. In a real application, we would have more code in place to perform various checks and error handling, but this should give you a good idea of what’s required:

Scrollview autolayout constraint setup in Xcode

This seems to be quite a common issue for people using the Interface Builder and AutoLayout in Xcode to manage a ScrollView. The main headache that people encounter is the Scrollview just doesn’t scroll, which is pretty useless. But the solution for this is actually pretty simple, it just takes a bit if tinkering to get it right.

Firstly, you want to put all your content inside a View and then embed that view in the Scrollview. In your inspector, the hierarchy would look like the image below:

Screen Shot 2015-12-11 at 15.29.29

The “Scroll View” here has only one child, everything else that appears in the Scroll view should be a child of this “View”.

The next thing you want to do is add constraints to the top, bottom, trailing and leading spaces on the Scrollview:

Screen Shot 2015-12-11 at 15.32.36

Now comes the interesting part, you need to set a constraint on the “View” child in Scrollview to be equal widths to the main View in your ViewController. To do this, ctrl drag from the child View to the main View, and select “Equal Widths” on the popup that appears:

Screen Shot 2015-12-11 at 15.37.18

Next, ctrl drag from the View to the ScrollView, and add constraints for top, bottom, trailing and leading as we did earlier.

If you were to run your app now, everything would appear correctly, but the ScrollView would still not scroll. This is because we need to tell the child View how high it is. This is the important bit. We do this by adding a “Bottom Space to Container” constraint on the bottom child element of the View. In the image below, our bottom element is a button called Finish:

Screen Shot 2015-12-11 at 15.41.28

In the image above, I have ctrl dragged from the button “Finish”, to the parent view, “View”, and selected the “Bottom Space to Container” constraint. Run your app now, and you should have a fully working ScrollView constructed with AutoLayout!

The important thing to remember is whatever element you have at the bottom, make sure you set a bottom constraint to the parent view. In the example above, if I were to add another button below the “Finish” button, I would have to remove the bottom constraint (to parent view) on the “Finish” button, and add a bottom constraint (to the parent view), on the new button I had just added.

Getting your app bundle version in Swift

Just a quick post, more for reference than anything else. It’s common practice nowadays as an App Developer to display the current app version somewhere in your app. Sometimes in loading screens, login screens, or in menus. It’s super easy to do this, all you need is the following code:

If your app version was 1.2.3, the above code would set the text property of a UILabel named appVersion to “v 1.2.3”.

Migrating to Swift 2

I was recently tasked with migrating an iOS codebase from Swift 1.2 to the latest Swift version, which at the time of writing is 2.1. Anyone else having to do this will no doubt, like myself, have had mixed results.

If your codebase is small, the Xcode migration tool should take care of most of it for you. However, if your codebase is more mature, you’re going to run into some issues!

Unfortunately for me, the app I was migrating fell in the “super mature” category. I still decided to run the migration tool and see how it coped – not well is the answer. It took about 20 minutes to run and the most helpful thing it did was update some Classes to new versions (e.g. Printable becomes CustomStringConvertible). It also changed some occurrences of “var” to “let”, which got rid of some of those pesky warning messages. But other than that, everything else had to done manually.

When trying to build my app, it instantly failed with hundreds of errors….

The first thing I noticed, were some new function signatures that now throw errors, a new feature of Swift. These new function signatures are basically there to say, “Hey, this function could make a boo boo, so you need to make sure you cater for this”. It’s actually quite straightforward to handle, take the example below:

If for some reason, you can guarantee a function will never fail, you can get rid of the do/catch block and simply write:

Note the difference here of “try!”, not “try”.

There was also an update to SCNetworkReacabilityFlags which means you can no longer compare it to an Int using the equality operator. Instead you now use the .contains() function to do the same thing. Where previously you would write something like:

You now achieve the same result by doing:


There are many more APIs that were updated, luckily for me these were the main and most frequently occurring ones I had to deal with.

Happy migrating folks.

Crop image in UIScrollView with Swift

I recently had a project where I had to crop a UIImageView based on it’s scale, after having been resized within a UIScrollView. The solution to this task turned out to be incredibly simple – see the code below.

imageScrollView – reference to our UIScrollView
editingImage – reference to our UIImage used to create our UIImageView which itself is a child of imageScrollView.

Happy coding 🙂


6 Apps in 6 months

For the past few weeks, I’ve had some down time from client projects. It’s always nice when this happens, not only because I’m not stressing trying to get a project finished in time, but also because it gives me time to think more about my ideas, the products I want to build and the things I am passionate about.

I’ve decided to set myself an ambitious target. I want to make 6 business focused iOS apps within 6 months, aiming for 1 every month.

Why you might ask? For me the answer is a simple one. I want to push myself and see what I can do. I’m under no illusions that this will be a huge amount of work, especially because I’ll be undertaking this at the same time as running a business, doing client work and having a personal life (one that I’m quite fond of).

I’ve always believed that if you want something enough, and you work hard enough for it, you can achieve anything. I also believe that you need the right people around you, pushing you, making sure you don’t stray from that goal. For me, those people will be my family, my work colleagues and my blog. Regularly updating my blog will incentivise me to get off my arse and actually do the work. Because when I come round to writing a weekly update, if I haven’t got anything to say, I know I haven’t worked hard enough that week.

As with any mammoth task, it helps to break it down into smaller more digestible chunks – this is no exception. Each app will have 4 milestones (1 per week) that need to be hit in order to stay on track. The milestones will be different to each app of course and I’ll be updating them as I go.

I plan to begin this on the first Monday of each month, beginning March 2nd 2015. Wish me luck, and stay tuned for updates!



Simple audio recording using Swift

Just a quick post on how to get audio recording happening on iOS using Swift.

All you need to do is copy this into your UIViewController.

The code itself is quite self explanatory. Make sure at the top of your class to include the line, “import AVFoundation”, this contains the libraries necessary to interact with the audio file system on iOS.

toggleRecording() – The function to call to toggle your audio recording on/off. You’ll need to setup a button linking to this function.

That’s it. Playing the file back is also quite straight forward, but I’ll include that in a later post.


Microphone input wave like Siri using Swift

In recent weeks I’ve been developing an audio recording app in my spare time, just for fun.

I wanted to be able to implement a Siri like wave that animates in response to sound input via the microphone as below:

siri wave

Luckily I was able to find an Objective-C implementation on Github called SCSiriWaveformView which was very well documented – thanks to Stefan Ceriu for this.

Since I’m focussing mainly on Swift these days, I’ve put together a Swift version including the files downloaded from Github. The full source code is below.

If you’re unsure on how to import and use Objective-C files in Swift, check out my earlier post on how to do this here.