Get the extension of a file contained in an NSString

前端 未结 6 1942
[愿得一人]
[愿得一人] 2020-12-08 13:25

I have an NSMutable dictionary that contains file IDs and their filename+extension in the simple form of fileone.doc or filetwo.pdf. I need to determine what type of file it

相关标签:
6条回答
  • 2020-12-08 13:29

    In Swift 3 you could use an extension:

    extension String {
    
    public func getExtension() -> String? {
    
            let ext = (self as NSString).pathExtension
    
            if ext.isEmpty {
                return nil
            }
    
            return ext
        }
    }
    
    0 讨论(0)
  • 2020-12-08 13:35

    Try this, it works for me.

    NSString *fileName = @"yourFileName.pdf";
    NSString *ext = [fileName pathExtension];
    

    Documentation here for NSString pathExtension

    0 讨论(0)
  • 2020-12-08 13:39

    You're looking for [fileType pathExtension]

    NSString Documentation: pathExtension

    0 讨论(0)
  • 2020-12-08 13:46

    Try using [fileType pathExtension] to get the extension of the file.

    0 讨论(0)
  • 2020-12-08 13:48
    //NSURL *url = [NSURL URLWithString: fileType];
    NSLog(@"extension: %@", [fileType pathExtension]);
    

    Edit you can use pathExtension on NSString

    Thanks to David Barry

    0 讨论(0)
  • 2020-12-08 13:48

    Try this :

    NSString *fileName = @"resume.doc";  
    NSString *ext = [fileName pathExtension];
    
    0 讨论(0)
提交回复
热议问题