您如何模仿描述输出? [英] How can you mimic the description output?
问题描述
想象一堂水果:
class Fruit: NSObject {
override var description:String {
return super.description
}
}
var apple = Fruit()
var banana = Fruit()
print(apple) // Output: <MyProject.Fruit: 0x7fa719627e00>
print(banana) // Output: <MyProject.Fruit: 0x7fa71962dab0>
问题:您如何模仿此输出?
我目前有以下内容:
class Fruit: NSObject {
override var description:String {
print(super.description)
return "<\(NSStringFromClass(self.dynamicType)): 0x\(String(self.hash, radix:16))>"
}
}
现在输出以下内容:
<MyProject.Fruit: 0x7fb958c289a0>
<MyProject.Fruit: 0x7fb958c289a0>
<MyProject.Fruit: 0x7fb958c22df0>
<MyProject.Fruit: 0x7fb958c22df0>
如您所见,输出是相同的,这正是我想要的.现在我想知道这是否是模仿其输出的正确方法,还是我忽略了下面评论中提到的内容.
As you can see the output is the same which is what I wanted. Now I am wondering if this is the proper way to mimic it's output or that I am overlooking something as mentioned in the comments below.
信用:Matt,Martin R和Vacawama
推荐答案
Any subclass of NSObject
inherits the
description
method of NSObject
(which is defined in the NSObjectProtocol
):
class Foo1 : NSObject { }
print(Foo1())
// <MyProject.Foo1: 0x100612fd0>
此默认实现"打印类名和内存 对象的地址,例如 星期五问答,2013-01-25:生成NSObject ,其中显示了Objective-C实现的方式 可能像这样:
This "default implementation" prints the class name and the memory address of the object, see for example Friday Q&A 2013-01-25: Let's Build NSObject, where it is shown how the Objective-C implementation could look like:
- (NSString *)description
{
return [NSString stringWithFormat: @"<%@: %p>", [self class], self];
}
%p
格式将指针的值打印为十六进制数字,
在0x
之前.
The %p
format prints the value of a pointer as a hexadecimal number,
preceded by 0x
.
要模仿在Swift中,我们可以使用
To mimic that in Swift, we can use
-
String(reflecting: self.dynamicType)
,它以字符串形式返回完全限定的类名,并且 -
unsafeAddressOf(self)
,它返回指向存储的指针 对象.
String(reflecting: self.dynamicType)
which returns the fully-qualified class name as a string, andunsafeAddressOf(self)
which returns a pointer to the storage of the object.
示例(使用方括号[]
演示
使用覆盖的方法):
Example (using square brackets []
to demonstrate that the
overridden method is used):
class Foo2 : NSObject {
override var description : String {
let className = String(reflecting: self.dynamicType)
let address = unsafeAddressOf(self)
return String(format: "[%@: %p]", className, address)
}
}
print(Foo2())
// [MyProject.Foo2: 0x100613310]
class Foo3 : Foo2 { }
print(Foo3())
// [MyProject.Foo3: 0x102000540]
这也适用于纯Swift类",因为没有Foundation 使用的方法:
This works for "pure Swift classes" as well, because no Foundation methods are used:
class Bar : CustomStringConvertible {
var description : String {
let className = String(reflecting: self.dynamicType)
let address = unsafeAddressOf(self)
return String(format: "[%@: %p]", className, address)
}
}
print(Bar())
// [MyProject.Bar: 0x102001200]
请注意(如以上注释中所述),哈希值
对象的地址不一定与存储器地址相同.
一个简单的示例是NSArray()
,其哈希值仅是数字
元素:
Note that (as already mentioned in above comments), the hash value
of an object is not necessarily identical to the memory address.
A simple example is NSArray()
whose hash value is just the number
of elements:
let array = NSArray(objects: 1, 2, 3)
print(unsafeAddressOf(array)) // 0x00000001020011a0
print(array.hashValue) // 3
针对Swift 3的更新
class Bar : CustomStringConvertible {
var description : String {
let className = String(reflecting: type(of: self))
let address = Unmanaged.passUnretained(self).toOpaque()
return "[\(className): \(address)]"
}
}
这篇关于您如何模仿描述输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!