allenhsu/NSDictionary-NilSafe

在iOS 11下,调用-openMapsWithItems:launchOptions会EXC_BAD_ACCESS

Closed this issue · 9 comments

iOS 11,发现引入该库的同时调用-openMapsWithItems:launchOptions会EXC_BAD_ACCESS。

简单测试代码:

    MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(31.25671959, 121.44902802) addressDictionary:nil];
    MKMapItem *fromLocation = [[MKMapItem alloc] initWithPlacemark:placemark1];
    fromLocation.name = @"上海市普陀区宜川路街道沪太路520-4号楼";
    
    MKPlacemark *placemark2 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(31.11640549, 121.51319885) addressDictionary:nil];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark2];
    toLocation.name = @"上海市闵行区浦江镇懿德路287-288号";
    
    [MKMapItem openMapsWithItems:@[fromLocation, toLocation]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
                                   MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

你可以把这里 NSNull 的 category 全部删掉,然后用 libextobjc/EXTNil 代替原来的 NSNull,或者干脆在遇到 nil 时不插入 placeholder

代替貌似没用,不插入placeholder可以

代替并且要把 NSNull 的 category 删了吗?导致 crash 的根源应该是那个 category

删了后,代替的[NSNull null]

我试了下,,是nilsale 导致的。我项目没有libextobjc/EXTNil 这个文件。然后我试着将- (void)gl_setObject:(id)anObject forKey:(id)aKey {
if (!aKey) {
return;
}
if (!anObject) {
// anObject = [NSNull null];
}
[self gl_setObject:anObject forKey:aKey];
}

  • (void)gl_setObject:(id)obj forKeyedSubscript:(id)key {
    if (!key) {
    return;
    }
    if (!obj) {
    // obj = [NSNull null];
    }
    [self gl_setObject:obj forKeyedSubscript:key];
    }
    这里注掉。就不会有EXC_BAD_ACCESS了。

但是我注掉 项目我都没对dic 做 null 判断。 那我该怎么搞

@Wind382690308 你试试改成 if(!key || !object) { return; }

不放 placeholder

perfect 。

  • (void)gl_setObject:(id)anObject forKey:(id)aKey {
    if (!aKey ||!anObject) {
    return;
    }
    if (!anObject) {
    anObject = [NSNull null];
    }
    [self gl_setObject:anObject forKey:aKey];
    }

  • (void)gl_setObject:(id)obj forKeyedSubscript:(id)key {
    if (!key || !obj) {
    return;
    }
    if (!obj) {
    obj = [NSNull null];
    }
    [self gl_setObject:obj forKeyedSubscript:key];
    }
    这样就可以了 。而且试了下其他也正常。
    dic {
    one = 1111,
    two =
    } dicidc {
    one = 1111,
    two =
    }