Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking code as modular as possible with private methods
    primarykey
    data
    text
    <p>In my <code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code> method I have this code to return youtube video date for cell label: </p> <pre><code> //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; </code></pre> <p>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?</p> <p>Let say I want to make private method. Something like: </p> <pre><code> -(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; } </code></pre> <p>What should I than write in <code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code> method? </p> <p>My argument for my private method will be <code>[self.videos objectAtIndex:indexPath.row]</code> but I've lost how do I call this method and on which instance variable? </p> <p>I need to create an <code>NSString</code> instance variable in my view class or I can just initiate NSString instance in the <code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code> method and execute my private method on it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload