Showing posts with label Development. Show all posts
Showing posts with label Development. 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)

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.