I have some source code to get the file name of an url
for example:
http://www.google.com/a.pdf
I hope to get a.pdf
because the way to join 2 NSStrings I can get is 'appendString' which only for adding a string at right side, so I planned to check each char one by one from the right side of string 'http://www.google.com/a.pdf', when it reach at the char '/', stop the checking, return string fdp.a , after that I change fdp.a to a.pdf
source codes are below
-(NSMutableString *) getSubStringAfterH : originalString:(NSString *)s0 { NSInteger i,l; l=[s0 length]; NSMutableString *h=[[NSMutableString alloc] init]; NSMutableString *ttt=[[NSMutableString alloc] init ]; for(i=l-1;i>=0;i--) //check each char one by one from the right side of string 'http://www.google.com/a.pdf', when it reach at the char '/', stop { ttt=[s0 substringWithRange:NSMakeRange(i, 1)]; if([ttt isEqualToString:@"/"]) { break; } else { [h appendString:ttt]; } } [ttt release]; NSMutableString *h1=[[[NSMutableString alloc] initWithFormat:@""] autorelease]; for (i=[h length]-1;i>=0;i--) { NSMutableString *t1=[[NSMutableString alloc] init ]; t1=[h substringWithRange:NSMakeRange(i, 1)]; [h1 appendString:t1]; [t1 release]; } [h release]; return h1; }
h1 can reuturn the coorect string a.pdf, but if it returns to the codes where it was called, after a while system reports 'double free *** set a breakpoint in malloc_error_break to debug'
I checked a long time and foudn that if I removed the code
ttt=[s0 substringWithRange:NSMakeRange(i, 1)];
everything will be Ok (of course getSubStringAfterH can not returns the corrent result I expected.), no error reported.
I try to fix the bug a few hours, but still no clue.
Welcome any comment
Thanks interdev
The getName() method is a part of File class. This function returns the Name of the given file object. The function returns a string object which contains the Name of the given file object.
The following line does the job if url is a NSString:
NSString *filename = [url lastPathComponent];
If url is a NSURL, then the following does the job:
NSString *filename = [[url path] lastPathComponent];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With