如何为NSNumber打印标记指针?

时间:2014-09-25 10:08:30

标签: ios objective-c xcode

我阅读了it's文章,我尝试打印标记指针NSNumber

我尝试使用xcode关注代码:

- (NSString *)binForObjectPointer:(NSObject *)obj
{
    return [self binForScalarNumber:(uintptr_t)(__bridge void *)(obj)];
}
- (NSString *)binForScalarNumber:(uintptr_t)number
{
    NSString *ms = @"";
    while (number)
    {
        if (number & 1)
            ms = [ms stringByAppendingString:@"1"];
        else
            ms = [ms stringByAppendingString:@"0"];
        number >>= 1;
    }
    return ms;
}
for (int i = 0; i < 10; ++i)
{
    NSNumber *n = [NSNumber numberWithInt:i];
    NSString *s = [self binForObjectPointer:n];
    NSLog(@"%i %@ %@ %@",
          i,
          [s substringWithRange:NSMakeRange(0, s.length - 4)],
          [s substringWithRange:NSMakeRange(s.length - 4, 3)],
          [s substringWithRange:NSMakeRange(s.length - 1, 1)]
          );
}

并在控制台中获得结果

2014-09-25 16:57:46.926 AppName[3983:60b] 0 0000001111100101001010100 110 1
2014-09-25 16:57:46.930 AppName[3983:60b] 1 0000010000000101001010100 110 1
2014-09-25 16:57:46.931 AppName[3983:60b] 2 0000000001110101001010100 110 1
2014-09-25 16:57:46.932 AppName[3983:60b] 3 0000011011111101110001100 110 1
2014-09-25 16:57:46.933 AppName[3983:60b] 4 0000011011000110000110100 110 1
2014-09-25 16:57:46.934 AppName[3983:60b] 5 0000101101000110101001100 110 1
2014-09-25 16:57:46.935 AppName[3983:60b] 6 0000100110100110111010100 110 1
2014-09-25 16:57:46.936 AppName[3983:60b] 7 0000000111011010101001100 110 1
2014-09-25 16:57:46.938 AppName[3983:60b] 8 0000001100000110101010100 110 1
2014-09-25 16:57:46.939 AppName[3983:60b] 9 0000001011000110000110100 110 1

在文章示例中看起来并不像:

  <00> 0000 0000 0000 0000 0000 0000 0011 1011

                           ^  ^ ^ tag bit
                           |  |
                           |  tagged pointer class (5)
                           |
                           binary 3

我的错误?

0 个答案:

没有答案