Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 31, 2008, 01:14 PM   #1
brygruver
macrumors member
 
Join Date: Aug 2007
No NSPredicate on iPhone, How do I filter an NSMutable Array?

So I have an Array (NSMutable) of objects and I am trying to filter them by date. Specifically I only want those objects that are less than 7 days old.

Last night I toiled away and got NSPredicate to do the job. And it did a BEAUTIFUL Job in the simulator. So I hook up my iPod touch and click Build and Go. Error, NSPredicate, bla bla bla. hmmmmm......So I start searching online and apparently NSPredicate is available in the simulator, but does NOT work on the actual Device. It is not included as part of the Foundation. Great.

So now I'm stuck, I don't know how else to do it. The Documentation specifically tells you to use NSPredicate to filter an Array. And that's the only suggestion that it gives you. (In hindsight, it also says that it's not available on the phone. lol)

Here is my code:

Code:
*NSDate nowDate = NSDate date;
// Take the Current date and find the time since 1970
NSTimeInterval now = nowDate timeIntervalSince1970;
// Take the time since 1970 and subtract the number of seconds in a week.
NSTimeInterval weekAgo = (now - 604800);

// Create the Predicate to do the Filtering.
*NSPredicate predicate = [NSPredicate predicateWithFormat:@"startDateTime >= %@", NSDate dateWithTimeIntervalSince1970: weekAgo];

// Load a New array with all of the sessions.
*NSMutableArray filteredArray = [NSMutableArray alloc initWithArray:appDelegate sessions];

// Fiter that new Array.
*filteredArray filterUsingPredicate:predicate; *
Are there any suggestions?

Sorry about the weird formatting, it's my first post.
brygruver is offline   0 Reply With Quote
Old Jul 31, 2008, 02:12 PM   #2
MacDonaldsd
macrumors 65816
 
MacDonaldsd's Avatar
 
Join Date: Sep 2005
Location: London , UK
you could just enumerate through the first array
So in non real code:

for( object in orignalArray){

if(Check to see if object is less that 7 days old){

add object to filtered array
}

}

Quote:
Originally Posted by brygruver View Post
So I have an Array (NSMutable) of objects and I am trying to filter them by date. Specifically I only want those objects that are less than 7 days old.

Last night I toiled away and got NSPredicate to do the job. And it did a BEAUTIFUL Job in the simulator. So I hook up my iPod touch and click Build and Go. Error, NSPredicate, bla bla bla. hmmmmm......So I start searching online and apparently NSPredicate is available in the simulator, but does NOT work on the actual Device. It is not included as part of the Foundation. Great.

So now I'm stuck, I don't know how else to do it. The Documentation specifically tells you to use NSPredicate to filter an Array. And that's the only suggestion that it gives you. (In hindsight, it also says that it's not available on the phone. lol)

Here is my code:

Code:
*NSDate nowDate = NSDate date;
// Take the Current date and find the time since 1970
NSTimeInterval now = nowDate timeIntervalSince1970;
// Take the time since 1970 and subtract the number of seconds in a week.
NSTimeInterval weekAgo = (now - 604800);

// Create the Predicate to do the Filtering.
*NSPredicate predicate = [NSPredicate predicateWithFormat:@"startDateTime >= %@", NSDate dateWithTimeIntervalSince1970: weekAgo];

// Load a New array with all of the sessions.
*NSMutableArray filteredArray = [NSMutableArray alloc initWithArray:appDelegate sessions];

// Fiter that new Array.
*filteredArray filterUsingPredicate:predicate; *
Are there any suggestions?

Sorry about the weird formatting, it's my first post.
MacDonaldsd is offline   0 Reply With Quote
Old Jul 31, 2008, 03:37 PM   #3
brygruver
Thread Starter
macrumors member
 
Join Date: Aug 2007
Almost got it.

Thank you soooo much, I think I've almost got it.
brygruver is offline   0 Reply With Quote
Old Jul 31, 2008, 05:03 PM   #4
brygruver
Thread Starter
macrumors member
 
Join Date: Aug 2007
Result

Code:
	NSDate *nowDate = [NSDate date];
	// Take the Current date and find the time since 1970
	NSTimeInterval now = [nowDate timeIntervalSince1970];
	// Take the time since 1970 and subtract the number of seconds in a week.
	NSTimeInterval weekAgo = (now - 604800);
	NSLog (@"Week Ago Date is %1.1f", weekAgo);
	NSMutableArray *filteredArray = [[NSMutableArray alloc] init];
	unsigned int i;
	for (i = 0; i < [appDelegate.sessions count]; i++) {
		id session = [appDelegate.sessions objectAtIndex: i];
		id startDate  = [[appDelegate.sessions objectAtIndex: i] startDateTime];
		NSTimeInterval startTime = [startDate timeIntervalSince1970];
		NSLog (@"Date of %@ is %@ or %1.1f", session, startDate, startTime);
		if (startTime > weekAgo) {
			[filteredArray addObject:session];
		}
	}
	NSLog(@"filteredArray = %@", filteredArray);
brygruver is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Old iBook HD crashed. All Data Still on iPhone. How Do I Merge to New MBP? MacintoshMan iPhone Tips, Help and Troubleshooting 4 Jun 3, 2010 04:54 PM
face-hacked on iphone. how do I log out? MichaelBarry iPhone 1 Jul 21, 2009 01:38 PM
Deleting an email on iPhone, how do you get it to delete in Gmail also? Vegeta-san iPhone 1 Aug 20, 2007 04:17 PM
Spam On Iphone How Do Stop That BigJohno iPhone 3 Jul 1, 2007 12:15 AM
websites on iphone? how do they fit? GianniCalvert iPhone Tips, Help and Troubleshooting 5 Jun 30, 2007 05:50 PM


All times are GMT -5. The time now is 06:40 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC