gpt4 book ai didi

objective-c - NSDictionary 选择器错误 NSString 带空格

转载 作者:行者123 更新时间:2023-11-28 20:44:07 25 4
gpt4 key购买 nike

-(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:]: unrecognized selector sent to instance。在使用 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;
}

当消息的字符串有空格时就会发生这种情况。但这以前没有发生过。

最佳答案

我看到了一些特点:

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 block 中,您将覆盖指向刚刚创建的 tempDict 的指针。那是内存泄漏(但不是问题的原因)。 更新:您创建的是自动发布的。没有泄漏。

  • 您也不要发布 if block (及其本地)中使用的解析器

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

    if (error)
    {
    // error handling
    }

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

添加一个

NSLog(@"%@", tempDict);

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

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

关于objective-c - NSDictionary 选择器错误 NSString 带空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7198371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com