Equivalent for “stringByDeletingPathExtension ” for an array

我们两清 提交于 2019-12-25 12:05:25

问题


I want to run stringByDeletingPathExtension for all NSString members of an NSArray. How can I do this in Objective-C?


回答1:


NSArray *myArray = ... ;

[myArray makeObjectsPerformSelector:@selector(stringByDeletingPathExtension)]; 

EDIT: As @Wevah pointed out in the comments, this solution does not solve the problem, since stringByDeletingPathExtension returns a string rather than modified the object on which is invoked.

NSMutableArray *newArray = [[[NSMutableArray alloc] initWithArray:myArray] autorelease];
for( int i = 0; i < [newArray count]; i++ ) {
   NSString* oneItem = [newArray objectAtIndex:i];
   [newArray replaceObjectAtIndex:i withObject:[oneItem stringByDeletingPathExtension]]; 
}

then use your newArray ...



来源:https://stackoverflow.com/questions/5015050/equivalent-for-stringbydeletingpathextension-for-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!