Append NSData to a file in Objective C

天大地大妈咪最大 提交于 2020-01-02 00:14:10

问题


I'm attempting to download a large file on the iPhone and save it to disk as it is downloaded.

Ideally, in:

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {

}

I want to append 'data' to a file on disk.

I've checked out the NSData documentation. There are functions to write to a file, but I can't see anything to append to an existing file.

Does anyone know how I can append this data? Any help much appreciated! Nick.


回答1:


You can use a combination of -writeData: and -seekToEndOfFile methods from NSFileHandle class for writing NSData to the end of a file.




回答2:


There's a Apple sample app that downloads to a file, using NSOutputStream to write to the file. Look in file "GetController.h" of the sample app called "SimpleURLConnections".

Here's a direct URL -- although I don't think it'll work if you're not logged into to developer.apple.com.

https://developer.apple.com/library/ios/#samplecode/SimpleURLConnections/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009245



来源:https://stackoverflow.com/questions/1255295/append-nsdata-to-a-file-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!