How to make an NSString path (file name) safe

后端 未结 7 815
陌清茗
陌清茗 2021-02-01 01:52

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

7条回答
  •  旧时难觅i
    2021-02-01 02:23

    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

提交回复
热议问题