1

The NSMutableArray (inboxFaxItems)consists of strings set up like: MM/DD/YYYY hh:mm:ss a for example: 2/2/2011 2:46:39 PM 2/4/2011 11:59:47 AM

I need to be able to sort this array so that the newest dates are at the top.

#import <UIKit/UIKit.h>


@interface InboxTableViewController : UITableViewController<NSXMLParserDelegate> {


    //all of these need to be put in custom class
    NSMutableArray *inboxFaxItems;
    NSMutableArray *dateArray;
    NSMutableArray *inboxMessageID;
    NSMutableArray *inboxMessagePageCount;
    NSMutableArray *inboxMessageFrom;



    NSMutableData *xmlData;
    NSURLConnection *connectionInprogress;
    NSMutableString *inboxFaxesString;
    UIActivityIndicatorView *activityIndicator;

}


-(void) loadInbox;

@end



- (void)dealloc {
    [inboxFaxItems release];
    inboxFaxItems = nil;

    [inboxMessageID release];
    inboxMessageID = nil;

    [inboxMessagePageCount release];
    inboxMessagePageCount = nil;

    [inboxMessageFrom release];
    inboxMessageFrom = nil;

    [dateArray release];
    dateArray = nil;


    [super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [dateArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"InboxFaxItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    [[cell textLabel]setText:[dateArray objectAtIndex:[indexPath row]]];
    cell.imageView.image = [UIImage imageNamed:@"document.png"];
    return cell;
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection{


    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];
    [parser setDelegate:self];
    [parser parse];
    [parser release];

    //do sorting here
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];

    for (NSString *dateString in inboxFaxItems) {
        NSDate *date = [formatter dateFromString:dateString];
        if (date) [dateArray addObject:date];
        // If the date is nil, the string wasn't a valid date.
        // You could add some error reporting in that case.
    }

    [[self tableView] reloadData];

    [connectionInprogress release];
    connectionInprogress = nil;

    [xmlData release];
    xmlData = nil;


}

However I am getting errors:

2011-06-29 02:49:28.782 app[4388:207] -[__NSDate isEqualToString:]: unrecognized selector sent to instance 0x4d9e090
2011-06-29 02:49:28.784 app[4388:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate isEqualToString:]: unrecognized selector sent to instance

OK THIS SEEMS TO HAVE FIXED IT: (UPDATE)

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"InboxFaxItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    NSDate *date = [dateArray objectAtIndex:[indexPath row]];

    NSString *dateStr = @"";
    dateStr = [formatter stringFromDate:date];

    [[cell textLabel]setText:dateStr];
    cell.imageView.image = [UIImage imageNamed:@"document.png"];
    return cell;
}
9
  • 2
    Where are you sorting? And use yyyy instead of YYYY and dd instead of DD in the date format. DD indicates date of the year and not month. YYYY will not always be same as the calendar year. See this for more. Commented Jun 29, 2011 at 8:06
  • The code that actually triggers the error doesn't appear to be included; none of the methods being called here should cause isEqualToString: to be called; except possibly [[self tableView] reloadData], but without the code to setup your table view, it's hard to tell. Commented Jun 29, 2011 at 8:07
  • @Deepak dates I am getting are like: 2/2/2011 2:46:39 PM 2/4/2011 11:59:47 AM. Also please see this question that I asked a few months back: stackoverflow.com/questions/5958998/… Commented Jun 29, 2011 at 8:17
  • @Williham Totland This is the only place where I am ding anything with a NSDate. Commented Jun 29, 2011 at 8:18
  • @jini I just saw your earlier question and tested some of your dates in it against this date format and it isn't working right i.e. the date you're getting isn't proper. Use @"MM/dd/yyyy hh:mm:ss a" as your date format. Commented Jun 29, 2011 at 8:24

1 Answer 1

2

Here's your problem:

[[cell textLabel] setText:[dateArray objectAtIndex:[indexPath row]]];

setText: assumes it gets a string, and as such, when deciding wether or not to update itself, it sends an isEqualToString: method to the object it is passed, but SURPRISE!: It's an NSDate object. Which doesn't respond to that selector, obviously.

What you need to do is to make a string from the date before you pass it to the label, and everything will come up roses.

- (NSString *)descriptionWithLocale:(id)locale

is probably the correct method for achieving this; but it's possible that

- (NSString *)description

will do all the relevant locale stuff automagically.

Sign up to request clarification or add additional context in comments.

3 Comments

You were correct. I have updated my code above and things seem to be fine. If you could kindly just take a look at my updated method. I am not too concerned with locales at the moment since the date comes from the server.
@jini: Looks about right, but if you're not doing anything with the dates other than displaying them; I would advise moving to a more sane date format, like YYYY-MM-DDTHH:MM:SS (ISO format). That'll allow simple textual sorting, solving a number of headaches.
I will bring this up in front of client since they are the ones makin the web services available. thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.