15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift の extension で変数を仕込もうとしたら、ObjectiveC と競合しているとエラーがでた時

Last updated at Posted at 2016-01-22

##概要

SwiftでUIViewのframe操作を楽にするextension
http://www.muratayusuke.com/2015/03/21/swift_uiview_extension/

上記サイトを参考に、UIViewcenter というプロパティを仕込もうとした

extension UIView
{
    var center: CGPoint {
        get { return CGPoint(x: midX, y: midY) }
        set { self.frame = CGRect(x: newValue.x - w / 2, y: newValue.y - h / 2, width:  w, height: h) }
    }
}

しかしエラーがでてコンパイルできない
さてどうしよう

Getter for 'center' with Objective-C selector 'center' conflicts with previous declaration with the same Objective-C selector

##解決策

@nonobjc を挿入するだけでおk

extension UIView
{
    @nonobjc
    var center: CGPoint {
        get { return CGPoint(x: midX, y: midY) }
        set { self.frame = CGRect(x: newValue.x - w / 2, y: newValue.y - h / 2, width:  w, height: h) }
    }
}

参考:http://stackoverflow.com/questions/29457720/compiler-error-method-with-objective-c-selector-conflicts-with-previous-declara

##備考
@objc は情報でてくるけど @nonobjc はないっぽい気がしたので

ていうかどっちもよくわかんないよね(´・ω・`)
Xcode7 じゃないとダメそうなことが参考ページに書いてある(と思う)ので気をつけましょう

15
13
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
15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?