Blah, Blog!

Leap Motion App with Swift (Part 1)

I have recently started playing with SceneKit and working with one of our Unity developers on creating interactive demos using the Kinect. I feel like my mind has opened up to new ways to use this new virtual reality hardware to think of new ways of interacting with my apps (I know I am a little late to the game but whatever). Getting the leap motion SDK set up using swift was very straight forward. You can see my Github project here as I make progress: [LeapFace]( **Step one:** Copy the files from the leap SDK. You will need the following files. You can download them from the [Leap Motion Site]( Leap.h LeapMath.h LeapObjectiveC.h libLeap.dylib **Step two:** Add a new "Copy Files Phase" under build phases to your project. Add the libLeap.dylib to the list of files in the newly created Copy Files phase and make sure you set the destination to "Executables" ![Build phases] ( **Step three:** Create your bridging header (in the case of the example LeapFace-Bridging-Header.h) and add the following line: // // Use this file to import your target's public headers that you would like to expose to Swift. // #import "LeapObjectiveC.h" **Step four:** Initialize your leap controller to start capturing events from the device: Whichever controller you want to interact with the Leap motion device let leapController = LeapController()

Swift arrays are not classes

I have been working with swift for a little over a year now and I was going back and reading the Big Nerd Ranch book and was surprised that I had not encountered this in my projects. var anArray = [2,3,4,5,1,2,1,4,5] func changeArray(mArray: [Int]) { let firstElement = mArray[0] mArray[0] = mArray.last! mArray[mArray.count - 1] = firstElement } changeArray(anArray) Since Swift's arrays are structs the value of anArray remains the same. The reason Swift structures are passed by value not by reference. In order for the above to work as intended it would need to pass a reference of the memory location that anArray is contained in (See below): var anArray = [2,3,4,5,1,2,1,4,5] func changeArray(inout mArray: [Int]) { let firstElement = mArray[0] mArray[0] = mArray.last! mArray[mArray.count - 1] = firstElement } changeArray(&anArray) In the above code we are defining a function which takes a reference of a Foo struct by specifying the "inout" directive. We also have to change where we are calling "changeArray()" to pass in a reference by adding the "&" before the anArray variable. Classes on the other hand are always passed by reference. class Foo { var name = "Pooch" } func changeFooName(foo: Foo) { = "Kangaroo" } let foo = Foo() print("Foo name is \(") changeFooName(foo) print("Foo name is \(") The above will change the name of the Foo object to "Kangaroo" since it is implied that we will be passing a reference to the changeFooName() function.

Swift's if-case statement

I have found myself repeatedly writing if conditions to try match a specific range which can become unruly. Example: if let httpResponse = httpResponse where httpResponse > 200 && httpResponse < 300 { print("httpResponse is \(httpResponse)") } The more complex the ranges you are looking to capture the longer the conditions can become. That is where the if-case comes in handy. The above can be rewritten to: if let httpResponse = httpResponse, case 200...300 = httpResponse { print("httpResponse is \(httpResponse)") } Viola, no need to endlessly chain && ||

Site converted to markdown

### Markdown js I recently got frustrated at Redcloth and Textile and decided to use something a little more light weight and familiar for my blog. Enter [markdown.js]( . I was already pretty familiar with markdown from github wikis and it seemed like a better option. In the process I wound up adding [Bower] ( for installing and managing npm packages directly through the Rails asset pipeline. This offloaded the markup processing to the client as well and removed the Redcloth dependency, which was overkill for what I was trying to accomplish. The hardest part for me was figuring out how to include the asset pipeline files. Once I had the bower gem installed. The missing piece to get this to work was updating my bower.json, Bowerfile and app/assets/javascripts/application.js to look like so: Bowerfile: +asset 'markdown' bower.json: { "lib": { "name": "bower-rails generated lib assets", "dependencies": {} }, "vendor": { "name": "bower-rails generated vendor assets", "dependencies": {} } } app/assets/javascripts/application.js: //= require markdown/lib/markdown The javascript that I added to my view was as simple as: $("article .content").each( function() { $(this).html(markdown.toHTML($(this).html())); }); Viola, a client side markdown js parser done.


One of the most intimidating things when I started mobile development was trying to figure out how to ingest json from API services. After some playing around with NSURL and JSON kit I thought to myself something like this has to have been done enough times that someone has written a library around it. After some quick google searches I came across "ASIHTTPRequest": but the maintainer seems to have dropped supporting this so I kept looking until I came across "RestKit": . Within 10 minutes I was parsing the JSON from my API server and up and running. The overall steps are simple. Set up your object manager, I set mine up in my Application delegate as for what I was doing I needed the data loaded as soon as the app loads. Also since it is using a single pattern. The same object manager is available through out the app as long as the Application delegate retains the object: self.objectManager = [RKObjectManager managerWithBaseURLString:@""]; [RKObjectManager setSharedManager: self.objectManager]; next set up your object mappings. I put the methods to do the object mappings in the class that it was being mapped to, in my case User.c: +(void) loadRestkitMappings { RKObjectManager *sharedManager = [RKObjectManager sharedManager]; RKObjectMapping *userMapping = [RKObjectMapping mappingForClass: [User class]]; [userMapping mapKeyPath:@"_id" toAttribute:@"userId"]; [userMapping mapKeyPath:@"token" toAttribute:@"token"]; [userMapping mapKeyPath:@"email" toAttribute:@"email"]; [userMapping mapKeyPath:@"first_name" toAttribute:@"firstName"]; [userMapping mapKeyPath:@"last_name" toAttribute:@"lastName"]; [userMapping mapKeyPath:@"current_location.latitude" toAttribute:@"currentPositionLat"]; [userMapping mapKeyPath:@"current_location.longitude" toAttribute:@"currentPositionLng"]; [sharedManager.mappingProvider setObjectMapping:userMapping forKeyPath: @"user"]; [sharedManager.mappingProvider setObjectMapping:userMapping forKeyPath: @"users"]; [sharedManager.mappingProvider setObjectMapping:userMapping forKeyPath: @"friends"]; [Direction loadRestkitMappingsWithUserMapping: userMapping]; } Note in the above code. The direction class has a call here which passes in the user mapping. The reason behind this is the User has many directions so I pass in the user mapping after I map the attributes. Below in the Direction class I map the actual object: +(void) loadRestkitMappingsWithUserMapping: (RKObjectMapping *) userMapping { RKObjectManager *sharedManager = [RKObjectManager sharedManager]; RKObjectMapping *directionMapping = [RKObjectMapping mappingForClass: [Direction class]]; [directionMapping mapKeyPath:@"_id" toAttribute:@"directionId"]; [directionMapping mapKeyPath:@"user" toRelationship:@"user" withMapping:userMapping]; [directionMapping mapKeyPath:@"end_coordinates.x" toAttribute:@"endCoordinateLatitude"]; [directionMapping mapKeyPath:@"end_coordinates.y" toAttribute:@"endCoordinateLongitude"]; [sharedManager.mappingProvider setObjectMapping:directionMapping forKeyPath: @"direction"]; [Route loadRestkitMappingsWithDirectionMapping: directionMapping andUserMapping: userMapping]; } the relative portion above is the mapKeyPath:toRelationship which maps the User object on Direction. set up your RKObjectLoader delegate and the 2 required methods: @interface FriendsViewController : UITableViewController <UITableViewDelegate, RKObjectLoaderDelegate></code> delegate - (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error used to handle any connection errors - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects (NSArray*)objects used to actually retain your objects that have been mapped successfully. Lastly make the request and specify that the calling class is the delegate NSString *userSessionPath = [NSString stringWithFormat: @"/api/v1/users.json?auth_token=%@", currentUser.token]; [sharedManager loadObjectsAtResourcePath: userSessionPath usingBlock: ^(RKObjectLoader *loader) { loader.method= RKRequestMethodGET; loader.delegate = self; }]; That is basically the gist of it. Check out the documentation on "RestKit": for more details.

Android tiling images with bitmaps

Recently I was banging my head trying to figure out how tile an image for the background of a view with a given image. Now for those of you that are familiar with IOS this is a simple one liner: self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: @"some_image"]]; With android it is not quite that simple. Hidden through pages and pages of documentation and many google searches I finally found something along the lines of: <layer-list xmlns:android="" > <item android:id="@android:id/background"> <bitmap xmlns:android="" android:src="@drawable/bg_bars_rpt" android:tileMode="repeat" /> </item> </layer-list> And in the xml for the progress bar: <ProgressBar android:id="@+id/lowest_model_score" android:progress="22" android:progressDrawable="@drawable/grey_progress_bar_states" style="@android:style/Widget.ProgressBar.Horizontal" /> The above is essentially used to draw the background of a progress bar view. This still does not work as smoothly as it does in IOS. it produces the following progress bar which seems skewed. See below: ![image]( VS IOS: !!