Writing iPhone Apps in Swift

The new iPhone App that we are releasing was written entirely in Swift. It’s a fairly simple app and I used it as a learning opportunity to learn the ins and outs of Swift. I’ve also been blogging about what I learned on my person blog here: Code Lysis.

I have to say that writing an App in Swift is very similar to writing it in Objective-C , the one major difference being the syntax. Otherwise, you follow the same patterns as in Objective-C. You still have an AppDelegate, you still need UIViewController subclasses, etc…  I didn’t use  a lot of Swift specific language features other than optionals because most of what you need when writing iOS Apps is contained inside UIKit which are all Objective-C classes that are translated to Swift classes. What I did find refreshing however was that the syntax was cleaner and easier to read/write.

This is also our first App that has iAd support enabled.  Using Swift to add iAd support was quite a challenge as well, especially because the example code that Apple provides is written in Objective-C and seems a bit outdated. I’ll be adding a blog post about the challenges of adding the iAd banner to the App on my personal blog fairly soon.

This new app also enabled me to learn how to use the Storyboard to create the views and how to use segues to transition between UIViewControllers. In the process, I also learned how to write custom segues, which I hope to write about soon as well.

Tags: , , , ,

XCP 1.6 Console Connections

I was working using the previous post found in the archive (blog.beefyapps.com/?p=338) on a new XCP 1.6 Host that I was setting up and when I was trying to connect into the new console for the VM that I just created and it kept splitting the input between different terminals.  I would start entering commands in one and then it would switch the input to the second terminal and then the display would be on one terminal and the input going into another and it would switch back and forth every dozen characters or so.

I searched around on the internet and was unable to find anything speaking to this, but then I found a post which suggested that I use a console command that I hadn’t previously used:

xe console vm="vm name"

As soon as I started using that everything worked fine. I noted that it is not listed in the auto-complete of the xe command.

Ruby ODBC and MSSQL delivering x96 Character

We recently overcame a problem where we were trying to pull data from Microsoft’s SQL Server into Ruby for some processing. First, the SQL Server we were connecting to was configured to use a case insensitive form of latin1. You can find your encoding by executing:


In our case we were getting a: SQL_Latin1_General_CP1_CI_AS. Now, we were making use of the fine Ruby ODBC library, and we tried to have the library do a conversion to UTF-8 for us, by setting the ODBC::UTF8 constant to true before proceeding. Although, we didn’t get what we’d expect. Our algorithm ran fine until we tried to convert one of the VarChar columns to UTF-8, and we ran into the following error:

Encoding::UndefinedConversionError: "x96" from ASCII-8BIT to UTF-8

Now looking at the string, we could see a single character “x96″ in the string returned from the ODBC library. Querying the database through the Management Studio we found that the offending character was a hyphen. Now looking at 0×96 (150 in decimal) in the extended ASCII table we find a dash. The problem is that our keyboards have a hyphen, or a 0x2d (45 in decimal). This can be verified in the interactive ruby interpreter:

ruby-1.9.2-p290 :001 > "-".ord
 => 45
ruby-1.9.2-p290 :002 > "x96".ord
 => 150
ruby-1.9.2-p290 :003 > 45.chr
 => "-"
ruby-1.9.2-p290 :004 > 150.chr
 => "x96"
ruby-1.9.2-p290 :005 > "x96".encode('UTF-8')
Encoding::UndefinedConversionError: "x96" from ASCII-8BIT to UTF-8

No dash for you!
The easiest way to get rid of this problem was to just substitute the dash for a legit hyphen

Tags: , , , ,

PostgreSQL > MySQL (Facebook MySQL Article Response)

I saw this original article via LinkedIn and a conversation ensued on Google+ and I thought some of the comments were worth posting here.

The original article: http://gigaom.com/cloud/facebook-trapped-in-mysql-fate-worse-than-death/

The question was raised, that because PostgreSQL was an old SQL model, wont it have the same problems as MySQL and Wes Devauld (of BeefyApps fame) commented stating:

Wes Devauld: Postgres was built from the ground up using MVCC, which has a much smaller overhead in update transactions. MySQL InnoDB 5.1 added MVCC, but I’ve read that the implementation is half assed and they are still hamstrung on how they handle the transaction logs.

I thought it was a comment worth preserving, for posterity.

Tags: , , ,

Xen Cloud Platform; Mount Disk in different Virtual Host

Here at BeefyApps we run our infrastructure atop the Xen Cloud Platform to maximize the utility of physical hardware. Working in a virtual environment has some excellent advantages for when things do not go quite as planned. Say for instance while messing with a boot loader, or kernel you ended up with a machine that was not in a bootable state. Because all the disks are on the hypervisor, we can just take the device and mount it on another virtual machine without needing a screwdriver.

xe vm-list
xe vm-disk-list vm=<dead-vm>
xe vdb-create vm-uuid=<good-vm> device=1 vdi-uuid=<disk-vdi-from-dead-vm> mode=RW type=Disk
# returned newly-minted-uuid
xe vbd-plug uuid=<newly-minted-uuid>

First we find our <dead-vm> and <good-vm> UUIDs and keep them handy. Look up the VDI (not the VDB) from the dead host; depending on your setup, you may have a lot of disks listed for the machine, in our case we only had the system default Local storage.

