- [এনএসবজেক্ট বর্ণনা] এর সুইফ্ট সমতুল্য কী?


163

উদ্দেশ্য-সি-তে, descriptionডিবাগিংয়ে সহায়তার জন্য কেউ তাদের শ্রেণিতে একটি পদ্ধতি যুক্ত করতে পারে :

@implementation MyClass
- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@: %p, foo = %@>", [self class], foo _foo];
}
@end

তারপরে ডিবাগারে, আপনি এটি করতে পারেন:

po fooClass
<MyClass: 0x12938004, foo = "bar">

সুইফটে সমমান কী? সুইফটের REPL আউটপুট সহায়ক হতে পারে:

  1> class MyClass { let foo = 42 }
  2> 
  3> let x = MyClass()
x: MyClass = {
  foo = 42
}

তবে কনসোলে মুদ্রণের জন্য আমি এই আচরণটি ওভাররাইড করতে চাই:

  4> println("x = \(x)")
x = C11lldb_expr_07MyClass (has 1 child)

এই printlnআউটপুটটি পরিষ্কার করার কোনও উপায় আছে ? আমি Printableপ্রোটোকলটি দেখেছি :

/// This protocol should be adopted by types that wish to customize their
/// textual representation.  This textual representation is used when objects
/// are written to an `OutputStream`.
protocol Printable {
    var description: String { get }
}

আমি অনুভব করেছি যে এটি স্বয়ংক্রিয়ভাবে "দেখা" printlnহবে তবে এটি এরূপ বলে মনে হচ্ছে না:

  1> class MyClass: Printable {
  2.     let foo = 42
  3.     var description: String { get { return "MyClass, foo = \(foo)" } }
  4. }   
  5> 
  6> let x = MyClass()
x: MyClass = {
  foo = 42
}
  7> println("x = \(x)")
x = C11lldb_expr_07MyClass (has 1 child)

এবং পরিবর্তে আমাকে স্পষ্টভাবে বর্ণনা কল করতে হবে:

 8> println("x = \(x.description)")
x = MyClass, foo = 42

একটি ভাল উপায় আছে কি?

উত্তর:


124

একটি সুইফ্ট প্রকারে এটি প্রয়োগ করতে আপনাকে অবশ্যই CustomStringConvertibleপ্রোটোকলটি প্রয়োগ করতে হবে এবং তারপরে একটি স্ট্রিং সম্পত্তিও প্রয়োগ করতে হবে description।

উদাহরণ স্বরূপ:

class MyClass: CustomStringConvertible {
    let foo = 42

    var description: String {
        return "<\(type(of: self)): foo = \(foo)>"
    }
}

print(MyClass()) // prints: <MyClass: foo = 42>

দ্রষ্টব্য: type(of: self)সুস্পষ্টভাবে 'মাই ক্লাস' লেখার পরিবর্তে বর্তমান উদাহরণগুলির ধরণটি পাওয়া যায়।


3
দুর্দান্ত খুঁজে! আমি একটি রাডার ফাইল করতে যাচ্ছি - "সুইফট -i নমুনা.সুইফ্ট" এবং "সুইফ্ট নমুনা.সুইફ્ટ এবং& নমুনা" এর প্রিন্টলন আউটপুট পৃথক।
— জেসন

যে তথ্যের জন্য ধন্যবাদ। আমি খেলার মাঠে মুদ্রণযোগ্য চেষ্টা করছিলাম এবং এটি এখনই কাজ করে না it এটি একটি অ্যাপে কাজ করে শুনে ভাল।
— টড কানিংহাম

মুদ্রণযোগ্য খেলার মাঠে কাজ করে, তবে যদি ক্লাসটি এনএসবজেক্ট থেকে নেমে আসে
— dar512

5
সুইফট ২.০ এ এটি কাস্টমস্ট্রিংকনভার্টেবল এবং কাস্টমডিবুগস্ট্রিংকনভার্টেবল
— মাইক ভোসেলার

এছাড়াও, এক্সকোড 7.2 সহ খেলার মাঠে কাস্টমস্ট্রিংকনভার্টেবল এবং কাস্টমডিবুগস্ট্রিংকনভার্টেবল ব্যবহার করে কোনও সমস্যা নেই
— নিকোলাস ক্রেডলি

54

সুইফটে ব্যবহার CustomStringConvertibleএবং CustomDebugStringConvertibleপ্রোটোকলগুলির উদাহরণ :

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)
    }

          ...
}

কোন প্রিন্ট আউট:

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

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

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

দ্রষ্টব্য: আপনার যদি কাস্টম ক্লাস থাকে যা ইউআইকিট বা ফাউন্ডেশন লাইব্রেরিতে অন্তর্ভুক্ত কোনও শ্রেণীর উত্তরাধিকার সূত্রে না আসে , তবে এটিকে NSObjectশ্রেণীর উত্তরাধিকারী করুন বা এটিকে উপযুক্ত CustomStringConvertibleএবং CustomDebugStringConvertibleপ্রোটোকল করুন।


ফাংশনটি অবশ্যই সর্বজনীন হিসাবে ঘোষণা করতে হবে
— কার্স্টেন

35

শুধু ব্যবহার করুন CustomStringConvertibleএবংvar description: String { return "Some string" }

এক্সকোড 7.0 বিটাতে কাজ করে

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))"
}

20

সম্পর্কিত উত্তরগুলি হ'ল CustomStringConvertibleউপায়। ব্যক্তিগতভাবে, ক্লাসের (বা স্ট্রাক্ট) সংজ্ঞাটি যতটা সম্ভব পরিষ্কার রাখতে, আমি বিবরণ কোডটি পৃথক এক্সটেনশনে আলাদা করব:

class foo {
    // Just the basic foo class stuff.
    var bar = "Humbug!"
}

extension foo: CustomStringConvertible {
    var description: String {
        return bar
    }
}

let xmas = foo()
print(xmas)  // Prints "Humbug!"

8
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

6

এখানে বর্ণিত হিসাবে , আপনি এই এক্সটেনশনটি ব্যবহার করে আপনার শ্রেণিগুলি তাদের নিজস্ব বিবরণ উত্পন্ন করার জন্য সুইফটের প্রতিবিম্ব ক্ষমতাগুলিও ব্যবহার করতে পারেন:

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
    }
}

4
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
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.