【问题标题】:How to compare the NSTimeInterval(NSNumber Objects) inside a NSMutableArray?如何比较 NSMutableArray 中的 NSTimeInterval(NSNumber Objects)?
【发布时间】:2012-04-09 15:52:52
【问题描述】:

我有两个 NSMutableArrays(arrayContainsGoodClicks 和 arrayContainsBadClicks)。当相关的好和坏按钮被点击时,我正在用一些 NSTimeInterval 值初始化这些数组。现在我的要求是,我从带有一些 TimeIntervals 的 web 服务调用中得到响应。现在,我想将我从 Web 服务响应中获得的 NSTimeInterval 值与我存储在两个数组中的值进行比较。请在下面找到代码。

question answer="t" qno="1" tin="71" title="Greet" tout="73"
question answer="t" qno="2" tin="74" title="Have Name Tag" tout="77
question answer="t" qno="3" tin="78" title="Greet" tout="83"

我收到了上述格式的回复。我正在解析我的文件并存储 NSTimeInterval 值的相关详细信息。现在我想比较 71 和 73 秒(分别为 74 和 77,78 和 83)之间的时间间隔。我有我的数组,其中包含用户单击“好”和“坏”按钮的所有 timeInterval 值。

    -(void)onClickOfGood{
//NSLog(@"The current playback time in good:%g",moviePlayerController.currentPlaybackTime);
currentPlaybackTime = moviePlayerController.currentPlaybackTime;
NSNumber *goodTimeIntervals = [NSNumber numberWithDouble:currentPlaybackTime];
[arrayContainsGoodClicks addObject:goodTimeIntervals];
NSLog(@"The total count of Array is: %i",[arrayContainsGoodClicks count]);
NSLog(@"The Array contains : %@",arrayContainsGoodClicks);}

-(void)onClickOfBad{
NSLog(@"The current playback time in bad: %g",moviePlayerController.currentPlaybackTime);
currentPlaybackTime = moviePlayerController.currentPlaybackTime;
NSNumber *goodTimeIntervals = [NSNumber numberWithDouble:currentPlaybackTime];
[arrayContainsBadClicks addObject:goodTimeIntervals];
NSLog(@"The total count of Array is: %i",[arrayContainsBadClicks count]);
NSLog(@"The Array contains Bad Clicks are: %@",arrayContainsBadClicks);}

我的数组包含以下示例详细信息...

The Array containing Good Clicks are : (
"2.065027933",
"3.153256308",
"4.216946226",
"4.94465584",
"5.688772132",
"6.48904879",
"7.256447126",
"8.000711516000001",
"8.760312588",
"9.537493762",
"10.45637486",
"11.153212146",
"11.880587144",
"12.672884372",
"13.52852395"

)

The Array containing Bad Clicks are: (
"2.70485684",
"3.713548251",
"4.593316639",
"5.353822771",
"6.049754725",
"6.930190204",
"7.592959653",
"8.353438538000001",
"9.074305708000001",
"9.905327347",
"10.809726701",
"11.448938757",
"12.321753456",
"14.106061449"

)

有人可以帮助我了解如何比较我的数组中的 NSTimeIntervals 和收到的响应吗?

【问题讨论】:

    标签: iphone objective-c ios5 nsmutablearray nstimeinterval


    【解决方案1】:

    如果您拥有正确格式的数据,这似乎很简单。 NSTimeInterval 是一个typedef double,因此您可以使用基本运算符进行比较

    for(NSNumber* click in arrayContainsGoodClicks) {
        NSTimeInterval clickTimeInterval = [click doubleValue];     
        if(clickTimeInterval > 71.0 && clickTimeInterval < 73.0 ) {
           ...
        }
    }
    

    【讨论】:

    • 但是这里的问题是......每次在正确的时间间隔单击按钮时,数组都会添加相同的对象。我在 if 条件检查后以这种方式添加对象.. if(clickTimeInterval > 71.0 && clickTimeInterval
    • 嗨@amleszk。你能解释一下 for(NSNumber* click in arrayContainsGoodClicks) 是什么意思吗?它会检查什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多