うちのいぬ Tech Blog

Tech Blog of Uchinoinu/My dog

時計の様に、ある関数を定期的に実行する様な仕組み

呼び出し方

NSTimer.scheduledTimerWithTimeInterval(
            1, // 単位は秒で間隔を指定
            target: self,
            selector: #selector(ThisClass.methodName),
            userInfo: nil,
            repeats: true
        )

実行

func methodName() {
        print("pikachu!")
}

UIViewControllerでやるなら

    override func viewDidLoad() {
        super.viewDidLoad()

        updateDateLabel()
        NSTimer.scheduledTimerWithTimeInterval(
            1,
            target: self,
            selector: #selector(ThisViewController.updateClock),
            userInfo: nil,
            repeats: true
        )
    }

    func updateClock() {
        datetimeLabel.text = Util.stringFromNSDate(NSDate(), format: ""yyyy-MM-dd HH:mm:ss"")
    }

Result

f:id:susanne:20160804211908g:plain

Get weekday String from NSDate by Swift

Easy way

    class func weekdayFromNSDate(date: NSDate) -> String {

        let cal = NSCalendar.currentCalendar()
        let comp = cal.components(NSCalendarUnit.Weekday, fromDate: date)
        let weekdayIndex = comp.weekday

        let formatter: NSDateFormatter = NSDateFormatter()
        formatter.locale = NSLocale(localeIdentifier: NSLocaleLanguageCode) // set locale
        print(formatter.shortWeekdaySymbols[weekdayIndex]) // show 日, Sun
        print(formatter.weekdaySymbols[weekdayIndex]) // show 日曜日, Sunday
        return formatter.shortWeekdaySymbols[weekdayIndex]
    }

Customize

    class func weekdayFromNSDate(date: NSDate) -> String {

        let cal = NSCalendar.currentCalendar()
        let comp = cal.components(NSCalendarUnit.Weekday, fromDate: date)
        let weekdayIndex = comp.weekday

        let weeks = ["日曜", "月曜", "火曜", "水曜", "木曜", "金曜", "土曜"]
        return weeks[weekdayIndex - 1]
}

SwiftでNSDateから曜日を求める方法

割りと楽なやり方

    class func weekdayFromNSDate(date: NSDate) -> String {

        let cal = NSCalendar.currentCalendar()
        let comp = cal.components(NSCalendarUnit.Weekday, fromDate: date)
        let weekdayIndex = comp.weekday

        let formatter: NSDateFormatter = NSDateFormatter()
        formatter.locale = NSLocale(localeIdentifier: NSLocaleLanguageCode) // ローケルを指定
        print(formatter.shortWeekdaySymbols[weekdayIndex]) // 日, Sun などが表示
        print(formatter.weekdaySymbols[weekdayIndex]) // 日曜日, Sunday などが表示
        return formatter.shortWeekdaySymbols[weekdayIndex]
    }

表示をいじりたいなら

    class func weekdayFromNSDate(date: NSDate) -> String {

        let cal = NSCalendar.currentCalendar()
        let comp = cal.components(NSCalendarUnit.Weekday, fromDate: date)
        let weekdayIndex = comp.weekday

        let weeks = ["日曜", "月曜", "火曜", "水曜", "木曜", "金曜", "土曜"]
        return weeks[weekdayIndex - 1]
}

Morning Study Time Again!

This is 4th morning study time of Swift!

Morning Study, which is "Asakatsu" in Japanese, is my favorite style of studying.

But these days I had not able to have enough time to it.

This time, relaxing and studying is my most important concept!!

before, I had done this in Starbucks in Oodori of Sapporo, but now in my home.

and time time, I make easy log like below.

Log

github.com

next time is tomorrow.

朝活みたいな感じで、Swiftをもくもく勉強するのを再開してみた!

実は第4回目になります。

朝活は暫く出来ていなかったので、ちゃんと、ゆる〜くやろうと思います。

以前はスタバでやっていてとっても快適だったのですが、今は自宅で。

今回は↓な感じでログを残しながら進めていきます。

ログ

ディレクトリ構成は帰るかも

続きは明日。

In Wercker, error "Bundler failing with index file .idx too small error" is show up

Situation

Using Wercker for Building and Deploying Rails 4.x application. The Wercker version is Docker.

In the build step,

Bundler failing with index file .idx too small error

error above is occurred so many times. and in the end,

Retry exceeds max retries

showing error above, build process was stopped.

Resolve

This error already has been reported, but never been resolved

Anyway, clearing cache is the solution.

And I tried it, build step has passed so easily.

But every time using this pipeline, i should clear the cache.

It is really bothering, so I want to set the script in wercker step.

    - script:
        name: Clear bundle cache
        code: rm -rf /pipeline/cache/bundle-install/ruby/2.3.0/cache/bundler/git/*

You should set your app own path/to/file in code.

But this script may make CI Load high. (Now I've never meet any trouble yet.)

Werckerで "Bundler failing with index file .idx too small error" といったエラーが出た場合の対処

状況

普段Rails4系のアプリでWerckerをCIとして使っています。 WerckerはDocker版を使用しています。

そのBuild Stepで

Bundler failing with index file .idx too small error

といったエラーがたくさんでて、結果的に

Retry exceeds max retries

といったエラーでBuildが止まりました。

対応

このエラーは報告されていますが、まだ解決されていないようです。 ただ、対応としては、cacheを消せばいいとのこと。

実際消してやってみるとすんなりとおりました。

でも毎回消す必要が出てしまうので、werckerのstepに入れ込みたいです。

    - script:
        name: Clear bundle cache
        code: rm -rf /pipeline/cache/bundle-install/ruby/2.3.0/cache/bundler/git/*

↑のパスは各自のパスに直してください。

ただ、これをすることで CI での Load は上がるかもしれないとも言われています。(現在これといって問題は感じていませんが)