Objective-c's Path to Magic "16-Use File"

Source: Internet
Author: User

Shimen the main contribution of the Lord, respect the author's labor results. Please do not reprint.

If the article is useful to you, you are welcome to donate it to the author. Support Shimen Master, donate amount arbitrarily. Heavy on the mind ^_^

I want to donate: Click Donate

Cocos2d-x source code Download: Dot I teleport


The design of language is mainly applied in practice,

The most widely used area of OBJECTIVE-C is the foundation framework for Mac OS X or iOS.


The Foundation framework agrees that you use the file system to run basic operations on files or folders, which are provided by the Nsfilemanager class.

Using the method provided by the Nsfilehandle class, you can open a file and run multiple read/write operations on the file.

The methods of the Nsfilehandle class are capable of implementing functions such as the following:

1) Open a file to run read, write, or update (read and write) operations.

2) Locate the specified location in the file.

3) read a specific number of bytes from the file. Or writes a specified number of bytes to the file.


The methods provided by the Nsfilehandle class can also be used with various devices or sockets.

The frameworks used in very many languages also have similar filehandle.

The Nsurl class agrees to use the URL method in your app.

This is almost identical to the URL in Java.

The NSBundle class provides a way to consent to the use of packages (bundles) in your application. Contains the specific resources in the search package.

This thing is more like resource or R in Android apps.


Manage files and folders using Nsfilemanager, a file or folder using the path name of the file is a unique identity.

The full path is also known as an absolute path, beginning with a slash (/).

The Special generation character (~) is used as the abbreviation for the user's home folder.

The current folder is "." ;

Parent folder is ".." ;

These concepts are the same as in Linux.


Each file method is a call to a Nsfilemanager object,

The Nsfilemanager object is created by sending a Defaultmanager message to the class.

On your iOS device. The program is executed in a sandbox, and it strictly restricts access to the file.

Assuming that the program is executed on the device, you will see that the current folder is/,

This means that the root folder of the app is in the sandbox where it was executed, not the entire iOS device Files folder.

To be able to understand this, each application is a process. So its scope is the context of the process.

A sandbox is a process.

This is the same reason as Android.

Android. Each application is executed in a process, and each application has its own PID, the process number.

It is also not easy to access data from other processes or to affect other applications when it crashes.

This is how the sandbox works.

In fact, these same points are caused by the Unix-like principles of their two ecosystems, the lowest OS.

The classmates who are familiar with Unix know. Process. The concept of the user, in fact, is the concept of sandbox.


Use the path with NSPathUtilities.h

Component is a Nsarray object that includes a string object for each part of the path.

Path is a string object. It specifies the path to the file; Ext is a string object with the path extension.

such as @ "MP3"


In order to save the data until the next execution of the program can still be used. You can use the Documents folder.

Each iOS app has its own Documents folder for data writing.

The caches folder in your app can also be used to store some data.

For iOS development says. Apple inspires developers to store persistent data in the cloud.


Understanding of these documents. To understand this, every application is a user.

In a Unix-like system. Each user is in fact separated from the different user folders in the home folder (the home of the Linux system).

There are main directories in different username directories, documents,downloads this kind of.

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

And under normal circumstances, the user identity and process identity of different applications are not the same. 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 remaining elements are the parameters that are entered on the command line.

This class is primarily used to record information about the current process.

As a matter of fact, we all know. The process portals under UNIX are all main functions.

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

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


Basic file Operation: Nsfilehandle, can be used for standard input, standard output, standard error and control device.

It should be noted that the Nsfilehandle class does not provide the ability to create files.

The create file must be created using the FileManager method.

In addition, under UNIX systems, it should be noted that opening a file for writing does not truncate the file. You need to complete the truncation.


The Nsurl object is not a string (such as @ "Http://blog.csdn.net/haomengzhu").

However, use the URLWithString: method to create a Nsurl object from a String object.


NSBundle class:

When an app is created, the system stores all the data associated with the app (including pictures, localized strings, icons, etc.).

Put the content in a package called an app package (application bundle).

To add a resource (slice or text file) to your app, simply drag the file to the left pane of Xcode.

The Mainbundle method gives the folder where the app package resides. This approach is available in Mac OS X and iOS.

Objective-c's Path to Magic "16-Use File"

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.