Xcode 6.3 woes

Swift Language

Xcode 6.3 woes

This is a note to myself, but it may be of help to you.

I downloaded Xcode 6.3 final and also installed OS X 10.3.3. No problems with the installation.
The next day, I fired up Xcode and it froze. The spinning beachball of misery. It happened repeatedly. I had to force quit.

What to do?

How I did it

The first thing I did was to delete the derived data directories in
~/Library/Developer/Xcode/DerivedData. That didn’t fix it.

So, how to open Xcode and have it not reopen any projects that it had open?
It seems there are a few ways.

If Xcode is in your dock, you can option-shift click on it and it won’t open any projects.

Or, you can get rid of all your autosave info like this.

Finally, you can open Xcode from the command line with this incantation.

Table of Contents


Open Xcode without opening any projects. That might fix the problem.
So far so good. I can get back to work now.

Posted in Apple | Tagged | 4 Responses

Swift and C API callbacks

Swift Language

Swift and C API callbacks

Swift is supposed to have been designed for compatibility with existing Apple APIs, including C APIs such as CoreMIDI.


Core MIDI is Apple’s C API for (surprise) MIDI. Apple provides no higher level bindings besides a player in AVFoundation. If you want to develop an app with MIDI capabilities, you need to use Core MIDI.

Like many C APIs, there are several callbacks:

  • MIDICompletionProc
  • MIDINotifyProc
  • MIDIReadProc

In context, the very first thing you’d do is to create a MIDI client. In Swift 1.2 beta, MIDIClientCreate finally works.! See my posts on the problem and the solution.

One of the parameters to MIDIClientCreate is a function pointer (MIDINotifyProc) to be called when MIDI inputs or outputs in the system have changed, such as plugging in a new device.

Another place where callbacks are used is reading MIDI input. A MIDIReadProc is called when data appears.

Table of Contents

Creating Function Pointers

Ok, so how do you make a CFunctionPointer?
Here is my attempt.
It looks like CFunctionPointer needs a COpaquePointer which needs an UnsafeMutablePointer or UnsafePointer.

BTW., the CFunctionPointer source code has this amusing comment:

CFunctionPointer is a struct with inits that will take an UnsafeMutablePointer or an UnsafePointer.

So, it looks like we need to make an unsafe pointer, use that to create an opaque pointer, then use that to create a function pointer (and a partridge in a pear tree).

Will this work with a MIDIReadProc?

It doesn’t work though with Core MIDI.
And it doesn’t matter if MyMIDIReadProc is a func, a class func, or a global func.

Here is the love letter from Core MIDI:

Table of Contents


Swift support for C APIs is improving. Given this problem with function pointers though, it is far from being usable.


Posted in Swift | Tagged , | 1 Response

Swift 1.2 beta2 and CoreMIDI

Swift Language

Swift 1.2 beta2 and CoreMIDI

Someone is actually working on CoreMIDI with Swift.

The Problem

Prior to Swift 1.2, various CoreMIDI types defined in MIDIServices.h were architecture dependent. Like this:

This mean that you had to deal with nonsense like this:

I blogged about the problem with creating a MIDI client ref in a 32-bit architecture. (Hint: don’t try).

Now in Swift 1.2 beta wer get this:

Those Opaque structs are now history. Yay.


Let’s look at the MIDI Services reference.

Here’s a gem. Note the return type.

You would use this like so:

ItemCount is defined in MacTypes. Or it was until now. Right now there is a commment for it but no definition.

Workaround. Go ahead and define it yourself as it was pre-Swift 1.2.

Or just don’t use it.

Table of Contents


There is progress with using CoreMIDI from Swift.
There are still potholes though.


Posted in Core MIDI, Swift | Tagged | 1 Response

Swift Framework creation

Swift Language

Swift Framework creation

This is my preferred way to set up a Swift framework project.


Sometimes, you will find yourself using the same custom code in multiple projects. Simply copying the files into a new project works, but then you have different versions. So, a better way to reuse that code is to create a Framework.

This is my preferred way.There are other ways that The Google will show you. I had luck with none of them – especially at link time. One example has you dragging the framework from the simulator’s temp directory via the Finder. Yikes.

Without a workspace, iterative development on the framework and a potential client of it will be awkward. So, I suggest creating a workspace and adding the framework project to it along with at least one app project that uses the framework.

