简体   繁体   English

如何将NSValue转换为NSString

[英]How to Convert NSValue to NSString

Some background... I am writing code that interacts with javascript via a ObjC-JS bridge utilizing UIWebView 's stringByEvaluatingJavaScriptFromString: . 一些背景......我正在使用UIWebViewstringByEvaluatingJavaScriptFromString:编写通过ObjC-JS桥与javascript交互的代码stringByEvaluatingJavaScriptFromString: The idea is that the "brains" of the app be in JS which tells Objective-C how to behave. 这个想法是应用程序的“大脑”在JS中,告诉Objective-C如何表现。 There are multiple benefits to this like reduced binary size, flexible updates, etc. However, there is a case where there is some Objective-C only object that the JS needs to have a reference to (JS instructs ObjC when to use/remove the object). 这有很多好处,比如减少二进制大小,灵活更新等等。但是,有一种情况是JS需要一些只需要Objective-C的对象(JS指示ObjC何时使用/删除宾语)。 This is being done by placing the native object in a dictionary with a unique identifier which can be passed as a string to JS (over the bridge). 这是通过将本机对象放在具有唯一标识符的字典中来完成的,该标识符可以作为字符串传递给JS(通过桥接器)。 My problem stems with coming up with a nice identifier for said native Objective-C object. 我的问题源于为所述原生Objective-C对象提供一个很好的标识符。

Thus, I am trying to convert a reference to an object to a string with no luck. 因此,我试图将对象的引用转换为没有运气的字符串。 This is what I have: 这就是我所拥有的:

// anObject is a custom class
NSValue *handle = [NSValue valueWithPointer:(__bridge const void *)anObject]; 
NSData *data = [NSData dataWithValue:handle];
NSString *stringHandle = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

The dataWithValue: function ( taken from this SO post ): dataWithValue:函数( 取自此SO帖子 ):

+ (NSData *)dataWithValue:(NSValue *)value {
    NSUInteger size;
    const char* encoding = [value objCType];
    NSGetSizeAndAlignment(encoding, &size, NULL);

    void* ptr = malloc(size);
    [value getValue:ptr];
    NSData* data = [NSData dataWithBytes:ptr length:size];
    free(ptr);

    return data;
}

Walking through it in the debugger shows me a nil value for stringHandle : 在调试器中遍历它会显示stringHandlenil值:

在此输入图像描述

What am I doing wrong? 我究竟做错了什么?

What you're doing wrong is trying to treat an address as if it's a UTF-8 encoded string. 你做错了是试图将地址视为UTF-8编码的字符串。 An address -- or any other chunk of arbitrary data -- isn't very likely to be valid UTF-8 data. 地址 - 或任何其他任意数据块 - 不太可能是有效的UTF-8数据。 (If by chance it were, it still wouldn't be the string you expect.) (如果是偶然的话,它仍然不是你期望的字符串。)

If you're trying to get a string containing the pointer value, ie, the address of the original object, that's just [NSString stringWithFormat:@"%p", anObject]; 如果您正在尝试获取包含指针值的字符串,即原始对象的地址,那只是[NSString stringWithFormat:@"%p", anObject];

If you really need to do it from the NSValue , then replace anObject with [theValue pointerValue] . 如果你真的需要从NSValue做到这NSValue ,那么用[theValue pointerValue]替换anObject

If you want to pretty-print arbitrary data, see How to convert an NSData into an NSString Hex string? 如果要打印任意数据,请参阅如何将NSData转换为NSString Hex字符串?

You can get a string representation by calling the NSObject method "description". 您可以通过调用NSObject方法“description”来获取字符串表示形式。 You can override the "description" method in a subclass if you need. 如果需要,可以覆盖子类中的“description”方法。

An NSValue of a pointer will be an object holding the 4 bytes of the 32-bit pointer. 指针的NSValue将是一个保存32位指针的4个字节的对象。 It will not hold any of the data pointed to in RAM. 它不会保存RAM中指向的任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM