gpt4 book ai didi

iphone - CLLocation 导致 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-28 20:36:17 24 4
gpt4 key购买 nike

此代码有时可以正常工作,但有时会因 EXC_BAD_ACCESS 错误而崩溃。

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;
}

这是一个用于排序 NSMutableArray 的比较函数:

-(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;
}

嗯。您认为问题可能出在 ARC 上吗?我正在关注 __bridge_retained void * 业务。

提前感谢您的任何建议!

最佳答案

__bridge_transfer将所有权转移到目标对象,这可能不是您想在此处执行的操作,您不想要 context 的所有权,因为 ARC 会在它认为您已完成它而您不希望它这样做时尝试释放它。

基本上,您不想在任何地方转让所有权,所以只需使用 __bridge在这两种情况下,它应该都能正常工作。

关于iphone - CLLocation 导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10407150/

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