うちのいぬ Tech Blog

Tech Blog of Uchinoinu/My dog

How to fix error "Could not insert new outlet connection and deleting DerivedData doesnt work"

Environment

Situation

  • Using Storyboard, when I tried to connect IBOutlet, error "Could not insert new outlet connection and deleting DerivedData doesnt work" was shown
  • And set automatic in Assistant Editor, No Result was shown, and targeted View Controller never been shown
  • But targeted ViewController has been set already

I thought

  • Basically valid indexing resolves any problems

Try

Indexing

  • Set Index On, and automatically launch indexing
    • Command to launch it defaults write com.apple.dt.Xcode IDEIndexDisable 0

Second hand: Clear old data and retry connecting with clear Xcode

  • Clear cache
    • rm -rf ~/Library/Caches/com.apple.dt.Xcode
  • Clear DerivedData
    • rm -rf ~/Library/Developer/XCode/DerivedData/{project name}
  • Clean Build
  • Restart Xcode

Result

  • Delete DerivedData and indexed, problems was solved

エラー "Could not insert new outlet connection and deleting DerivedData doesnt work" への対処

環境

状況

  • Storyboardで、該当のコードに対してIBOutletを結ぼうとしたら、"Could not insert new outlet connection and deleting DerivedData doesnt work" というエラーがでた
  • そもそもAssistant Editorでautomaticにしたとき、No Resultとなり、対応させたはずのViewControllerのコードは表示されない
  • ViewControllerは設定済

考えたこと

  • 基本的にIndexが正確に行われていれば問題ないはず

対応

まずはIndexを待つ

  • IndexがOnになっていれば自動でIndexが走るはず
    • Onにするコマンド defaults write com.apple.dt.Xcode IDEIndexDisable 0

これでダメなら、過去のデータを消してXcodeをまっさらにしてやり直してみる

  • キャッシュを消してみる
    • rm -rf ~/Library/Caches/com.apple.dt.Xcode
  • DerivedDataを消してみる
    • rm -rf ~/Library/Developer/XCode/DerivedData/{対応するプロジェクト名}
  • Clean Build
  • Xcode 再起動

結果

  • DerivedDataを削除して、Indexが行われたらうまくいきました

How to execute some methods at regular intervals like clock

Call

NSTimer.scheduledTimerWithTimeInterval(
            1, // second
            target: self,
            selector: #selector(ThisClass.methodName),
            userInfo: nil,
            repeats: true
        )

Execute

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

In 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

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

呼び出し方

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.