How to convert NSIndexPath to NSString-iOS

11,594

Solution 1

I made this a category on NSIndexPath at one point in time:

@interface NSIndexPath (StringForCollection)

-(NSString *)stringForCollection;

@end

@implementation NSIndexPath (StringForCollection)

-(NSString *)stringForCollection
{
    return [NSString stringWithFormat:@"%d-%d",self.section,self.row];
}

@end

Solution 2

I made this extension to help with debugging:

@interface NSIndexPath (DBExtensions)
- (NSString *)indexPathString;
@end

@implementation NSIndexPath (DBExtensions)
- (NSString *)indexPathString;
{
  NSMutableString *indexString = [NSMutableString stringWithFormat:@"%lu",[self indexAtPosition:0]];
  for (int i = 1; i < [self length]; i++){
    [indexString appendString:[NSString stringWithFormat:@".%lu", [self indexAtPosition:i]]];
  }
  return indexString;
}
@end

Solution 3

You can't convert an NSIndexPath to a string -- an NSIndexPath is effectively just an array of integers. Assuming by "convert" you mean that you want to access the data associated with a particular path, you have to go back to the source of that data.

If you generated the table from an array of objects, which is commonly the case, then you'll simply look at the object at the array index equal to the first element of the indexPath. If the table is sectioned then you need to look at how the data was accessed in order to create the sections -- it likely relates to sorting of the objects based on some object property.

There's no conversion, there's just looking at the data source the same way it was accessed when generating the table.

Share:
11,594
Namratha
Author by

Namratha

Updated on June 11, 2022

Comments

  • Namratha
    Namratha about 2 years

    I want to convert NSIndexPath to NSString. How would I do it? I have to use this:

    - (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)sourcePath 
    {
        [client deletePath:@"/objc/boston_copy.jpg"];
    }
    

    inside commitEditingStyle method where I only get NSIndexPath as input.

    - (void)tableView:(UITableView *)aTableView 
            commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
            forRowAtIndexPath :(NSIndexPath *)indexPath 
    {                  
        [self.itemArray  removeObjectAtIndex:indexPath.row];          
        [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                          withRowAnimation:YES];    
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
                      withRowAnimation:UITableViewRowAnimationFade];   
    }  
    
    • ughoavgfhw
      ughoavgfhw about 13 years
      How are you supposed to get that from a set of numbers? We are going to need more information, or this question will be closed.
    • saadnib
      saadnib about 13 years
      why do you want to convert it in NSString?
    • Namratha
      Namratha about 13 years
      the parameter that deletePath takes-it is NSString. Can I simply typecast it? is I have (NSIndex Path*)indexPath; can I do (NSString*)indexPath and give it to deletePath.
    • Namratha
      Namratha about 13 years
      @ughoavgfhw: Check the edited question please.
    • Matthew Frederick
      Matthew Frederick about 13 years
      An NSIndexPath is effectively just an array of integers, so casting it won't do anything useful.
    • Namratha
      Namratha about 13 years
      okay. What do I do? I need to convert the information to NSString
    • mike nelson
      mike nelson over 4 years
      This is a good question. There can be many reasons someone might want to do this. Perhaps displaying to the user, sending over the wire, saving in a database, logging, or hovering in the debugger.