Ste*_*her 9 debugging nsthread ios
当您打印NSThread的描述时,您会得到如下内容:
<NSThread: 0x1e511b70>{name = (null), num = 1}
Run Code Online (Sandbox Code Playgroud)
这个"数字"是否以某种方式可用?
这仅用于调试,因此无需清除Apple的批准过程.
Mat*_*ing 13
这个数字实际上是NSThread的私有实现类中的一个ivar.该类是_NSThreadInternal,它的名字是"_private".在那个物体里面,伊娃是seqNum.
如果您愿意依赖未记录的密钥路径,则可以直接将其拉出.这样做(并且在使用valueForKeyPath而不是运行时调用时调用neilsbot):
@implementation NSThread (GetSequenceNumber)
- (NSInteger)sequenceNumber
{
return [[self valueForKeyPath:@"private.seqNum"] integerValue];
}
@end
Run Code Online (Sandbox Code Playgroud)
我通过手动设置运行时调用的ivar然后NSLogging线程来测试它.果然,描述反映了这种变化.这显然没有记录,所以......
这是一个有趣的练习,但事情通常是私人的.除非所有其他路线都已彻底耗尽,否则运送的代码当然应该避免这样的事情.
我继续写下@ xlc的建议,因为:
@implementation NSThread (ThreadGetIndex)
-(NSInteger)getThreadNum
{
NSString * description = [ self description ] ;
NSArray * keyValuePairs = [ description componentsSeparatedByString:@"," ] ;
for( NSString * keyValuePair in keyValuePairs )
{
NSArray * components = [ keyValuePair componentsSeparatedByString:@"=" ] ;
NSString * key = components[0] ;
key = [ key stringByTrimmingCharactersInSet:[ NSCharacterSet whitespaceCharacterSet ] ] ;
if ( [ key isEqualToString:@"num" ] )
{
return [ components[1] integerValue ] ;
}
}
@throw @"couldn't get thread num";
return -1 ;
}
@end
Run Code Online (Sandbox Code Playgroud)
这回答了从线程中获取"num"的问题 - 尽管作为欺骗链接的问题可能有助于唯一识别线程的一般问题.
(我喜欢的答案是"生成一个UUID并放入线程的线程字典中.)
| 归档时间: |
|
| 查看次数: |
4479 次 |
| 最近记录: |