Table of Contents

Creating the projects

You will create a framework, a Single Page App that uses it, and a workspace to make your life easier.

Create a Single Page App.

Close it.

Create a Framework.

Close it.

Create a Workspace.

Choose Add Files (⌥ ⌘-A or right click or choose from the File Menu).
Add the .xcodeproj files for your framework and your app to the workspace.

If you didn’t close your framework project or your app, it will not be added correctly. You will see just your .xcodeproj file and not the project.

You should see something like this in your project navigator.


Table of Contents


Click on your Framework project.
For the scheme, make certain that your framework is selected and then choose IOS Device.
Like this:

Build the framework. (⌘-B)
Open up Products and MyFrameework.framework should not be red anymore but black.

Now select your App and choose the General tab for the app target.
Drag MyFrameework.framework to “Embedded Binaries”
Notice that Linked frameworks and Libraries also picks this up.

You’re ready!

Table of Contents

A Framework Class

Create a class in your framework. Be certain that the class and the function your will be calling are both public. If you define init functions, then they also need to be public.

Build (⌘-B)

Table of Contents

Try it out

For a simple test, open your view controller. You need to import your framework. If you named it MyFramework, simply specify MyFramework.

Now use your class from the framework. When you build, check the scheme to make sure you’re building the app and not the framework!

It’s fairly simple to go back to the framework, add classes and modify them, build, then go back to the app and try them out.

Table of Contents


Create a framework, an app that uses it, then add both projects to a workspace.
Build the framework, then drag the framework product to “Embedded Binaries” on the general tab of your app’s target.


Posted in Swift | Tagged , , | 1 Response

Swift fail: MIDIClientCreate

Swift Language

Swift fail: MIDIClientCreate

There is a problem with calling Core MIDI’s MIDIClientCreate function from Swift.


Let’s start with a simple call to Core MIDI’s client create function. You need the midi client to create MIDI input and output ports.

Works great!

Table of Contents


So, what’s the problem?

The above code compiled just fine when the scheme was an iPhone 6. I then plugged in my iPhone 4s and the problem raised its ugly head. If you don’t have an older iOS device, just select the scheme in XCode.

To verify that this was the problem I tried checking the arch and then calling separate init methods. The initial code for both was what you see in the first example here.

XCode will give you this love letter for 32 bit devices. This refers to the line where you create the client variable. (var client = MIDIClientRef())

Ok, just do this then.


Ok, then


Go back to 64 bits.

[expletive deleted]

Here are the definitions in CoreMIDI/MIDIServices.h

Well, actually in Objective-C:


Table of Contents


You can’t create a MIDI client on older iOS devices using Swift.
If you have a solution, I’d love to hear it!

In the meantime, I’ll create the Core MIDI code (i.e. creating the client and ports) in Objective-C and call that from my Swift code.


Posted in Apple, Core MIDI, MIDI, Swift | 2 Responses

Book Review: iOS 8 for Programmers: An App-Driven Approach with Swift

There is now a tidal wave of books being released on Apple’s new Swift programming language. Here, I’m going to review iOS 8 for Programmers: An App-Driven Approach with Swift (3rd Edition) (Deitel Developer Series) which was just released. For once they did not hire Yoda to write their book title as they did with Java How To Program. But they did work a colon into the title.

I have a hardcopy of the book, so I cannot speak about the quality of the ebook versions. The same content of course, but I know from producing my own epub books that the formatting can be tedious and error prone.

Readers can download a zip of the code examples from the Deitel website. Unfortunately, you have to “register” on their site to get the download link as if we are still living in 1995.

First off, the audience for the book. It is aimed at experienced programmers, especially those with experience in an object oriented language. If you are just starting out, this is probably not the book for you. If that is the case, I’d suggest Swift for Absolute Beginners which is another brand new book.

