Xcode 7.3 中的调试视图层次结构失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36313850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:41:01  来源:igfitidea点击:

Debug view hierarchy in Xcode 7.3 fails

xcodeswift

提问by orkenstein

This function fails with runtime error:

此函数因运行时错误而失败:

-[UIWindow viewForFirstBaselineLayout]: unrecognized selector sent to instance 0x7fb9dae257d0

Anybody encountered the same?

有人遇到同样的吗?

UPD:
Fails on simulator iOS 8.1/8.4. 9.3 works fine.

UPD:
在模拟器 iOS 8.1/8.4 上失败。9.3 工作正常。

UPD2:UIWindowis created like:

UPD2:UIWindow创建如下:

window = UIWindow(frame: UIScreen.mainScreen().bounds)    
window?.rootViewController = RootViewController.rootVC
window?.makeKeyAndVisible()

采纳答案by Brian Nickel

I got the view debugger working again by placing the following fix in my project:

通过在我的项目中放置以下修复程序,我让视图调试器再次工作:

#ifdef DEBUG

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@implementation UIView (FixViewDebugging)

+ (void)load
{
    Method original = class_getInstanceMethod(self, @selector(viewForBaselineLayout));
    class_addMethod(self, @selector(viewForFirstBaselineLayout), method_getImplementation(original), method_getTypeEncoding(original));
    class_addMethod(self, @selector(viewForLastBaselineLayout), method_getImplementation(original), method_getTypeEncoding(original));
}

@end

#endif

When your project loads, the loadmethod will execute, causing viewForFirstBaselineLayoutand viewForLastBaselineLayoutto use the viewForBaselineLayoutimplementation if they are not currently implemented, so view debugging gets iOS8 flavor the behavior it was looking for.

当您的项目加载时,该load方法将执行,如果它们当前未实现,则导致viewForFirstBaselineLayoutviewForLastBaselineLayout使用viewForBaselineLayout实现,因此视图调试获得 iOS8 风格它正在寻找的行为。

To add this to your own project, create a new empty Objective-C file in your project and paste the contents in. You can name it whatever you want. I call mine "UIView+FixViewDebugging". If you are in a pure Swift project you do notneed to create a bridging header. The file will be compiled into your project and you don't need to reference it.

要将其添加到您自己的项目中,请在您的项目中创建一个新的空 Objective-C 文件并将内容粘贴到其中。您可以随意命名它。我称我的为“UIView+FixViewDebugging”。如果您在纯 Swift 项目中,不需要创建桥接头。该文件将被编译到您的项目中,您无需引用它。

Note this will only work for debug builds because of the #ifdef DEBUG. You can remove it but then you may accidentally compile this into your release builds (though it should have no ill side effects). If this method isn't working with these lines, check that your target has DEBUG=1in Build Settings > Apple LLVM - Preprocessing > Preprocessor Macros > Debug.

请注意,这仅适用于调试版本,因为#ifdef DEBUG. 您可以删除它,但是您可能会不小心将其编译到您的发布版本中(尽管它应该没有不良副作用)。如果此方法不适用于这些行,请检查您的目标是否DEBUG=1在 Build Settings > Apple LLVM - Preprocessing > Preprocessor Macros > Debug 中。

回答by Andrey Gordeev

Looks like Xcode 7.3 uses viewForFirstBaselineLayoutproperty to draw the UI. But this property is marked as available since iOS 9.0.

看起来 Xcode 7.3 使用viewForFirstBaselineLayout属性来绘制 UI。但是这个属性从 iOS 9.0 开始被标记为可用。

Screenshot of UIView.h

UIView.h 的截图

[UIView viewForFirstBaselineLayout]method should be used for the version prior to iOS 9.0. It seems the guys from Apple didn't consider this case.

[UIView viewForFirstBaselineLayout]方法应用于iOS 9.0 之前的版本。看来苹果公司的人没有考虑这个案子。

回答by Qun Li

Yes. when click the debug view hierarchy button ,the page has nothing, and print "[UIWindow viewForFirstBaselineLayout]: unrecognized selector sent to instance 0x7fb9dae257d0" .

是的。单击调试视图层次结构按钮时,页面没有任何内容,并打印“[UIWindow viewForFirstBaselineLayout]: unrecognized selector sent to instance 0x7fb9dae257d0”。

To solved it, just be sure you are using the iOS systom not below iOS 9.0 and you will still use that function freely.

要解决它,请确保您使用的iOS系统不低于iOS 9.0,并且您仍然可以自由使用该功能。