将浮点值从数组传递到坐标时出现异常

时间:2012-01-22 18:05:41

标签: iphone ios

我在这些代码行中得到例外,不知道为什么要这么做..帮助我... 原因是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:   
'-[__NSArrayI floatValue]: unrecognized selector sent to instance 0x7c57840'

coord.latitude = [[[self.holdingArray objectAtIndex:k] 
valueForKey:@"latitude"]floatValue];
 coord.longitude = [[[self.holdingArray objectAtIndex:k] valueForKey:@"longitude"]  
 floatValue];

修改1

 -(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[appDelegate.dealerDataGlobalArray removeAllObjects];
NSString *requestString;

requestString = [NSString stringWithFormat:@"http://www.hvinfotech.net/projects/carwash
/search.php?search=%@",textField.text];
NSString *strCOntents1 = [NSString stringWithContentsOfURL:[NSURL 
URLWithString:requestString] encoding:NSUTF8StringEncoding error:nil];

NSLog(@"Data: %@",strCOntents1);

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL  
URLWithString:requestString]];
DealerDetailParser *parser = [[DealerDetailParser alloc] 
initDealerDetailParserXMLParser];
[xmlParser setDelegate:parser];
[xmlParser parse];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSLog(@"dealerDataGlobalArray...... %@",[appDelegate.dealerDataGlobalArray 
description]);
[holdingArray addObject:appDelegate.dealerDataGlobalArray];
NSLog(@"holdingArray...... %@",holdingArray);
flagForHoldingArray = YES;    
[self loadOurAnnotations]; 
[textField resignFirstResponder];
return YES;
}

我已编辑,以便更清楚。我正在调用webservice来获取与文本字段中输入的内容相对应的数据。之后,我调用了上一行o代码发布的方法......

EDIT2

它给出了像

    2012-01-23 00:27:35.475 CarWashApp[2584:11603] (
    {
    businessdesc = "having a garage and teamof 10members, originaly belongs to mumbai 
    maharashtra.";
    "businessdesc_de" = "having a garage and teamof 10members, originaly belongs to 
    mumbai maharashtra.";
    city = vadodra;
    "city_de" = "dadar_de";
    companyname = "Maruti Suzki Ltd";
    "companyname_de" = "Maruti Suzki Ltd _de";
    contactname = "bijju sharma";
    "contactname_de" = "bijju sharma_de";
    coupondetails = "10% Discount";
    "coupondetails_de" = "10% Discount _de";
    email = "bsharma@gmail.com";
    friday = "Friday: Closed";
    happyhour = "10:00 AM to 5:00 PM";
    id = 5;
    latitude = "22.30731";
    logo = "http://www.hvinfotech.net/projects/carwash/upload/images/Sunset.jpg";
    longitude = "73.181098";
    monday = "Monday: Closed";
    payment = Visa;
    payments = "";
    phone1 = 07932453453;
    phone2 = 07934344444;
    postcode = JU113;
    saturday = "Saturday:9:30 AM - 4:00 PM";
    services = "";
    state = Groningen;
    "state_de" = Groningen;
    streetaddress1 = "500 golf Road,";
    "streetaddress1_de" = "sectore 5 d_de";
    streetaddress2 = "airport Road";
    "streetaddress2_de" = "";
    sunday = "Sunday:9:30 AM - 4:00 PM";
    thursday = "Thursday: Closed";
    tuesday = "Tuesday:9:30 AM - 4:00 PM";
    website = "http://hvinfotechPvtLtd.com";
    wednesday = "Wednesday: Closed";
}
 )
  2012-01-23 00:27:43.434 CarWashApp[2584:11603] (
"22.30731"
)

我只需要上面的值,即“22.30731”作为坐标,但是如果我也写的那样它的显示错误     coord.latitude = [[self.holdingArray objectAtIndex:k] valueForKey:@“latitude”];

2 个答案:

答案 0 :(得分:1)

self.holdingArray - dictionaries中有哪些对象? 好的,字典中的关键“经度”是什么类型的对象?它们似乎是阵列。 通常很好的做法是拆分这些嵌套语句,以便您可以轻松地进行调试。

答案 1 :(得分:0)

正如@Rayfleck所说,数组中的对象可能不是你期望的那种对象。使用调试器或NSLog()找出它实际上是什么对象。

另一种可能性是,您可能在某处遇到内存管理错误。你有ARC启用吗?

最后,它可能是一个线程安全问题。在那种情况下,祝你好运!除了读取每行代码并试图找出错误之外,没有任何简单的方法来追踪线程问题。

修改

你有这行代码:

[holdingArray addObject:appDelegate.dealerDataGlobalArray];

看起来您正在向holdingArray数组添加数组对象。向它们发送valueForKey:时,Array对象将返回另一个数组(在NSArray的文档中查找valueForKey ...)。

无论如何,数组对象不响应floatValue。只有NSStringNSNumber个对象会回复floatValue

你应该NSLog()这些对象,因为我认为它们不是你所期望的那样:

NSLog(@"%@", [self.holdingArray objectAtIndex:k]);
NSLog(@"%@", [[self.holdingArray objectAtIndex:k] valueForKey:@"latitude"]);