2017-02-24 28 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