Thursday, 21 May 2015

Borrow & Lend

I don't usually carry cash around. I've gotten to the point where I pay for everything using my credit card. It's even easier to do this now that we have the tap to pay feature in our credit cards here in Canada. But occasionally, when I go out with my co-workers for lunch, I end up at a place that takes cash only and I have to borrow $10 or $20 from someone to pay for lunch. I wanted a way to keep track of this and so I wrote the Borrow & Lend App.

You can not only track the money you lend or borrow from people, but you can track anything. For example, if you lent out a DVD, borrowed a book, or just owe someone a nice dinner for helping you move, you can track it in the Borrow & Lend App.

I wrote this application entirely in Swift and using only Apple provided libraries, so no need for CocoaPods or any other 3rd Party library. My secondary motivation for writing this App was to learn Swift and I learned a lot (which I'll be writing about soon).

Try the app out and let me know what you think. It's now available on the App Store.

Monday, 22 September 2014

Working With Core Data in Swift

In general, working with Core Data is similar to how you would do it in Objective-C. You use the Data Model Design tool to create your managed object model. When creating your project, if you selected "Use Core Data", Xcode will automatically create the functions to create and get the NSPersistentStoreCoordinator, NSManagedObjectContext, and set up your Apps document directory, and even provides a function to save your context.

After you create your entities in your managed object model, you can automatically generate the Swift class files for your entities by selecting your data model file (.xcdatamodeld), and selecting Editor->Create NSManagedObject subclass.

One gotcha is that in Swift all of your classes are namespaced to the module they are compiled in (typically this is your project). For example, if you have a project called MyProject and you have a class in it called MyModel, in order to refer to the class in your managed object model, you will need to use MyProject.MyModel.

This means that after you have created your entity models in your managed object model and generated your Swift classes for your entities, you will need to go back and prefix "MyProject." to the class name in the Class field in the Data Model inspector panel for each of your entities.

Otherwise you will get the following error:
CoreData: warning: Unable to load class named 'MyModel' for entity 'MyModel'. Class not found, using default NSManagedObject instead.
This is clearly stated in Using Swift with Cocoa and Objective-C. But, I would imagine that if I generated the Swift classes from the model file, I would have this automatically set for me. The fact that I have to set this manually seems like a short fall in Xcode. Hopefully Apple will fix this soon.

Saturday, 20 September 2014

Learning Swift

One of the biggest news out of WWDC14 was the release of Apple's new programming language: Swift.

I've worked with Objective-C and written a few applications for iOS but most of my day job involves programming in Python (and Java), so when Swift was announced I was pretty excited! I could write iOS apps in a higher level scripting language AND it gets compiled down to native code thanks to LLVM AND Apple has made all of Cococa accessible through Swift!

It has also gotten a lot of hype lately:

Over the next few weeks I'm going to undertake learn Swift and write some simple Apps. I'll use this space to blog about my journey and post any interesting things I find out about Swift along the way.