Objective-C magic path [16-Use Files], objective-c16-

Source: Internet
Author: User

Objective-C magic path [16-Use Files], objective-c16-

Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.

If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^

I want to donate: Click to donate

Cocos2d-X source code download: point I send


Language design is mainly used in practice,

The most widely used Objective-C is Mac OS X or iOS Foundation framework.


The Foundation framework allows you to use a file system to perform basic operations on files or directories. These basic operations are provided by the NSFileManager class.

You can use the NSFileHandle class to open a file and perform multiple read/write operations on the file.

The NSFileHandle class can implement the following functions:

1) open a file and perform read, write, or update (read and write) operations.

2) Find the specified location in the file.

3) read a specified number of bytes from the file or write the specified number of bytes to the file.


The NSFileHandle class provides methods for various devices or sockets.

The frameworks used in many languages also include FileHandle.

The NSURL class allows you to use the URL method in an application. This is similar to the URL in Java.

The NSBundle class allows you to use a bundle in an application, including searching for specific resources in a package.

This is more like a job in an Android app, such as Resource or R.


NSFileManager is used to manage files and directories. The path name of a file or directory is unique.

The complete path is also called an absolute path, starting with a slash.

Special generation character (~) As the abbreviation of the user's main directory.

The current directory is ".";

The parent directory is "...";

These concepts are the same as those in Linux.


Each file method calls the NSFileManager object,

The NSFileManager object is created by sending a defaultManager message to the class.

On iOS devices, the program runs in the sandbox, which strictly limits file access.

If you run this program on the device, the current directory is /,

This indicates that the root directory of the application is in the sandbox where it runs, and is not the root of the entire file directory of the iOS device.

It can be understood that every application is a process, so that its scope is the context of the process. Sandbox is a process.

This is the same as Android.

In Android, each application runs in a process, and each application has its own pid, that is, the process number.

You cannot access the data of other processes or affect other applications during crash. This is the principle of sandbox.

In fact, these similarities are due to the fact that the underlying operating systems of both ecosystems are caused by a similar unix principle.

Everyone familiar with Unix knows that processes and users are actually the concept of sandbox.


Use NSPathUtilities. h

Components is an NSArray object that contains a String object in each part of the path,

Path is a string object, which specifies the file Path; ext is a string object with the Path Extension. For example, @ "mp3"


To save data until the next running program can still be used, you can use the Documents directory.

Each iOS app has its own Documents directory for Data Writing.

The Caches directory in the application can also be used to store some data.

For iOS developers, Apple encourages developers to store persistent data on the cloud.


These Documents can be understood in this way. Every application is a user.

In Unix-like systems, every user is actually separated from different user folders in the home Directory (home in Linux.

Different user name folders have basic folders, such as Documents and Downloads.

In short, an application generally belongs to a user (UserId) and a process (Pid ).

Generally, the user and process IDs of different applications are different. This is the so-called sandbox principle.


The argments method in the NSProcessInfo class returns an array of string objects.

The first element of the array is the process name, and the other elements are input parameters in the command line.

This class is mainly used to record the information of the current process.

In fact, we all know that the process entry in unix is a main function.

This is why NSProcessInfo records the input parameters in the command line.

All input parameters in the command line are passed in through the input parameters of the main function.


Basic file operations: NSFileHandle, which can be used for standard input, standard output, standard error, and control devices.

Note that the NSFileHandle class does not provide the file creation function.

To create a file, use the FileManager method.

In addition, in Unix systems, it should be noted that opening a file for writing will not cut off the file, and you need to complete the truncation yourself.


The NSURL object is not a string (for example, @ "http://blog.csdn.net/haomengzhu "),

However, you can use URLWithString to create an NSURL object from a string object.


NSBundle class:

When creating an application, the system stores all data associated with the application (including images, localized strings, icons, and so on ),

Put the content in a package called application bundle.

Add a resource (slice or text file) to the application and drag the file to the left pane of Xcode.

The mainBundle method provides the directory where the application package is located. This method is applicable to Mac OS X and iOS.

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.