与NSLog一起使用的"toString()"的Objective-C等效项是什么? [英] What is the Objective-C equivalent for "toString()", for use with NSLog?
本文介绍了与NSLog一起使用的"toString()"的Objective-C等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有可以在自定义类中覆盖的方法,以便何时
Is there a method that I can override in my custom classes so that when
NSLog(@"%@", myObject)
被称为,它将打印对象的字段(或我认为重要的任何字段)?我想我在寻找与Java toString()
等效的Objective-C.
is called, it will print the fields (or whatever I deem important) of my object? I guess I'm looking for the Objective-C equivalent of Java's toString()
.
推荐答案
它是description
实例方法,声明为:
It is the description
instance method, declared as:
- (NSString *)description
这是一个示例实现(感谢grahamparks):
Here's an example implementation (thanks to grahamparks):
- (NSString *)description {
return [NSString stringWithFormat: @"Photo: Name=%@ Author=%@", name, author];
}
这篇关于与NSLog一起使用的"toString()"的Objective-C等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文