TL:DR将其粘贴到Swift游乐场:
import UIKit
public protocol NibLoadable {
static var nibName: String { get }
}
extension NibLoadable where Self: UIView {
public static var nibName: String {
return String(describing: self)
}
func printName(){
print(Self.nibName)
}
}
public class Shoes: UIView, NibLoadable {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
printName()
}
override init(frame: CGRect) {
super.init(frame: frame)
printName()
}
}
public class Cake: Shoes {
}
let cake = Cake() // this line prints 'Shoes'
我怎么让它打印Cake
,而不是Shoes
?
完整说明:
我找到了将xib文件加载到故事板中的方法:https://stackoverflow.com/a/47295926/2057955
这非常有效,但是我必须将样板NibLoadable
代码放入每个视图中,如下所示:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupFromNib()
}
当你以这种方式拥有20-30个视图时,这显然很烦人。
所以我试图设置一个基类来处理这个,然后让一切都继承它。
不幸的是,无论我尝试什么,String(describing: Self.self)
总是返回我的基类的名称,而不是派生类的名称。
当我在XCode中打开Storyboard时,我在控制台日志中遇到此异常:
[MT] IBSceneUpdate:[scene update,delegate =] Error Domain = IBMessageChannelErrorDomain Code = 3“无法与帮助工具通信”UserInfo = {NSLocalizedFailureReason =代理引发了“NSInternalInconsistencyException”异常:无法在bundle中加载NIB:'NSBundle (loaded)'名称为'BaseNibLoadable.Type',IBMessageSendChannelException =无法在bundle中加载NIB:'NSBundle(loaded)',名称为'BaseNibLoadable.Type',NSLocalizedDescription =无法与帮助工具通信}
例:
@IBDesignable
class BaseNibLoadable: UIView, NibLoadable {
// MARK: - NibLoadable
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupFromNib()
}
}
@IBDesignable
class SomeHeader: BaseNibLoadable {
@IBOutlet fileprivate weak var headerLabel: UILabel!
@IBInspectable var header: String? {
didSet {
self.headerLabel.text = header
}
}
}
显然我的主要包(两者都是相同的包)包括SomeHeader.xib
文件。
所以我要找的是以某种方式获取在nibName
静态方法中实例化的实际类型的名称。
更新根据要求,这里是我在问题顶部链接的原始答案(相反,只是它的代码,因为它很长,否则请按照链接):
public protocol NibLoadable {
static var nibName: String { get }
}
public extension NibLoadable where Self: UIView {
public static var nibName: String {
return String(describing: Self.self) // defaults to the name of the class implementing this protocol.
}
public static var nib: UINib {
let bundle = Bundle(for: Self.self)
return UINib(nibName: Self.nibName, bundle: bundle)
}
func setupFromNib() {
guard let view = Self.nib.instantiate(withOwner: self, options: nil).first as? UIView else { fatalError("Error loading \(self) from nib") }
addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
view.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
view.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
view.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
view.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
}
}
更新2
我知道子类肯定在堆栈中,因为如果我记录Thread.callStackSymbols
,那么实际的具体视图类名就在那里。
我怎么做它打印蛋糕,而不是鞋子?
更改
print(Self.nibName)
至
print(type(of:self).nibName)