This method returns a boolean YES or NO result and takes as arguments the pathname for the file to be moved, the destination path and an optional NSError object into which information describing any errors encountered during the operation will be placed.
If no error description information is required, this argument may be set to NULL. Note that if the destination file path already exists this operation will fail. File copying can be achieved using the copyItemAtPath method. As with the move method, this takes as arguments the source and destination pathnames and an optional NSError object. Success of the operation is indicated by the returned boolean value:.
The removeItemAtPath method removes the specified file from the file system. The method takes as arguments the pathname of the file to be removed and an optional NSError object. The success of the operation is, as usual, reported in the form of a boolean YES or NO return value:.
A symbolic link to a particular file may be created using the createSymbolicLinkAtPath method. This takes as arguments the path of the symbolic link, the path to the file to which the link is to refer and an optional NSError object.
The NSFileManager class includes some basic file reading and writing capabilities. Firstly, the contents of a file may be read and stored in an NSData object through the use of the contentsAtPath method:. Having stored the contents of a file in an NSData object, that data may subsequently be written out to a new file using the createFileAtPath method:.
This, however, gives us no control over how much data is to be read or written and does not allow us to append data to the end of an existing file. Clearly some more flexible mechanism is required. The NSFileHandle class provides a range of methods designed to provide a more advanced mechanism for working with files.
In addition to files, this class can also be used for working with devices and network sockets. In the following sections we will look at some of the more common uses for this class. An NSFileHandle object can be created when opening a file for reading, writing or updating reading and writing.
Having opened a file, it must subsequently be closed when we have finished working with it using the closeFile method. If an attempt to open a file fails, for example because an attempt is made to open a non-existent file for reading, these methods return nil. For example, the following code excerpt opens a file for reading and writing and then closes it without actually doing anything to the file:. NSFileHandle objects maintain a pointer to the current position in a file.
This is referred to as the offset. When a file is first opened the offset is set to 0 the beginning of the file. This means that any read or write operations we perform using the NSFileHandle methods will take place at offset 0 in the file.
To perform operations at different locations in a file for example to append data to the end of the file it is first necessary to seek to the required offset. For example to move the current offset to the end of the file, use the seekToEndOfFile method. Alternatively, seekToFileOffset allows you to specify the precise location in the file to which the offset is to be positioned. Finally, the current offset may be identified using the offsetInFile method.
In order to accommodate large files, the offset is stored in the form of an unsigned long long. The following example opens a file for reading and then performs a number of method calls to move the offset to different positions, outputting the current offset after each move:.
File offsets are a key aspect of working with files using the NSFileHandle class so it is worth taking extra time to make sure you understand the concept. I registered the file types in Info. I did everything by this post , which was marked as solved. The problem is that this works only if I drag and drop my file on my application while it is running.
But it doesn't work if I just double click on my file. It starts my application, but not as it would start if I would drag and drop. So the code which is in application:openFile: doesn't run when double-clicked, but only when I drag and drop my file. Some more detail about my code, and what I am trying to achieve. I created a wrapper application for an other app.
Let's call the other app the "HelperApp. With the wrapper app I specified a new file type, let's call it ". This file contains some argument commands. What I try to achieve, that when a user clicks on a file which is a ". This HelperApp is opening different things depending on the argument it gets.
Below you can check my code. I have an AppDelegate. I added this line to my AppDelegate. If I comment out the above function from the processFile function in AppDelegate , then everything works "smoothly". I mean the wrapper app starts and it starts the HelperApp with default arguments. So here should be the error If you've implemented -application:openFile: , it should be called when you double-click a file of the type that you've registered.
You say that the app launches, so the OS is trying to use your app to open the file. Here's a useful note from the documentation:. If the user started up the application by double-clicking a file, the delegate receives the application:openFile: message before receiving applicationDidFinishLaunching:. So, if you're doing anything in -applicationDidFinishLaunching: that has to be done before you open any files, that could be your problem. Consider moving your app initialization code to -applicationWillFinishLaunching:.
I've figured it out. When you double-click on a file icon, the application will launch itself, other things done correctly. But the application that responds to your action is not necessarily the one that you built for the last time. Probably, an old copy of your application is responding. You should see many folders for your application. You can locate your application folders by right-clicking and choosing Shown In Finder after build one. Trash them all, and build a new application. Then double-click and see what happens now.
The problem was, that I gave the wrong path in my function. This path worked if I started the app from Xcode, but did not if I started the app by itself. Here is the post which solved my problem! The Apple Docs leave out a vital piece of info Its NOT!
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Open file with own application written in objective-c Ask Question. Asked 8 years, 7 months ago. Active 7 years, 3 months ago. Viewed 4k times. Improve this question.
0コメント