Object-C, file path API

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.