What is the Swift equivalent of -[NSObject description]?

前端 未结 7 1129
不思量自难忘°
不思量自难忘° 2020-12-23 02:25

In Objective-C, one can add a description method to their class to aid in debugging:

@implementation MyClass
- (NSString *)description
{
    ret         


        
相关标签:
7条回答
  • 2020-12-23 02:59
    class SomeBaseClass: CustomStringConvertible {
    
        //private var string: String = "SomeBaseClass"
    
        var description: String {
            return "\(self.dynamicType)"
        }
    
        // Use this in newer versions of Xcode
        var description: String {
            return "\(type(of: self))"
        }
    
    }
    
    class SomeSubClass: SomeBaseClass {
        // If needed one can override description here
    
    }
    
    
    var mySomeBaseClass = SomeBaseClass()
    // Outputs SomeBaseClass
    var mySomeSubClass = SomeSubClass()
    // Outputs SomeSubClass
    var myOtherBaseClass = SomeSubClass()
    // Outputs SomeSubClass
    
    0 讨论(0)
  • 2020-12-23 03:01

    Example of using CustomStringConvertible and CustomDebugStringConvertible protocols in Swift:

    PageContentViewController.swift

    import UIKit
    
    class PageContentViewController: UIViewController {
    
        var pageIndex : Int = 0
    
        override var description : String { 
            return "**** PageContentViewController\npageIndex equals \(pageIndex) ****\n" 
        }
    
        override var debugDescription : String { 
            return "---- PageContentViewController\npageIndex equals \(pageIndex) ----\n" 
        }
    
                ...
    }
    

    ViewController.swift

    import UIKit
    
    class ViewController: UIViewController
    {
    
        /*
            Called after the controller's view is loaded into memory.
        */
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let myPageContentViewController = self.storyboard!.instantiateViewControllerWithIdentifier("A") as! PageContentViewController
            print(myPageContentViewController)       
            print(myPageContentViewController.description)
            print(myPageContentViewController.debugDescription)
        }
    
              ...
    }
    

    Which print out:

    **** PageContentViewController
    pageIndex equals 0 ****
    
    **** PageContentViewController
    pageIndex equals 0 ****
    
    ---- PageContentViewController
    pageIndex equals 0 ----
    

    Note: if you have a custom class which does not inherit from any class included in UIKit or Foundation libraries, then make it inherit of NSObject class or make it conform to CustomStringConvertible and CustomDebugStringConvertible protocols.

    0 讨论(0)
  • 2020-12-23 03:07

    Just use CustomStringConvertible and var description: String { return "Some string" }

    works in Xcode 7.0 beta

    class MyClass: CustomStringConvertible {
      var string: String?
    
    
      var description: String {
         //return "MyClass \(string)"
         return "\(self.dynamicType)"
      }
    }
    
    var myClass = MyClass()  // this line outputs MyClass nil
    
    // and of course 
    print("\(myClass)")
    
    // Use this newer versions of Xcode
    var description: String {
        //return "MyClass \(string)"
        return "\(type(of: self))"
    }
    
    0 讨论(0)
  • 2020-12-23 03:08

    As described here, you can also use Swift's reflection capabilities to make your classes generate their own description by using this extension:

    extension CustomStringConvertible {
        var description : String {
            var description: String = "\(type(of: self)){ "
            let selfMirror = Mirror(reflecting: self)
            for child in selfMirror.children {
                if let propertyName = child.label {
                    description += "\(propertyName): \(child.value), "
                }
            }
            description = String(description.dropLast(2))
            description += " }"
            return description
        }
    }
    
    0 讨论(0)
  • 2020-12-23 03:16
    struct WorldPeace: CustomStringConvertible {
        let yearStart: Int
        let yearStop: Int
    
        var description: String {
            return "\(yearStart)-\(yearStop)"
        }
    }
    
    let wp = WorldPeace(yearStart: 2020, yearStop: 2040)
    print("world peace: \(wp)")
    
    // outputs:
    // world peace: 2020-2040
    
    0 讨论(0)
  • To implement this on a Swift type you must implement the CustomStringConvertible protocol and then also implement a string property called description.

    For example:

    class MyClass: CustomStringConvertible {
        let foo = 42
    
        var description: String {
            return "<\(type(of: self)): foo = \(foo)>"
        }
    }
    
    print(MyClass()) // prints: <MyClass: foo = 42>
    

    Note: type(of: self) gets the type of the current instances instead of explicitly writing ‘MyClass’.

    0 讨论(0)
提交回复
热议问题