Storing acceleration values in Objective-C/Cocoa?
I'm writing for the iPhone and I'm trying to measure the time between maximum accelerations. Imagine you've attached your iPhone to an oscillating spring. What I want to do is measure the frequency of the oscillation. The way I am going about doing this is storing a certain number acceleration values (updated 20 times per second) in an NSMutableArray and comparing the current acceleration value with previous acceleration values.
How do I add each updated acceleration value to the NSMutableArray 开发者_运维技巧and at the same time delete values from the back of the array as I don't need them any more?
Thanks in advance!
Are NSMutableArray
s -addObject:
/-insertObject:atIndex:
and -removeLastObject
/-removeObjectAtIndex:
what you're looking for? See this one for more information.
Think of the array as a wheel. Once you have filled it up, use a rotating index for the replace location.
myIndex = ( ( myIndex + 1 ) % myCount );
[myArray replaceObjectAtIndex:myIndex withObject:newValue];
You can initialize it with a bunch of [NSNull null] objects that you will replace, so you do not need other special processing the first time through.
Once you have finished collecting data, organize the array to have the appropriate element first if it simplifies other operations.
NSRange range = NSMakeRange( 0 , myIndex );
NSArray *temp = [myArray subarrayWithRange:range];
[myArray removeObjectsInRange:range];
[myArray addObjectsFromArray:temp];
精彩评论