NSArray *urls = [fm contentsOfDirectoryAtURL:url includingPropertiesForKeys:nil options:0 error:&error];
returns an array of NSURL
objects, not NSString
s. You can convert the URL to a string
simply with
NSString *path = [urls[x] path];
that make the string manipulation obsolete. Alternatively, use
NSArray *files = [fm contentsOfDirectoryAtPath:directory error:&error];
which returns an array of NSString
objects instead of NSURL
s.