Search code examples
iosnsstringnsarraykey-value-coding

KVC get lastPathComponent from string which is the property of object


I have e.g. 5 MyFile objects in my NSArray. Each of these 5 objects has property path.

MyFile *myFile ...;

NSString path = myMyFile.path;

So using KVC I can get the properties form all my 5 objects in the array:

NSArray *folders = [[PNFolder MR_findAllWithPredicate:predicate] valueForKey:@"path"];

so code above will return paths instead of MyFile objects and this is ok for me.

But, how can I using the lastPathComponent here as well to make my array return me just folder name instead of full path. I mean using KVC. or do I need to write cycle?


Solution

  • use valueForKeyPath with the array

    NSArray *folderNames = [[PNFolder MR_findAllWithPredicate:predicate] valueForKeyPath:@"path.lastPathComponent"];