Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要为 NSMutableDictionary 创建一个密钥。理想的键是指针和整数的合并。
这仅适用于指针 NSValue *key = [NSValue valueWithPointer:somePointer];
如何将 somePointer + someInt 相加,然后从结果中形成一个键?
我会这样做:
NSObject *p = <object> int n = 0xdeadbeef; NSValue *v = [NSValue valueWithPointer:(void *)((uint32_t )p + n)]; NSLog(@"p = %p n = %x v = %@",p,n,v);
指针是整数类型,其大小取决于您的平台(通常为 32 或 64 位),可以使用合适的强制转换将其添加到其他整数类型。只需确保使用长度大于或等于 void 指针长度的类型进行强制转换。这将确保您不会截断指针。