2017-02-24 102 views
1

我是新来的ios开发。UIView方便的初始化器在哪里

在Xcode中,我可以找到UIView有两个初始化程序。

@available(iOS 2.0, *) 
open class UIView : UIResponder, NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, CALayerDelegate { 


    open class var layerClass: Swift.AnyClass { get } // default is [CALayer class]. Used when creating the underlying layer for the view. 


    // default is [CALayer class]. Used when creating the underlying layer for the view. 

    public init(frame: CGRect) 

    public init?(coder aDecoder: NSCoder) 


    ...... 
} 

这是我的问题。

  • in open class,public init(frame: CGRect) has no method body?
    我认为只有协议可以有空的实现。

  • 在RCTViewManager,我们始终创建无参数

代码

override func view() -> UIView! { 
    let ins = CustomUIView() 
    return ins; 
} 

一个UIView是这样方便初始化定义在哪里?我无法在任何地方找到它。

回答

4

in open class,public init(frame: CGRect) has no method body? 我认为只有协议可以有空的实现。

init(frame: CGRect)确实有一个方法体。它只是没有显示。你所看到的是接口UIView。你想想要看到的是UIView的源代码,我认为你不能得到。

在RCTViewManager中,我们总是创建一个没有参数的UIView。这个方便的初始化器在哪里定义?我无法在任何地方找到它。

因为几乎一切都在UIKit延伸NSObject,几乎所有的东西会沿用在NSObject定义的参数的构造函数:

enter image description here

+0

非常感谢。 xcode跳转到的文件是UIKit.UIVIEW.swift,它不是实现吗?我认为迅速,接口和实现已经合并在一起。 –

1

UIView的子类UIResponder是NSObject的子类。 NSObject有一个初始值为

public init() 

因此,在这里你不需要一个方便的初始值设定项。我希望这回答了你的问题。

+0

thx。 NSObject中的public init()是它的声明。 –

+0

是的@徐东武你是对的。 – swiftyAmit