Ios 无法使用swizzling方法打印当前ViewController名称
我想打印Ios 无法使用swizzling方法打印当前ViewController名称,ios,swift,Ios,Swift,我想打印ViewControllername,而不是在每个ViewController中打印屏幕名称。是否有其他可能使用ViewController扩展打印ViewController名称?或者有什么建议 例如如果您在登录页面,它应该打印LoginViewController,如果您移动到主屏幕,它应该打印HomeViewController我尝试了下面的代码,但它没有打印确切的类名 extension UIViewController { @objc dynamic func _tracked
ViewController
name,而不是在每个ViewController
中打印屏幕名称。是否有其他可能使用ViewController
扩展打印ViewController
名称?或者有什么建议
例如如果您在登录页面,它应该打印LoginViewController
,如果您移动到主屏幕,它应该打印HomeViewController
我尝试了下面的代码,但它没有打印确切的类名
extension UIViewController {
@objc dynamic func _tracked_viewWillAppear(_ animated: Bool) {
print("ClassName@: "+String(describing: type(of: self)))
_tracked_viewWillAppear(animated)
}
static func swizzle() {
if self != UIViewController.self {
return
}
let _: () = {
let originalSelector =
#selector(UIViewController.viewWillAppear(_:))
let swizzledSelector =
#selector(UIViewController._tracked_viewWillAppear(_:))
let originalMethod =
class_getInstanceMethod(self, originalSelector)
let swizzledMethod =
class_getInstanceMethod(self, swizzledSelector)
method_exchangeImplementations(originalMethod!, swizzledMethod!);
}()
}
并在Appdelegate.swift
文件中
func application( _ application: UIApplication,
didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
UIViewController.swizzle()
return true
}
输出:-
ClassName@: UIInputWindowController
ClassName@: UISystemKeyboardDockController
ClassName@: UICompatibilityInputViewController
ClassName@: UICompatibilityInputViewController
ClassName@: UICompatibilityInputViewController
ClassName@: UICompatibilityInputViewController
ClassName@: UICompatibilityInputViewController
ClassName@: UISystemKeyboardDockController
但它并没有打印ViewController
的名称。
谢谢。您可以使用类型(of:T)
方法
print(type(of: self))
游乐场示例:
import UIKit
class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(type(of: self))
}
}
let vc = SomeViewController()
vc.loadViewIfNeeded()
输出:SomeViewController
您可以自己编写UIViewController子类,重写ViewDidDisplay方法,并使用以下代码:
覆盖函数视图显示(uu动画:Bool){
super.viewdide显示(动画)
打印(“显示的viewcontroller”+字符串(描述:类型(of:self)))
//(做任何事)
}
然后,从这个类继承其余的类。我将执行以下操作
struct StringUtils {
static func className(_ obj: AnyObject) -> String {
return String(describing: type(of: obj))
}
}
然后叫它如下
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(StringUtils.className(self))
}
}
这里我们需要在每个类的viewdiload中编写print(type(of:self)),但我需要一个解决方案,比如动态打印当前可见的VIewController名称。创建一个自定义UIViewController来执行此操作,并让所有其他用户从中继承。您可以创建一个自定义导航控制器,打印它设置的所有类/pushesCan我们可以使用UIVieController的扩展打印相同的类吗?您可以打印
字符串(描述:type(of:self)
从任何地方,而不仅仅是UIViewController。这种子类化和重写是为了确保每个屏幕都打印消息。扩展与向类中添加方法大致相同。您需要从某个地方调用该函数来运行该代码。如果需要,可扩展UIViewController并将该行代码添加到新方法,然后您可以从任何视图控制器调用该方法。let class\u name=“\(HomeViewController.classForCoder())”
let name=String(description:type(of:self))
@AnuragSharma它应该是“(self.classForCoder())”而不是“(HomeViewController.classForCoder())”因为我是在UIViewController扩展中编写的。在这里,我试图使用方法swizzling打印类名。这太过分了。如果您实现了,请共享。@ManuMateos是的,我实现了,您可以在您的项目中尝试。我上面编写的代码是正确的。