Zero to BLE Part Two Core Bluetooth Post Updated for Swift at Cloud City Development Blog!

I’m happy to announce that I recently updated Part Two of my Zero-to-BLE series on Core Bluetooth Post Updated for Swift at Cloud City Development Blog! It’s been updated for Swift 2, because when I wrote it, Swift 3 hadn’t officially been released and I honestly thought it would be published well in advance of the September 7 Apple Event.

Zero to BLE on iOS – Part Two – Swift Edition

Better late than never!

Who knows? Maybe there will be a Swift 3 version in the future? For sure all my code samples will be in Swift 3…

Hope you like it!

Syntax highlighting for Swift in BBEdit and TextWrangler

Big thanks to Curt Clifton (https://github.com/curtclifton – his bio indicates that he’s with the Omni Group!) for creating this codeless Language Module for Swift that can be used in BBEdit and in TextWrangler.

A BBEdit Codeless Language Module for Swift

Curt indicates there are some limitations to codeless language modules:

Keyword, comment, and string highlighting work. Top-level classes, structs, enums, functions, and extensions are indexed and can be folded. Because of limitations in the matching power of codeless language modules, nested declarations are not indexed and are not fold-able.

However, it works great if you want to do some Average Joe coding in Swift in TextWrangler or just see your .swift files highlighted outside of Xcode.

Installation is super easy:

BBEdit

  1. Copy the file swift.plist to ~/Library/Application Support/BBEdit/Language Modules. You may have to create the Langauge Modules folder if it doesn’t exist.
  2. Quit and relaunch BBEdit.

…and for TextWrangler it’s just as simple:

TextWrangler

  1. Copy the file swift.plist to ~/Library/Application Support/TextWrangler/Language Modules. You may have to create the Langauge Modules folder if it doesn’t exist.
  2. Quit and relaunch TextWrangler.

Smoother transitions when showing a View Controller with a UINavigationBar in Swift

Here’s the scenario. You want to display a modal view controller without a navigation bar, and then from that view controller you want to navigate to another view controller that displays a navigation bar.

First to get things set up, in the Storyboard we set up the view controller that would be displayed modally. We create a view controller, perform the Editor -> Embed in -> Navigation Controller. Then, in the viewWillAppear method, we hide the navigation bar programmatically.

It turns out that there are two ways to do this and, at least in my case, one way turned out to be better than the other. The first way I tried worked, but the animation for the transition from one view controller to the next looked a little odd.

The first way is the more obvious of the two, because you can simply just set the hidden property of the navigationBar like in the following example:

self.navigationController?.navigationBar.hidden = true

There is also another way to do the hiding and showing of the UINavigationBar, and the second way is do do it via the UINavigationController, as in the second example:

self.navigationController?.setNavigationBarHidden(true, animated: true)

There are two advantages to doing it the second way. First, because you get a method to hide and show the navigation bar, you get the additional animated parameter that you can set.

The second advantage is that when you use it in this way with the animated parameter set to true, iOS performs a slightly more smooth transition.

Your mileage may vary, so try both ways and see which you personally like best.

How to create an Objective-C Bridging header

Super important to know during this time of Objective-C to Swift transition. I’ve had to use this technique and it will be good for all Swift developers to have in their kit-bag until we can finally be 100% Swift.

How to create an Objective-C Bridging header – iOS-Blog
So you want to use an Objective-C Library or SDK in your Swift application eh? Well do not fret. This quick tutorial will show You how you can create an Objective-C Bridging Header in your Swift application so you can use them both together and seamlessly.

UIView Animation with Swift

Here is a very simple way to do UIView Animations in Swift.

First, you create an IBOutlet for a constraint created in Interface Builder in Xcode – in my case, I had a constraint that anchored to the bottom of the containing view. In the code example, the constraint is named “constraintToAnimate”. You just control drag from the Storyboard to your code as you would when connecting a control, but in this case you connect a constraint. It will look something like this (the name of your constraint will hopefully be different):

    @IBOutlet weak var constraintToAnimate: NSLayoutConstraint!

The following snippet is basically a straight-up conversion of Apple’s own Objective-C example given in the documentation article Auto Layout by Example in the Animating Changes Made by Auto Layout section (you may need to scroll down to find that section).

    self.view.layoutIfNeeded()
    UIView.animateWithDuration(0.3, animations: {
        // make constraint changes here...
        self.constraintToAnimate.constant = 250 // your value here.
        self.view.layoutIfNeeded()
        }, completion: {
            (value: Bool) in
            // completion code here...
    })

Basically, in pre-Auto Layout days you would just animate the frame. Today you just animate the constraint. It works great and once you get your head wrapped around it, it’s pretty straightforward.