উদ্দেশ্য-সি-তে, 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
একটি ভাল উপায় আছে কি?