Cocoa - 不知从何而来的 NSError 描述
我有这段代码:
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
if ([response respondsToSelector:@selector(statusCode)]) {
int statusCode = [((NSHTTPURLResponse*)response) statusCode];
if (statusCode >= 400) {
NSError* statusError = [NSError errorWithDomain:@"Server connection error" code:statusCode userInfo:nil];
[self connection:connection didFailWithError:statusError];
}
}
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"%@", [error localizedDescription]);
}
这给出了缺失的页面:
-->操作无法完成。 (服务器连接错误错误 404。)
描述(本地化与否)来自哪里?
我刚刚用代码和自定义的无意义域字符串初始化了 NSError...
I have this piece of code :
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
if ([response respondsToSelector:@selector(statusCode)]) {
int statusCode = [((NSHTTPURLResponse*)response) statusCode];
if (statusCode >= 400) {
NSError* statusError = [NSError errorWithDomain:@"Server connection error" code:statusCode userInfo:nil];
[self connection:connection didFailWithError:statusError];
}
}
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"%@", [error localizedDescription]);
}
That gives for a missing page :
--> The operation couldn’t be completed. (Server connection error error 404.)
From where does that description (localized or not) comes from ?
I've just initialized the NSError with a code and a custom meaningless domain string...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误消息意味着服务器无法找到您的在线资源。
例如:http://www.google.com/notthepageyourelookingfor。
HTTP 404 - 维基百科
如果您询问错误消息的来源,它应该像这样分解这个:
本地化描述
:<块引用>
操作无法完成()
默认情况下,此方法返回用户信息字典中键 NSLocalizedDescriptionKey。如果用户信息字典不包含 NSLocalizedDescriptionKey,默认字符串是根据域和代码构造的。
NSLocalizedDescriptionKey 是错误的本地化字符串表示形式,如果存在,将由 localizedDescription 返回。
适用于 Mac OS X v10.2 及更高版本。在 NSError.h 中声明。
errWithDomain:@"服务器连接错误"
:<块引用>
服务器连接错误
code:statusCode
:<块引用>
错误404
That error message means that your online resource cannot be found by the server.
For example: http://www.google.com/notthepageyourelookingfor.
HTTP 404 - Wikipedia
If you're asking where the error message originates, it ought to break down like this:
localizedDescription
:By default this method returns the object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code.
NSLocalizedDescriptionKey is a localized string representation of the error that, if present, will be returned by localizedDescription.
Available in Mac OS X v10.2 and later. Declared in NSError.h.
errWithDomain:@"Server connection error"
:code:statusCode
:这是一个标准 POSIX 错误。您的域和错误代码只是附加到实际的错误消息中,以确定错误的来源。通常,使用反向 DNS 样式的域,例如
com.developer.package
。That is a standard POSIX error. Your domain and error code are just appended to the actual error message to determine the error's origin. Usually, a reverse-DNS style domain is used, like
com.developer.package
.