Showing posts with label Swift. Show all posts
Showing posts with label Swift. Show all posts

Thursday, March 26, 2015

Swiftコード実行遅延





ネット上で探しました、簡単な例を見つけられませんでした。こちらで自分でやってみました。
「Click Pls」をタップすると3秒の後に「Result」ラベルで「clicked」を表示しましょう。そのためにビューコントローラーに次のコードを書きました。







import UIKit

class ViewController: UIViewController {

    @IBOutlet var resultLabel: UILabel!
    @IBAction func clicked(sender: UIButton) {
        func delay(delay:Double, closure:()->()) {
            dispatch_after(
                dispatch_time(
                    DISPATCH_TIME_NOW,
                    Int64(delay * Double(NSEC_PER_SEC))
                ),
                dispatch_get_main_queue(), closure)
        }
        delay(3) {
        self.resultLabel.text = "clicked"
        }
    }

}

結果的に「Click Pls」をタップすると3秒の後に「Result」ラベルで「clicked」が表示されます。

Monday, March 23, 2015

Swift: How to Update Variable Outside the Scope of Function



Apple Swift brings some new ways of coding practices which might be challenging for developers who already are used to Java, C# and other object oriented languages. One of such areas for me was seemingly simple one - how to update variables outside of functions (which are often used as methods) due to restrictions on function scope and variable optionals in Swift. Here, i give simple example in Xcode playground where two variables (score1 and score2) are given functions to return values and then summed up for another valuable (totalScore).

import UIKit

    var score1:Int? = 6
func getScore1 () -> (Int?) {

    if score1 != nil {
        score1
    } else {
        score1 = 0
    }
    return (score1)
}

var score2:Int? = nil
func getScore2 () -> (Int?) {
    
    if score1 != nil {
        score1
    } else {
        score1 = 0
    }
    return (score1)
}

var totalScore = getScore1()! + getScore2()!

println(totalScore)

Tuesday, December 23, 2014

Introduction to Indoor Positioning Solution - Beacons

Follow on Twitter

Beacons as a new technology has emerged relatively recently in the mobile and retail industries. It has attracted attention among innovative entrepreneurs, developers and retailers due to its very precise location based capabilities to engage with mobile device users such as customers at retail stores or other places. While bringing with it potentially disruptive nature, it is a bit surprising to note that general awareness of this impactful technology is still relatively low. In this article we will introduce at high level what it is and how it works.
Basic Idea
Indoor positioning with beacons is about the right message at the right time and place to mobile device user. While GPS is well known mainstream geo-positioning technology used in mobile devices, it is actually not fully accurate when it comes to exact location in buildings, floors and layouts. When you are in a city, tall buildings distort the satellite signal. Wi-Fi also can be used for Wi-Fi enabled devices but has not been particularly successful in precise indoor positioning either. To address the challenges, beacon technology have been introduced. Apple’s iBeacon is part of iOS7 (and later versions) and also available on Android devices (not all). This new technology uses Bluetooth Low Energy and allows to place beacons (devices) in various indoor locations to transmit data for location based advertising and notifications. iBeacon can be iOS device or purposefully made beacon devices by various vendors.
From consumer or user perspective there are various use cases and applications. For example, customer downloads retailer’s app and walks into store, then notification (voucher etc.) can be sent when customer is standing on exact location (e.g. aisle) and/or hits a specific profile. Of course, for this to work user has to enable Blue Low Tooth connection on the device. Retailer also shall take into account customer profile and unique needs into consideration to be successful in such campaigns.

Application Scenarios
Applications for beacons can be many and diverse within or outside of retail industry which currently seems to be leading the technology adaptation. Retailer can combine location in the store with demographic and behavioral data of its app user to push well targeted messages or vouchers to the customers. Real-time external data like weather can be taken into account into this communication. Use beacons in retail Omni-channel strategy integrating them with other CRM communication channels.
According to iMore.com, McDonald's trial with beacons in Columbus, GA to guide customers to its stores using Bluetooth LE and an app developed by beacons supplier Piper, has led to increase in sales of McNuggets by roughly 8% in the store. According to Kajsa Dahlberg, Head of Media & Digital at McDonald's Sweden (see presentation at http://www.vmob.co/resources/events/retail-insight-breakfast-ny/recordings/) McDonalds needs to go from mass to personal – called 1-1 communication. By engaging in real-time segmentation, McDonalds can ‘Push’ visitor an offer selected meal offering based on his or her intent, likes lifestyle and location.
There could be plenty of use cases outside of retail – for example, finding your friend in a large, crowded building visiting first time (shopping mall, hospital etc.) or attaching a beacon device to a pet to control (notify you via an app) that it does not go too far from your home.
In museum, when in front of piece of art, beacon technology could trigger information about the exhibit when user stands in from of it. Beacon technology could well fit into so called IoT (Internet of Things) architecture.

Vendors
Apple has been on the forefront with its iBeacon technology. Beacons can be enabled for transmission function in iOS7 (and higher) devices or special beacon devices can be purchased from Apple’s partners, placed in store or other locations and take different form from iPhone or iPad devices. As the industry is picking up, more players are coming into play. PayPal and Qualcomm have announced plans for hardware. Multiple retail focused vendors have included or are planning to include beacons into their retail solution architecture. In the future posts, we will have a closer look into beacon integration with CRM and other mainstream enterprise platforms to enhance consumer experience.

Monday, December 22, 2014

Stack Overflow for Swift is Speedy - 4 Solutions in Less than 50 Min


Apple's Swift is a new programming language and as new development platform or tool it takes time for the community and knowledge base to pick up while the official manuals might not give the necessary depth of insight.
While playing around and actually trying to build a new native mobile app for iPhone, one come across an issue (even seemingly simple task in other programming languages) which does not allow you to move forward and takes unpredictably long time to resolve.
As you cannot find the answer on the Internet or official manuals for Swift, I found posting a question on Stock Overflow to be quite valuable. For an issue i spent long hours of my weekend i received 4 answers in less than 50 minutes while combining  it with other already posted question and answer I was able to resolve the issue and move forward with my first native app for iPhone.