Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSDictionary中的nil对象_Ios_Objective C_Nsdictionary - Fatal编程技术网

Ios NSDictionary中的nil对象

Ios NSDictionary中的nil对象,ios,objective-c,nsdictionary,Ios,Objective C,Nsdictionary,我有一个NSDictionary,从中我在代码中的两个不同方法中读取了一些值,但是只有在其中一个方法中正确地检索到键的值,而在另一个方法中我总是得到零 这是有效的方法: -(void)drawDriver:(NSString*)driverId withPosition:(CLLocationCoordinate2D)position withRotation:(float)bearing isAvailable:(BOOL)isDriverAvailable{ GMSMarker *

我有一个NSDictionary,从中我在代码中的两个不同方法中读取了一些值,但是只有在其中一个方法中正确地检索到键的值,而在另一个方法中我总是得到零

这是有效的方法:

-(void)drawDriver:(NSString*)driverId withPosition:(CLLocationCoordinate2D)position withRotation:(float)bearing isAvailable:(BOOL)isDriverAvailable{

    GMSMarker *driverMarker = [driversList objectForKey:driverId];
    if (driverMarker != nil) {
        // do some work
    }
}
它是从另一个方法调用的:

- (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet
{
    NSMutableDictionary *datos = (NSMutableDictionary*)packet.data;
    NSDictionary *driverId = datos[@"from"];
    NSString *id = [driverId objectForKey:@"id"];

    //gets other values

    NSLog(@"Socket message: driverId: %@, position %f,%f, isAvailable:%d", id, latitude, longitude, isDriverAvailable);

    //Call the first method
    [self drawDriver:id withPosition:position withRotation:bearing isAvailable:isDriverAvailable];

}
这是一个不起作用的方法(对于driverMarker,返回nil):

从这里我们称之为:

- (void)updateTripWithData:(NSDictionary *)data {

    //This NSDictionary comes from a notification
    NSString *newStatus = [[data objectForKey:@"aps"] objectForKey:@"alert"];

    if ([newStatus isEqualToString:NO_TRIP]) {

    } else if ([newStatus isEqualToString:WAITING_CONFIRMATION]) {

    } else if ([newStatus isEqualToString:DRIVER_ON_THE_WAY]) {

        //Get the driverId and call the other method
        NSString *driverId = [data objectForKey:@"driver_id"];
        [self updateMapForTripWithDriver:driverId];
    }
}
正如您所看到的,这两种方法都有完全相同的代码来检索对象,但我仍然得到不同的结果。 我能找到的唯一区别是,我在方法上设置了一些断点,这就是我发现的:

在第一个方法中,虽然我将driverId作为NSString传递,但不知何故它被读取为NSFCNumber,这似乎是可行的,因为我获得的值没有任何问题

但是在第二个方法中,键被读取为实际的NSString,出于某种原因,这使得该方法对于NSDictionary上确实存在的对象返回nil


如何让第二个方法工作并返回正确的值?

您的调试似乎表明了问题:一种情况下传递的是
NSNumber
,另一种情况下传递的是字符串,而字典中的键实际上是字符串。生成参数并调用这些方法的代码是罪魁祸首,因此如果您希望这里的人能够提供帮助,您需要证明这一点。@JoshCaswell是正确的,但我认为关键实际上是一个
NSNumber
,而不是
NSString
哦,是的,您是对的,@BradAllred:我倒过来了。@JoshCaswell我刚刚用两种方法的调用更新了我的问题。如何使字符串成为数字并将其作为密钥传递给字典?
- (void)updateTripWithData:(NSDictionary *)data {

    //This NSDictionary comes from a notification
    NSString *newStatus = [[data objectForKey:@"aps"] objectForKey:@"alert"];

    if ([newStatus isEqualToString:NO_TRIP]) {

    } else if ([newStatus isEqualToString:WAITING_CONFIRMATION]) {

    } else if ([newStatus isEqualToString:DRIVER_ON_THE_WAY]) {

        //Get the driverId and call the other method
        NSString *driverId = [data objectForKey:@"driver_id"];
        [self updateMapForTripWithDriver:driverId];
    }
}