LoginSignup
3
3

More than 5 years have passed since last update.

【Swift】warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.

Last updated at Posted at 2015-12-15

Swiftでタイトルのようなランタイムエラーが出たので、メモ。

問題の実行コード

UILabelを拡張して、Interface Builder上でテキストの下線の有無の設定ができるようにしようとしていました。


extension UILabel {

    @IBInspectable
    var underline: Bool {
        get {
            return self.underline
        }
        set {
            guard let text: String = self.text else {
                return
            }
            let textAttributes =  NSMutableAttributedString(string: text)
            if newValue {
                textAttributes.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
            } else {
                textAttributes.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleNone.rawValue, range: NSMakeRange(0, text.characters.count))
            }
            self.attributedText = textAttributes
            self.underline = newValue
        }
    }
}

エラー全文

warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.

エラーの内容

ググってみたところ、以下のような事例が見つかり再帰的な呼び出しで無限ループに陥っている模様。

参考

その他の情報は英語情報がほとんどでよくわからず、、

結論

var underlinesetterの中でnewValueを自身に代入するという盛大な勘違い処理をしてしまっていたことが問題でした。
このため、setterの中で再度setterが呼ばれ、またsetterが呼ばれ、、という無限ループになっていたようです。

ということで、19行目の
self.underline = newValue
を削除で修正できました。

基本的なところが抜けているな、、恥ずかしい :sweat_smile:

ちなみに

上記ExtensionのコードはGithubにおいています。
https://github.com/taji-taji/TJExtension/tree/master/TJExtension/TJExtension

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3