Archive | March 2016

SAP HANA, xcode, Cocoa and Swift

So as many of you know I work with SAP HANA quite a bit in terms of events, developer evangelism, etc.

Well the other day I decided I would give Swift a try, I wrote about it before. So I thought what better way to drive myself a bit nuts then to try and connect an existing SAP HANA XSODATA service.

The service is one I use in an IoT demo that basically takes the value from a temperature sensor and stores it in a SAP HANA table. The service when called returns a JSON formatted response that I can use in other applications to show active values for different sensors.

  {"results": [
            "type": "sap.devs.demo.iot.IOTType",
            "uri": ""
         "SVALUE": "25.169"

Nothing fancy and outside of the whole demo really unimpressive, but for my purposes it was a perfect way to attempt my hand at something I’d not tried before despite reading about it and being extremely curious.

Therefore up comes my xcode environment on my Mac and following the wizard to create a basic Cocoa application.

Screen Shot 2016-03-24 at 14.45.28

With very basic settings and a project name.

Screen Shot 2016-03-24 at 14.45.45

From there it was a matter of adding a label or a two and a button to my Main Storyboard and then linking the label and button into the View controller.

Screen Shot 2016-03-24 at 14.47.27.png

Now inside my View Controller I added a timer control so I would be able to continuously update the value from the server itself.

timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.doUpdate), userInfo: nil, repeats: true)

Then I added my doUpdate function that actually calls the service and retrieves the XSODATA value. Now I really just wanted to show how I was able to read the SAP HANA xsodata and I’m sure you OSX and iOS developers would be able to do this more efficiently but the results I think would be the same.

func doUpdate() {
        let url = NSURL(string: "\(proto)://\(server):\(port)\(path)\(param)")!
        let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
            if let urlContent = data {
                do {

                    let json = try NSJSONSerialization.JSONObjectWithData(urlContent, options: []) as! NSMutableDictionary
                    let lv_value = json["d"]?["results"]! as? NSArray
                    let lv_temp_value = lv_value![0]["SVALUE"]! as! String
                    //print("lv_temp_value: ",lv_temp_value)
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.lblTemp.stringValue = "\(lv_temp_value) \(self.temp_unit)"
                } catch {
                    print("error: \(error)")
    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.

You’ll notice I also use some parameters for my url creation just because I thought it looked cleaner than the really long url string.

var proto = "http"
    var server = "xx.xx.xx.xx"
    var port = "80xx"
    var path = "/sap/devs/demo/iot/services/iot.xsodata/IOT"
    var param = "?$orderby=ID%20desc&$top=1&$select=SVALUE&$filter=SNAME%20eq%20%27Office1%27&$format=json"
    var temp_unit = "° C"

The parameters refer to the SAP HANA Developer Edition that you can find via the SAP Developers website.

The end result is just a quick little application that I can now expand to duplicate the existing SAP HANA XS application if I wanted to.

Screen Shot 2016-03-24 at 14.58.11

Sometimes a demo is just waiting for you to get over that first initial – what the hell moment! For me it was this.

let lv_value = json["d"]?["results"]! as? NSArray
let lv_temp_value = lv_value![0]["SVALUE"]! as! String

After I managed to get that bit of code and actually access the part of my JSON response I needed the rest was easy. Sometime I’ll share the larger demo now converted to OSX.