问题描述
这个num是否可用吗?
这仅用于调试,因此不需要清除Apple的审批流程。
这个数字实际上是NSThread的私有实现类中的一个ivar。该类是 _NSThreadInternal
,其名称为_private。在该对象内,ivar是 seqNum
。
如果您愿意依靠它,您可以直接拉未记录的关键路径。这样做(并且使用valueForKeyPath而不是运行时调用好的调用neilsbot):
@implementation NSThread(GetSequenceNumber)
- (NSInteger)sequenceNumber
{
return [[self valueForKeyPath:@private.seqNum] integerValue];
}
@end
我通过手动设置该ivar与运行时调用,然后NSLogging的线程。果然,这个描述反映了变化。这显然没有记录,所以...
...自行承担风险。
一个有趣的运动,但事情通常是私人的一个原因。运送的代码一定要避免这样的事情,除非所有其他路线已经彻底耗尽。
When you print a NSThread's description, you get something like this:
<NSThread: 0x1e511b70>{name = (null), num = 1}
Is this "num" available somehow?
This is for debugging only, so it does not need to clear Apple's approval process.
That number is actually an ivar in NSThread's private implementation class. The class is _NSThreadInternal
, and its name is "_private". Inside that object, the ivar is seqNum
.
You can pull it directly if you're willing to rely on undocumented key paths. This'll do it (and good call neilsbot on using valueForKeyPath instead of runtime calls):
@implementation NSThread (GetSequenceNumber)
- (NSInteger)sequenceNumber
{
return [[self valueForKeyPath:@"private.seqNum"] integerValue];
}
@end
I tested it by manually setting that ivar with runtime calls and then NSLogging the thread. Sure enough, the description reflected the change. This is obviously not documented, so...
...use at your own risk.
It's a fun exercise, but things are typically private for a reason. Shipped code should certainly avoid things like this unless all other routes have been thoroughly exhausted.
这篇关于iOS上的NSThread号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!