Object-C, file path API
Sharp complaints
1. they are also "file and directory operations". In java, java is used. util. A File class encapsulates a lot of APIS, and Object-C is engaged in so many classes and functions. The specific reasons are to be analyzed.
2. It is clear that it is an NSString, string operation, and how the "pathComponents" and other methods related to the Operation file path appear are very strange.
3. stringByAppendingString. The name of this function is a bit long.
4. In general, the Object-C syntax is more complex than Java, and the code efficiency is much lower.
/
/// Main. m // FilePathUtil /// Created by fansunion on 15/11/29. // Copyright (c) 2015 demo. All rights reserved. // # import
// Demo file path APIint main (int argc, const char * argv []) {@ autoreleasepool {NSString * fileName = @ "path. m "; NSFileManager * fm; NSString * path, * tempDir, * extention, * homeDir, * fullPath; NSArray * components; fm = [NSFileManager defamanager]; // temporary directory tempDir = NSTemporaryDirectory (); NSLog (@ "The tempDir is % @", tempDir); // extract The basic directory path = [fm currentDirectoryPath]; NSLog (@ "The base dir is % @", [path lastPathComponent]); // complete path of fileName in the current directory // This location has a problem // local output "/Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-bvzjqehotbexooebruphtwcmqekz/Build/ products/Debugpath. m "// Debug and path. there is no "separator"/"between m, and it is best to manually add some examples in the book. This separator is also not available in Java. You need to manually add fullPath = [path stringByAppendingString: fileName]; NSLog (@ "The fullPath is % @", fullPath); // obtain The file extension extention = [fullPath pathExtension]; NSLog (@ "The extentions is % @", extention); // obtain The user's home directory homeDir = NSHomeDirectory (); NSLog (@ "The home directory is % @", homeDir ); // The split path is components = [homeDir pathComponents]; for (path in components) {NSLog (@ "% @", path) ;}} return 0 ;}
Program output
13:43:30. 550 FilePathUtil [2861: 179163] The tempDir is/var/folders/4q/5ylpds9n5n97bq_r41qvly4w1_gn/T/
13:43:30. 551 FilePathUtil [2861: 179163] The base dir is Debug
13:43:30. 551 FilePathUtil [2861: 179163] The fullPath is/Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-examples/Build/Products/Debugpath. m
13:43:30. 551 FilePathUtil [2861: 179163] The extentions is m
13:43:30. 552 FilePathUtil [2861: 179163] The home directory is/Users/fansunion
13:43:30. 552 FilePathUtil [2861: 179163]/
13:43:30. 552 FilePathUtil [2861: 179163] Users
13:43:30. 553 FilePathUtil [2861: 179163] fansunion
Program ended with exit code: 0