Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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 如何在数组中搜索最近的日期时间_Iphone_Ios_Datetime_Nsarray - Fatal编程技术网

Iphone 如何在数组中搜索最近的日期时间

Iphone 如何在数组中搜索最近的日期时间,iphone,ios,datetime,nsarray,Iphone,Ios,Datetime,Nsarray,我正在尝试构建一个应用程序,其中包括随叫随到的技术人员名称。我有一个简单的NSArray,它包含以下格式的4个对象 20130910;0800;John Doe 20130910;1400;Sally Smith 20130910;2000;Jim Jones 20130911;0800;Jane Johnson 上面的格式是yyyyMMdd格式的日期、2400小时时间格式的时间以及技师姓名 我有两个stings*timeString和*dateString,它们具有与上面相同格式的本地设备的

我正在尝试构建一个应用程序,其中包括随叫随到的技术人员名称。我有一个简单的NSArray,它包含以下格式的4个对象

20130910;0800;John Doe
20130910;1400;Sally Smith
20130910;2000;Jim Jones
20130911;0800;Jane Johnson
上面的格式是yyyyMMdd格式的日期、2400小时时间格式的时间以及技师姓名

我有两个stings*timeString和*dateString,它们具有与上面相同格式的本地设备的时间和日期

我想在数组中搜索最近的过期日期/时间,以便将技师姓名分配给新字符串


使用上面的例子,如果是9月10日下午4点,我希望让Sally Smith回来,因为她在下午2点开始随叫随到。

这应该很简单。按alpha顺序对数组排序,也就是日期/时间/名称顺序

然后,当您有一个新的日期/时间时,对数组进行二进制搜索,将新的日期/时间字符串与数组中的日期/时间字符串进行比较。根据我的计算,二进制搜索将最多为您提供log2比较中的正确项

二进制搜索:具有最大和最小搜索范围。将其设置为要启动的阵列的开始/结束。将数组索引除以2,然后将字符串与该索引处的项进行比较。如果字符串>该索引处的字符串,请将最小搜索范围设置为当前索引。如果字符串<该数组索引处的字符串,请将最大搜索范围设置为新索引。如果它等于,你有一个匹配


重复上述步骤,直到找到您的物品。(我太累了,无法准确地说明退出条件。我将把它作为练习留给你。)

根据技术人员名单的大小,循环将起作用。下面的代码遍历列表,将每个项目分为三部分(日期、时间、技术人员),计算从现在起的时间间隔,并确定哪一个是最新/最活跃的代理(时间间隔应为最大负值)

为了得到有意义的东西,我更改了数组中的日期

NSArray *agents = [NSArray arrayWithObjects:
                        @"20130920;0800;John Doe",
                        @"20130920;1400;Sally Smith",
                        @"20130920;2000;Jim Jones",
                        @"20130921;0800;Jane Johnson",nil];

// Setup date formatter
NSDateFormatter* onCallFormatter = [[[NSDateFormatter alloc] init] autorelease];
[onCallFormatter setDateFormat:@"yyyyMMddHHmm"];
[onCallFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSTimeInterval mostRecent = -9999999999999;
NSInteger agentIndex;
int i;

for ( i=0; i < [agents count]; i++ ) {
    // Split string into elements
    NSArray *elements = [[agents objectAtIndex:i] componentsSeparatedByString:@";"];

    // Convert date/time into NSDate
    NSDate *onCallDateTime = [onCallFormatter dateFromString:[NSString stringWithFormat:@"%@%@", elements[0], elements[1]]];

    // Calculate the time interval against current date/time
    NSTimeInterval onCallInterval = [onCallDateTime timeIntervalSinceNow];

    // The agent on call would be the one with the largest negative interval
    // onCallInterval should be < 0 (postive would be in the future)
    if ( mostRecent < onCallInterval && onCallInterval < 0) {
        mostRecent = onCallInterval;
        agentIndex = i;
    }
    NSLog( @"%@ on call since %@ - %@ - %f hrs ", elements[2], elements[0], elements[1], onCallInterval/(60*60) );
}

NSLog( @"On call = %@", [agents objectAtIndex:agentIndex] );
NSArray*代理=[NSArray阵列包含对象:
@“20130920;0800;无名氏”,
@“20130920;1400;萨利·史密斯”,
@“20130920;2000;吉姆·琼斯”,
@“20130921;0800;简·约翰逊”,无);
//设置日期格式化程序
NSDateFormatter*onCallFormatter=[[NSDateFormatter alloc]init]autorelease];
[onCallFormatter setDateFormat:@“yyyyMMddHHmm”];
[onCallFormatter setTimeZone:[NSTimeZone timeZoneWithName:@“GMT”];
NSTimeInterval mostRecent=-9999999999;
NSInteger-agentIndex;
int i;
对于(i=0;i<[代理计数];i++){
//将字符串拆分为元素
NSArray*元素=[[agents ObjectatinIndex:i]由字符串@;“]分隔的组件;
//将日期/时间转换为NSDate
NSDate*onCallDateTime=[onCallFormatter dateFromString:[NSString stringWithFormat:@“%@%@”,元素[0],元素[1]];
//根据当前日期/时间计算时间间隔
NSTimeInterval onCallInterval=[onCallDateTime timeIntervalSinceNow];
//待命代理将是负间隔最大的代理
//OnCalInterval应<0(将来为正值)
if(最短
到目前为止,您尝试了哪些方法,但没有成功。请显示您的代码。已编辑的代码。过来看。