As the title suggests, this is not a Swift tutorial. Instead, you are introduced to Swift’s features by writing several toy apps. That’s what “app-driven approach” means. I really hate books and course materials that are simple laundry lists of features. In fact, over 90% of the live courses I’ve taught over the past 25 years ignored the printed course materials (unless it was one I authored :)). Laundry lists are easy on the author but hard on the learner. This app-driven approach gets closer to enabling real learning. If the learner has a question in their head while working through the material, and then see the answer a few pages later, that is excellent. Motivational seeding is what I call that. So, you will get a decent foundation in Swift, but you will not see any advanced topics. The things that I’ve banged my head against the wall with, such as interfacing with legacy APIs such are Core Audio or Core MIDI, are not touched upon. I don’t mean those APIs in particular, but interfacing with any of the legacy APIs. As is common with most iOS development books, unit testing is not covered.

The Apps

These are the Apps that the learner will build:

  • Welcome App
  • Tip Calculator App
  • Twitter Searches App
  • Flag Quiz App
  • Cannon Game App
  • Doodlz App
  • Address Book App

Each App introduces a new iOS and/or Swift feature. For example, the Cannon Game touches on Sprite Kit and the Address Book uses Core Data.

I like the format of each chapter. Each begins with a list of objectives followed by an outline. The page header for the page on the right will be an outline title. I wonder if the ebook formats the outline items as links. This seems to be a small thing, but after you’ve gone through a book, you might need to find something. This helps a lot. It also sets your expectations for what is going to be accomplished in the chapter. Not surprising, the end of each chapter has a “wrap up” telling you what they just told you. Also useful for answering “In what chapter was that thing on X covered?”

Sometimes, the author is a bit lazy. For example, section 4.3.13 talks about external parameter names. The paradigm is given but no code example. Thanks for the Amo, Amas, Amat, but where is the example sentence? Amo libri huius? Also, the Alert controller code on page 148 has a memory leak when you access the text fields in that manner. The Twitter app sidesteps Twitter’s RESTful API and uses a WebView instead. I guess NSURLSession would be too complicated or having to authenticate would be too much trouble.

There are a decent number of technologies touched upon. iCloud, Sprite Kit, Social Framework, Core Data, etc.

The book ends with a chapter on the business end and the App Store. Most developers will tell you that the coding is easier than getting it onto the App Store. Useful information is provided here.


If you are an experienced programmer, this is a good book to get to get a decent foundation in iOS development and the Swift language.
The softcover book is around 40 bucks.

You can get more information on the InformIT site.

Posted in Book Review, Swift | Tagged , | Leave a comment

iOS 8 Bluetooth MIDI LE build tip

Swift Language

iOS Bluetooth MIDI LE


iOS 8 and OS X Yosemite now supports sending and receiving MIDI data using Bluetooth Low Energy connections on any iOS device or Mac that has native Bluetooth Low Energy support.

I reminding myself here of a simple problem I had that wasted my time.

Table of Contents

The Bluetooth classes

So, I’m playing around with the new Bluetooth LE MIDI capabilities.
Im my build settings I include the CoreAudioKit framework in order to get
the new Core Audio Bluetooth MIDI (CABTMIDI) controllers CABTMIDILocalPeripheralViewController and CABTMIDICentralViewController.

You also get the Inter-App audio classes CAInterAppAudioSwitcherView and CAInterAppAudioTransportView with CoreAudioKit, but I’m not using them here.

Here is a very simple view controller example.

I played around with it, then had to do other work. I came back to it a week later, and it wouldn’t even compile. I didn’t change anything (e.g. no XCode updates). Yes, the CoreAudioKit is indeed included, but the error was one the “import CoreAudioKit”. The compiler didn’t know what that was even though the framework is there and I can even see the headers in the XCode UI tree under CoreAudioKit.framework.

It turns out that the build scheme needs to have a device selected, and not any of the simulator choices. Even if you are building and not running. The device does not need to be attached. You can just choose the first item: iOS Device. Then it will build.


Apple even says so in a tech note (that I did not know existed). See the resources below.

Table of Contents


Bluetooth LE MIDI support will build only if a device is selected.


Posted in Apple, Computer Music, MIDI, Swift | Tagged , , | Leave a comment

BASH script to create a git bare repo

I can’t count how many times I’ve created a project in my IDE then dropped to the terminal to create a bare git repository, then add that as a remote to my project. And also add/commit/push. So, I decided to make my life a bit easier by writing a small shell script to do all this nonsense. You might find this useful as is or for parts you can copy and paste.

BTW., If I’m the only one working on a project right now, I find that creating the bare repo on Dropbox is handy. This won’t work of course if multiple developers are pushing at the same time.


