0

In my -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method I have this code to return youtube video date for cell label:

   //Video upload date
   NSString *formattedString = [[[self.videos objectAtIndex:indexPath.row] objectForKey:@"uploaded"] stringByReplacingOccurrencesOfString:@"T" withString:@""];

   formattedString = [formattedString stringByReplacingCharactersInRange:NSMakeRange(18, 5) withString:@""];

   NSDateFormatter *df = [[NSDateFormatter alloc] init];

   [df setDateFormat:@"yyyy-MM-ddHH:mm:ss"];

   NSDate *date = [df dateFromString:formattedString];

   [df setDateFormat:@"dd/MM/yyyy"];

   NSString *dateStr = [df stringFromDate:date];

   cell.date.text = dateStr;

My method implementation looks bit messy and I am wondering what is correct approach. Should I keep this code where it is or I should make another private method for the date formatting?

Let say I want to make private method. Something like:

   -(NSString *)formatDateWithString:(NSString *) mystring {

      NSString *formattedString = [mystring stringByReplacingOccurrencesOfString:@"T" withString:@""];

      formattedString = [formattedString stringByReplacingCharactersInRange:NSMakeRange(18, 5) withString:@""];

      NSDateFormatter *df = [[NSDateFormatter alloc] init];

      [df setDateFormat:@"yyyy-MM-ddHH:mm:ss"];

      NSDate *date = [df dateFromString:formattedString];

      [df setDateFormat:@"dd/MM/yyyy"];

      NSString *dateStr = [df stringFromDate:date];

      return dateStr; 
      }

What should I than write in -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method?

My argument for my private method will be [self.videos objectAtIndex:indexPath.row] but I've lost how do I call this method and on which instance variable?

I need to create an NSString instance variable in my view class or I can just initiate NSString instance in the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method and execute my private method on it?

Foggzie
  • 9,691
  • 1
  • 31
  • 48
Guferos
  • 4,337
  • 5
  • 18
  • 25
  • General rule of thumb, is if you intend to reuse logic, then place it into a reusable object. The idea is prevent duplication. In your case, create a `DateUtils` class and place your logic in there. Creating a category would be another option. – Jeremy Feb 12 '13 at 18:11
  • Possible duplicate of : http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c – Kekoa Feb 12 '13 at 18:12

2 Answers2

2

General rule of thumb, is if you intend to reuse logic, then place it into a reusable object. The idea is to prevent duplication. In your case, how about you create a DateUtils class and place your logic in there. Creating a category would be another option.

Example Usage:

NSString *youtubeVideoDateStr = [DateUtils formatDateWithString:@"..."];
Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • but if I am not going to reuse the logic and it will be used only once in my UITableView method than I should just leave it where it was? It is part of my uni assignment and I wasn't sure if this should be kept separately to make my code looks cleaner. – Guferos Feb 12 '13 at 18:19
  • You need to think a little more broadly. You may not think you need to reuse it now, but you should always make a habit of placing generic logic into its own reusable space. You may decide to do some formatting of your dates elsewhere. Think about it as though you are adding a useful tool to your tool chest which would become quite useful as your program becomes more complex. – Jeremy Feb 12 '13 at 18:23
1

There's not really a 'right' answer to this question. "Looking messy" is not a quantifiable problem so there's not way to measure the rightness of any changes.

If you can make a case that the code will be easier to maintain then perhaps you might be able to justify the time to re-factor it. Will more time be spent learning what it does than is spent simplifying it?

Jay
  • 13,803
  • 4
  • 42
  • 69