Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Iphone CLLocation导致EXC\u访问错误_Iphone_Objective C_Ios_Automatic Ref Counting_Cllocation - Fatal编程技术网

Iphone CLLocation导致EXC\u访问错误

Iphone CLLocation导致EXC\u访问错误,iphone,objective-c,ios,automatic-ref-counting,cllocation,Iphone,Objective C,Ios,Automatic Ref Counting,Cllocation,这段代码有时会工作,但有时会因EXC_BAD_访问错误而崩溃 NSInteger compareDistance(id num1, id num2, void *context) { int rv; //cast parameters from general type CLLocation* location = (__bridge_transfer CLLocation *)context; Attraction* attr1 = num1; Att

这段代码有时会工作,但有时会因EXC_BAD_访问错误而崩溃

NSInteger compareDistance(id num1, id num2, void *context) 
{
    int rv;
    //cast parameters from general type
    CLLocation* location = (__bridge_transfer CLLocation *)context;
    Attraction* attr1 = num1;
    Attraction* attr2 = num2;

    //convert to CLLocation objects and calculate distance from user current
    CLLocation* locationAttr1 = 
    [[CLLocation alloc] initWithLatitude:attr1.latitude 
                               longitude:attr1.longitude];
    CLLocation* locationAttr2 = 
    [[CLLocation alloc] initWithLatitude:attr2.latitude 
                               longitude:attr2.longitude];
    CLLocationDistance distance1 = [locationAttr1 distanceFromLocation:location];
    CLLocationDistance distance2 = [locationAttr2 distanceFromLocation:location];

    //compare and rate
    if (distance1 < distance2)
        rv = NSOrderedAscending;
    else if (distance1 > distance2)
        rv = NSOrderedDescending;
    else
        rv = NSOrderedSame;

    return rv;
}
嗯,你认为问题可能是ARC吗?我正侧视着这桩生意


提前感谢您的建议

\uuuuu bridge\u transfer
将所有权转移到目标对象,这可能不是您在这里想要做的,您不想拥有
上下文的所有权,因为ARC会在认为您已完成时尝试释放它,而您不希望它这样做


基本上,您不想将所有权转移到任何地方,所以在这两种情况下都只需使用
\uuuu桥
,它应该可以正常工作。

\uu桥
将所有权转移到目标对象,这可能不是您想要做的,您不想要
上下文的所有权,因为ARC会在它认为你已经完成的时候尝试释放它,而你不希望它这样做


基本上,您不想在任何地方转移所有权,所以在这两种情况下只使用<代码>π桥接器/COD>,它应该工作得很好。

可能要考虑打开NSZOMBeEnabl。HMM,我尝试过,并且得到了这个错误:<代码> -[CurryPrave]:发送给Debug实例0x6AC2090 < /代码>的消息。代码> Excel断点(代码=ExcIII366BPT,子代码=0x0 -[CurryPrave]:发送到DeXOffice实例0x6AC2090
的消息。<代码> Excel断点。(code=EXC\u I386\u BPT,subcode=0x0
。手动计算坐标距离可能比这更容易…编译器除了在那里传输之外,不会接受任何东西。它不会接受
\u桥
,也不会接受
\u桥
。当然,它不会接受简单地传递
电流entLocation
原样。哦,等等,把它擦掉,我以为你指的是调用函数中保留的
\uuuu桥。尝试
\uu桥
。初步结果很有希望。:)谢谢你的建议。编译器只接受那里的
\uuuuuu bridge\u transfer
。它不接受
\uuuuu bridge
,也不接受
\uuuu bridge\u retain
。埃塔:当然,它也不接受简单地按原样传递
当前位置
。哦,等等,划掉它,我以为你指的不是o
\uuuuu bridge\u保留在调用函数中。尝试
\uuuu bridge
。初步结果令人满意。:)感谢您的建议。
-(NSMutableArray *)getAttractionsByDistanceInCategory:(int)catID
{
    [self confirmAttractions];

    //set up array and context to prepare for sort
    NSMutableArray *attractionsToSort = [[NSMutableArray alloc] init];
    for (Attraction *a in attractions)
    {
        if ((catID < 0) || (catID >= 0 && a.category == catID))
            [attractionsToSort addObject:a];
    }

    CLLocation* currentLocation = 
        [[CLLocation alloc] initWithLatitude:usersLat longitude:usersLng];

    //conduct sort
    [attractionsToSort sortUsingFunction:compareDistance
                                 context:(__bridge_retained void *)currentLocation];

    return attractionsToSort;
}