How to make an NSString path (file name) safe

2019-03-09 11:03发布

I'm using very tricky fighting methods :) to make a string like Fi?le*/ Name safe for using as a file name like File_Name. I'm sure there is a cocoa way to convert it. And I'm sure the best place to ask is here :)

Thank you!

7条回答
够拽才男人
2楼-- · 2019-03-09 11:59
NSURL *fileURL = [NSURL fileURLWithPath:yourFilePath isDirectory:NO];

if (fileURL) {

    NSError *error;

    fileURL = [NSURL URLByResolvingAliasFileAtURL:fileURL options:(NSURLBookmarkResolutionWithoutUI | NSURLBookmarkResolutionWithoutMounting) error:&error];

}

Before Transfer:

/Users/XXX/Desktop/~!@#$%^&*()_+`-={}|"<>?[]\;',.: {}<>:^ .png

After Transfer:

file:///Users/johnny/Desktop/~!@%23$%25%5E&*()_+%60-=%7B%7D%7C%22%3C%3E%3F%5B%5D%5C%3B',.:%20%20%7B%7D%3C%3E%5C:%5E%20.png

查看更多
登录 后发表回答