gpt4 book ai didi

iphone - 如何使用sortUsingFunction :context

转载 作者:行者123 更新时间:2023-12-03 16:49:07 27 4
gpt4 key购买 nike

appdata.items 是 NSMutableArray。

我无法编译此代码。错误代码为“prop.173 的类型不完整”。

NSInteger compareInfo(id aInfo1, id aInfo2, void *context){
NSDate* info1 = [aInfo1 objectAtIndex:2];
NSDate* info2 = [aInfo2 objectAtIndex:2];
return [info1 compare:info2];
}

-(void)saveData{
NSData* data = [[NSMutableData alloc] init];
appdata.items = [appdata.items sortUsingFunction:compareInfo context:NULL];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:appdata forKey:DATAKEY];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[archiver release];
[data release];
}

最佳答案

这里有一些代码可能会对您有所帮助,(我花了很多时间使其工作,文档并没有真正的帮助)

它计算 POI 对象与当前位置之间的距离,并按照距该位置最近到最远的顺序排列 POI 对象

NSInteger compareDistance(id num1, id num2, void *context) 
{

int retour;
// fist we need to cast all the parameters
CLLocation* location = context;
POI* param1 = num1;
POI* param2 = num2;

// then we can use them as standard ObjC objects
CLLocation* locationCoordinates = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
CLLocation* locationPOI1 = [[CLLocation alloc] initWithLatitude:param1.coords.latitude longitude:param1.coords.longitude];
CLLocation* locationPOI2 = [[CLLocation alloc] initWithLatitude:param2.coords.latitude longitude:param2.coords.longitude];
CLLocationDistance distance1 = [locationPOI1 distanceFromLocation:locationCoordinates];
CLLocationDistance distance2 = [locationPOI2 distanceFromLocation:locationCoordinates];

//make the comparaison
if (distance1 < distance2)
retour = NSOrderedAscending;
else if (distance1 > distance2)
retour = NSOrderedDescending;
else
retour = NSOrderedSame;

[locationCoordinates release];
[locationPOI1 release];
[locationPOI2 release];
return retour;

}

-(void) orderByProximityFromLocation:(CLLocationCoordinate2D) coordinates
{
CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:coordinates.latitude longitude:coordinates.longitude];
[listOfPOI sortUsingFunction:compareDistance context:currentLocation];
[currentLocation release];

}

关于iphone - 如何使用sortUsingFunction :context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1736876/

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