objective-c NSString 的 initWithData:encoding: 返回类型问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/338598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSString's initWithData:encoding: return type issue
提问by Coocoo4Cocoa
I have a couple of lines of trivial code such as the following:
我有几行简单的代码,如下所示:
NSData *dataReply;
NSString *stringReply;
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
The problem here is, initWithData:encoding: does not return an instance of NSString as the documentation claims it does. I've tried doing an explicit cast using (NSString *) as well without much luck. This is giving me compiler warnings when I try to pass the stringReply to a method I've written with type mismatches. Given I treat all warnings as errors, I'd really like to understand what stringReply is being returned as and how I can enforce it to be of type NSString.
这里的问题是, initWithData:encoding: 没有像文档声称的那样返回 NSString 的实例。我也尝试过使用 (NSString *) 进行显式转换,但运气不佳。当我尝试将 stringReply 传递给我用类型不匹配编写的方法时,这给了我编译器警告。鉴于我将所有警告都视为错误,我真的很想了解返回的 stringReply 是什么,以及我如何将其强制为 NSString 类型。
回答by Matt Gallagher
A "type mismatch" from the compiler when you try to use stringReply in another location has nothing to do with the object being returned from initWithData:encoding: and everything to do with where stringReply is subsequently being used.
当您尝试在另一个位置使用 stringReply 时,来自编译器的“类型不匹配”与从 initWithData:encoding: 返回的对象无关,并且与随后使用 stringReply 的位置有关。
For example, if you are getting a type mismatch when you do this:
例如,如果您在执行此操作时遇到类型不匹配:
SomeClass *someObject;
NSString *stringReply;
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
[someObject methodWithString:stringReply];
The problem is notthat initWithData:encoding: is returning the wrong type but one of:
问题不在于 initWithData:encoding: 返回错误的类型,而是以下类型之一:
- methodWithString: doesn't actually take an NSString*
- methodWithString: is not properly declared/included before being used
- methodWithString: 实际上并不采用 NSString*
- methodWithString:在使用之前没有正确声明/包含
The second option can often happen if SomeClass is forward declared in a header file (as @class SomeClass) but never subsequently included in the implementation file.
如果 SomeClass 在头文件中前向声明(如@class SomeClass)但随后从未包含在实现文件中,则通常会发生第二个选项。
回答by Max Stewart
Not really sure what's going on, I ran the code below and got an NSString returned. Switching out the data with nil also works fine.
不太确定发生了什么,我运行了下面的代码并返回了一个 NSString。用 nil 切换数据也可以正常工作。
const char* os = "12345";
NSString* str = [[NSString alloc] initWithData:[NSData dataWithBytes:os length:5 ] encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
If you debug it can you tell what type of object is being created?
如果你调试它,你能知道正在创建什么类型的对象吗?
Might help if you post the compiler warning.
如果您发布编译器警告可能会有所帮助。

