NSDictionary 选择器错误 NSString 带空格

发布于 2024-12-01 17:51:15 字数 1832 浏览 4 评论 0原文

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巡山小妖精 2024-12-08 17:51:15

我看到了一些特点:

error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

通常,你只需这样做:

NSError *error = nil;
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

如果出现错误,则错误变量将填充自动释放的错误对象的地址,否则它应该保持为零。不要自己分配一个,因为您可能会释放错误的一个(由 sendSynchronousRequest: 等方法返回的那个)。这可能导致方法结束时过度释放。

NSMutableDictionary *tempDict= [[[NSMutableDictionary alloc] init] autorelease];
if (request)
{
    Parser *parser = [[Parser alloc] init];
    tempDict = [parser readXMLString:tempString];
  • 在 if 块中,您将覆盖指向刚刚创建的 tempDict 的指针。这是内存泄漏(但不是问题的原因)。 更新:您创建的版本会自动发布。没有泄漏。

  • 您也不释放 if 块(及其本地)中使用的解析器

  • 您永远不会检查错误的值来查看是否确实发生了错误。正如我所说,您应该在调用 sendSynchronousRequest:etc. 之前将 error 设置为 nil,然后检查它是否仍然为 nil,如果没有,则做出相应反应:

    如果(错误)
    {
        // 错误处理
    }
    

[parser readXMLString: tempString]; 的返回类型是什么?它可以是一个数组而不是一个字典吗?例如字典数组?

添加

NSLog(@"%@", tempDict);

在返回 tempDict 之前 in request:。它显示了什么?

getObjects:AndKeys: 可能在 -[NSMutableDictionary initWithDictionary:] 中调用。显然,request: 返回的字典的 real 类型不是字典,而是一个数组。看我上面写的。

I see a few peculiarities:

error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

Usually, you simply do:

NSError *error = nil;
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

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.

NSMutableDictionary *tempDict= [[[NSMutableDictionary alloc] init] autorelease];
if (request)
{
    Parser *parser = [[Parser alloc] init];
    tempDict = [parser readXMLString:tempString];
  • 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 the Parser 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 to nil before the invocation of sendSynchronousRequest:etc. and then check if it is still nil, and if not, react accordingly:

    if (error)
    {
        // error handling
    }
    

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

NSLog(@"%@", tempDict);

in request:, before you return the tempDict. What does it show?

The getObjects:AndKeys: is probably called in -[NSMutableDictionary initWithDictionary:]. Apparently the real type of the dictionary returned by request: is not a dictionary, it is an array. See what I wrote above.

倾听心声的旋律 2024-12-08 17:51:15

罪魁祸首是 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 an NSArray instead of an NSDictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文