NSDictionary 选择器错误 NSString 带空格
-(void)messageSend:(NSString *)message;
{
NSLog(@"messageSend");
urlString = [[NSString alloc] initWithFormat:@"http://someaddress/message/send?from=%@&msg=%@&latitude=0&longitude=0",appDelegate.userName,message];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:[self request:urlString]];
NSLog(@"Dictionary response");
if ([dictionary count] > 0)
{
if ([[dictionary objectForKey:@"send"] isEqualToString:@"OK"] )
{
NSLog(@"envio de mensagem de %@ Ok: %@",appDelegate.userName,message);
}
}
[urlString release];
[dictionary release];
}
给出错误 -[__NSArrayM getObjects:andKeys:]: 无法识别的选择器发送到实例
。经过使用 NSLogs 进行一些测试后,该行
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:[self request:urlString]];
是罪魁祸首,女巫正在调用此方法:
-(NSDictionary *)request:(NSString *)requestString
{
url =[[NSURL alloc] initWithString:requestString];
request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[responseData retain];
NSString *tempString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *tempDict= [[[NSMutableDictionary alloc] init] autorelease];
if (request)
{
Parser *parser = [[Parser alloc] init];
tempDict = [parser readXMLString:tempString];
for (id key in tempDict)
{
NSLog(@"%@ is %@",key,[tempDict objectForKey:key]);
}
}
[url release];
[error release];
[responseData release];
[tempString release];
return tempDict;
}
当消息的字符串有空格时,就会发生这种情况。 但以前并没有发生过这种情况。
-(void)messageSend:(NSString *)message;
{
NSLog(@"messageSend");
urlString = [[NSString alloc] initWithFormat:@"http://someaddress/message/send?from=%@&msg=%@&latitude=0&longitude=0",appDelegate.userName,message];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:[self request:urlString]];
NSLog(@"Dictionary response");
if ([dictionary count] > 0)
{
if ([[dictionary objectForKey:@"send"] isEqualToString:@"OK"] )
{
NSLog(@"envio de mensagem de %@ Ok: %@",appDelegate.userName,message);
}
}
[urlString release];
[dictionary release];
}
Gives an error of -[__NSArrayM getObjects:andKeys:]: unrecognized selector sent to instance
. After some testing with NSLogs
, the line
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:[self request:urlString]];
is the culprit, witch is calling this method:
-(NSDictionary *)request:(NSString *)requestString
{
url =[[NSURL alloc] initWithString:requestString];
request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[responseData retain];
NSString *tempString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *tempDict= [[[NSMutableDictionary alloc] init] autorelease];
if (request)
{
Parser *parser = [[Parser alloc] init];
tempDict = [parser readXMLString:tempString];
for (id key in tempDict)
{
NSLog(@"%@ is %@",key,[tempDict objectForKey:key]);
}
}
[url release];
[error release];
[responseData release];
[tempString release];
return tempDict;
}
And it happens when the string of the message has spaces.
But it was not happening before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到了一些特点:
通常,你只需这样做:
如果出现错误,则错误变量将填充自动释放的错误对象的地址,否则它应该保持为零。不要自己分配一个,因为您可能会释放错误的一个(由
sendSynchronousRequest:
等方法返回的那个)。这可能导致方法结束时过度释放。在 if 块中,您将覆盖指向刚刚创建的
tempDict
的指针。这是内存泄漏(但不是问题的原因)。 更新:您创建的版本会自动发布。没有泄漏。您也不
释放
if 块(及其本地)中使用的解析器
。您永远不会检查错误的值来查看是否确实发生了错误。正如我所说,您应该在调用
sendSynchronousRequest:etc.
之前将error
设置为nil
,然后检查它是否仍然为nil
,如果没有,则做出相应反应:[parser readXMLString: tempString];
的返回类型是什么?它可以是一个数组而不是一个字典吗?例如字典数组?添加
在返回
tempDict
之前 inrequest:
。它显示了什么?getObjects:AndKeys:
可能在-[NSMutableDictionary initWithDictionary:]
中调用。显然,request:
返回的字典的 real 类型不是字典,而是一个数组。看我上面写的。I see a few peculiarities:
Usually, you simply do:
The error variable will be filled with the address of an autoreleased error object if there is an error, otherwise it should remain nil. Do not allocate one yourself, since you could be releasing the wrong one (the one returned by the
sendSynchronousRequest:
etc. method). This could cause an over-release at the end of your method.In the if-block, you are overwriting the pointer to the
tempDict
you just created. That is a memory leak (but not the cause of your problem). Update: the one you created is autoreleased. No leak.You also don't
release
theParser
used in the if-block (and local to it).You never check the value of error to see if actually an error occurred. As I said, you should set
error
tonil
before the invocation ofsendSynchronousRequest:etc.
and then check if it is stillnil
, and if not, react accordingly:What is the return type of
[parser readXMLString: tempString];
? Could it be an array and not a dictionary? E.g. an array of dictionaries?Add an
in
request:
, before you return thetempDict
. What does it show?The
getObjects:AndKeys:
is probably called in-[NSMutableDictionary initWithDictionary:]
. Apparently the real type of the dictionary returned byrequest:
is not a dictionary, it is an array. See what I wrote above.罪魁祸首是
tempDict = [parser readXMLString:tempString]
行。事实上,这意味着您之前创建的[[[NSMutableDictionary alloc] init] autorelease]
是毫无意义的,因为它只会被[parser readXMLString:tempString] 的返回值覆盖
。无论如何,-readXMLString:
方法似乎返回的是NSArray
而不是NSDictionary
。The culprit is the line
tempDict = [parser readXMLString:tempString]
. In fact, this means your previous creation of a[[[NSMutableDictionary alloc] init] autorelease]
is pointless, as it will just be overwritten by the return value of[parser readXMLString:tempString]
. In any case, it appears the-readXMLString:
method is returning anNSArray
instead of anNSDictionary
.