With the virtual device interface we create a new block device on a good host, and plug it in. Our example uses device 1, which will create and install /dev/xvdb in the virtual machine. Using device 0 gives you /dev/xvda which is probably already in use. Inside the good host we simply mapped the partitions and mounted them

kpartx -a /dev/xvdb
mount /dev/mapper/xvdb3 /mnt
Tags: , , , , ,

Export iCal to Excel or CSV

I had what I thought was a simple task. I wanted to get a group calendar that I was putting together in Google out into a list that I could print out to pass around to the *ahem* more tactilely oriented among us.

Google Calendar does print and agenda format which is okay except for the fact that it adds in the person who created the event and the calendar that the event came from. This is great if you want to know all the information, but if you are just trying to print off a quick list for people to read, they often don’t want to look through multiple pages of repetitive text. Also, think of the tree-folk.

So I thought, I have desktop programs that can read a calendar, surely they will have a view that corresponds to my needs. I looked at iCalendar for OSX and I happened to have a copy of Entourage (first time using it, I guess it is like Outlook light?), but neither of these applications met my needs. Entourage teased me on to the point of getting me to a list that I thought looked good, but then would not let me print it.

So then I thought to myself, “self”, I said, because I always refer to myself in the third person, the second person sounds so accusatory, “you’re a programmer, how hard could this problem be?”

So I took a look at the Ruby libraries and I immediately found http://icalendar.rubyforge.org/. So I installed it

gem install icalendar

and created this little ruby program to export a CSV for my easy printing needs.

#!/usr/bin/env ruby
require 'rubygems'
require 'icalendar'
require 'date'
ARGV.each do |filename|
  file = File.open(filename)
  cals = Icalendar.parse(file)
  cals.each do |calendar|
    calendar.events.each do |event|
      puts """ + event.dtstart.strftime("%Y-%m-%d %H:%M") + "","" + event.dtend.strftime("%Y-%m-%d %H:%M") + "","" + event.summary + "","" + event.location + """

I hope this helps someone out there. I was surprised how quickly I managed to get that up and running, it took me longer to write the blog post than it did to write the code.

Tags: , , , , ,

WordPress PostgreSQL Trouble – PHP Fatal error: Cannot redeclare class wpdb

So over here at BeefyApps are using PostgreSQL instead of MySQL for our WordPress backend. There is this discussion in the WordPress codex which talks about why they do not natively support PostgreSQL out of the box.

However, we have decided that PostgreSQL is the way forward for us so we needed to find a work around. Luckily for us there is a plugin, PostgreSQL for WordPress, which will allow us to do this.

So I followed the steps that were laid out in the Readme.txt that is bundled with the package and I eventually came to run the installation (after the configuration) and I was presented with a blank white screen. Not so lucky after all…

I went through the logs and found:

PHP Fatal error:  Cannot redeclare class wpdb

Which was originating in the class wp-db.php in the wp-includes directory. This was being invoked from the wp-admin/setup-config.php file.

After some searching on the internet I came across some other posts talking about modifying the wp-functions.php file and some others which didn’t really seem appropriate because they would be overwritten with an upgrade. I did find that in the wp-admin/install.php file, there was a reference to wp-db.php as part of the installer. I commented this section out and the installer ran successfully.

I think this problem occurs because before the db.php file can run and eval the class definition inside the wp-db.php file, after it makes it’s replacements for the file, the install.php file has already loaded and processed that class into memory. When we try to redefine the class it throws an error. Anyway it seems to work now.

Tags: ,

Connecting to a Xen Console

So earlier I had one solution for connecting to a Xen domU instance that you had created, but I just came across another, more native way and I thought I should include it up here.

Once you have created your domU guest you can find it’s domain id by entering:

xe vm-list params=dom-id uuid=<uuid-of-vm> --minimal

That will give you the domain id and then you just plug that into:

/usr/lib/xen/bin/xenconsole <dom-id>

and you are off to the races.

I would like to credit the XVP installation guide for pointing that out to me:

Tags: , ,

UITextView Scrolling Doesn't Work

I had an issue where I was unable to get scrolling on a UITextView to work when editing was turned off. I searched and searched through the vast sea of information that is the internet and came up empty.

Basically the problem was that I had nested my UITextView inside of a UIImageView. This seems innocuous enough but UIImageView does not have the userInteractionEnabled flag set to YES by default. Therefore the event was dispatched up to that view from the main thread and was blocked because the view would not handle and propagate the event.

So the solution was to set the UIImageView’s userInteractionEnabled flag to YES.

    self.userInteractionEnabled = YES;

I think there could be other views that are set up like this as well, although I am not sure which ones they would be. So watch out for this, especially if you are defining your views in code.

I hope that this shows up in someone’s Google search and helps them out.

Tags: , , , ,

Custom Fonts in iOS 4

Up till now there hasn’t been an easy way to add custom fonts to your iPhone applications.  As of iOS 4 it has become very easy to do.  Here is what you need to do in order to add custom fonts:

  1. Add your custom font files into your project using XCode as a resource
  2. Add a key to your info.plist file called UIAppFonts.
  3. Make this key an array
  4. For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
  5. Save info.plist
  6. Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:12] to get the custom font to use with your UILabels and UITextViews, etc…

It’s that simple!