Posted in Uncategorized | Tagged , , , | Leave a comment

Unit testing async network calls in Swift

Swift Language

Asynchronous unit testing in Swift

You have probably written code with a NSURLSessionDataTask that notifies a delegate when the data is received. How do you write a unit test for that?


Let’s stub out some typical code. Here a an API function that takes perhaps a REST endpoint and a delegate that receives a Thing instance. I use a NSURLSessionDataTask because I’m expecting, well, data (as JSON). I’m not showing the gory details of parsing the JSON since that’s not my point here. BTW., it’s not very difficult to parse. The idea is that a Thing is instantiated and the delegate is notified.

Table of Contents


So, how do you write a unit test for this kind of code? The API call does not return anything to pass into XCTAssertTrue or siblings. Wouldn’t it be nice if you can make the network API call and wait – with a timeout of course – for a response?

Previously, you’d have to use semaphores, a spin loop, or something similar. Since this is such a common scenario, Apple gave us XCTestExpectation in XCode 6. (Actually, it’s a category in XCTextCase+AsynchronousTesting.)

Here is a simple usage example. I have an instance variable of type XCTestExpectation because I need it in the delegate callback in addition to the test function. I simply instantiate it, make the network call, then call one of the new wait functions. In this case waitForExpectationsWithTimeout. When the delegate is notified, I fulfill the expectation. If you don’t, the test will fail after the timeout.

Table of Contents


Simple huh? Take a look at the documentation for a few variations.


Posted in iOS, Swift | Tagged , , | Leave a comment

Swift and AVMIDIPlayer

Swift Language

Swift and AVMIDIPlayer

How to play MIDI data via the AVFoundation AVMIDIPlayer.


Previously, I wrote about attaching a low level core audio AUGraph to a MusicSequence to hear something besides sine waves when played via a MusicPlayer. Here, I’ll show you how to use the new higher level AVMIDIPlayer. You can even play a MusicSequence by sticking your elbow in your ear.

Playing a MIDI File

Preparing an AVMIDIPlayer to play a standard MIDI file with a SoundFont or DLS file is fairly straightforward. Get both NSURLs from your bundle, then pass them into the init function.

Note that I’m passing nil to the play function. It expects a completion function. It will crash if you pass in either a function or a closure. My workaround is to pass nil.

Play your MIDI file in the simulator, and you’ll hear sine waves. Huh? A valid SoundFont was sent to the init function, and you hear sine waves? Yeah. After you spend a day verifying that your code is correct, install iOS8 on your actual device and try it there. Yup, it works. Nice.

ps. that slider thing is just some eye candy in the final project. A UISlider moves while playing.

Table of Contents

Playing NSData from a file

AVMIDIPlayer has an init function that takes an NSData instance instead of a URL. So, let’s try creating an NSData object from the URL and a simple first step.

Not surprisingly, that works. But why would you want to do this?

Table of Contents

Playing a MusicSequence

The hoary grizzled MusicSequence from the AudioToolbox is still the only way to create a MIDI Sequence on the fly. If you have an app where the user taps in notes, you can store them in a MusicSequence for example. But AVMIDIPlayer has no init function that takes a MusicSequence. Our choices are an NSURL or NSData.

A NSURL doesn’t make sense, but what about NSData? Can you turn a MusicSequence into NSData? Well, there’s MusicSequenceFileCreateData(). With this function, you can pass in a data variable that will be initialized to the data that would be written to a standard MIDI file. You can then use that NSData in the player code in our previous example.

I haven’t checked to see if there is a memory leak with the takeUnretainedValue call. I’ll check that out next.

update: I checked and there is indeed a small memory leak.
The docs for MusicSequenceFileCreateData say that the caller is responsible for releasing the CFData. So OK, takeUnretainedValue is the right one. So I tried saving the data variable as an ivar, checking for nil when playing again, then calling release(). Crash. What about DisposeMusicSequence? OK, I tried saving the sequence as an ivar and calling that. No crash, but memory still leaks. CFRelease is simply unavailable.

What do you think? Advice?

Table of Contents


So you can play a MusicSequence with sounds via an AVMIDIPlayer. You just need to know the secret handshake.


Posted in Computer Music, Swift | Tagged , | 4 Responses