nsfilehandle

Append new string to txt file in swift 2

别等时光非礼了梦想. 提交于 2019-12-08 06:50:54
问题 I used to do coding in java but now I want to move my app to iOS using swift 2. So I want a method to append text in new line to an existing txt file in app documents. I searched and tried so many methods but its all overwriting to new txt file In java i used this method PrintWriter out=null; try { out=new PrintWriter(new FileWriter("file.txt",true)); out.println("some txt"} out.close(); } catch (IOException e) { e.printStackTrace(); } 回答1: I think you must use NSFileHandle : let dir:NSURL =

Append new string to txt file in swift 2

早过忘川 提交于 2019-12-07 04:06:28
I used to do coding in java but now I want to move my app to iOS using swift 2. So I want a method to append text in new line to an existing txt file in app documents. I searched and tried so many methods but its all overwriting to new txt file In java i used this method PrintWriter out=null; try { out=new PrintWriter(new FileWriter("file.txt",true)); out.println("some txt"} out.close(); } catch (IOException e) { e.printStackTrace(); } I think you must use NSFileHandle : let dir:NSURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.CachesDirectory, inDomains:

NSFileHandle writeData: exception handling

匆匆过客 提交于 2019-12-05 00:40:44
问题 This could be rather broad problem but I could not find any online resource addressing or explaining this matter. The question is after creating NSFileHandle *writer = [NSFileHandle fileHandleForWritingAtPath:"path"] and when you use [writer writedata:"NSData"] there are sine possible exception that could fire according to the apple doc. "This method raises an exception if the file descriptor is closed or is not valid, if the receiver represents an unconnected pipe or socket endpoint, if no

Async execution of shell command not working properly

心不动则不痛 提交于 2019-12-02 12:34:26
问题 So, this is my code : - (void)runCmd:(NSString *)cmd withArgs:(NSArray *)args { NSLog(@"\nRunning ::\n\tCmd : %@\n\tArgs : %@",cmd, args); [theSpinner start]; if (task) { [task interrupt]; } else { task = [[NSTask alloc] init]; [task setLaunchPath:cmd]; [task setArguments:args]; [pipe release]; pipe = [[NSPipe alloc] init]; [task setStandardOutput:pipe]; NSFileHandle* fh = [pipe fileHandleForReading]; NSNotificationCenter* nc; nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self

Async execution of shell command not working properly

走远了吗. 提交于 2019-12-02 04:16:35
So, this is my code : - (void)runCmd:(NSString *)cmd withArgs:(NSArray *)args { NSLog(@"\nRunning ::\n\tCmd : %@\n\tArgs : %@",cmd, args); [theSpinner start]; if (task) { [task interrupt]; } else { task = [[NSTask alloc] init]; [task setLaunchPath:cmd]; [task setArguments:args]; [pipe release]; pipe = [[NSPipe alloc] init]; [task setStandardOutput:pipe]; NSFileHandle* fh = [pipe fileHandleForReading]; NSNotificationCenter* nc; nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self]; [nc addObserver:self selector:@selector(dataReady:) name:NSFileHandleReadCompletionNotification

How to save images and recorded files in temp directory?

不问归期 提交于 2019-12-01 05:10:39
I want to store pictures taken from camera and video recordings from my application in a separate folder in temporary directories for a while. And as the task is being completed, they shall be going to save into the database. How do I save pictures taken from camera & video recording files in a separate folder in temp directories? You are looking for this to get tot he caches folder to store temporary files: NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; From there you can use NSFileManager to create and remove the directories you

How to properly handle NSFileHandle exceptions in Swift 2.0?

被刻印的时光 ゝ 提交于 2019-11-30 06:41:07
First of all, I am new to iOS and Swift and come from a background of Android/Java programming. So to me the idea of catching an exception from an attempt to write to a file is second nature, in case of lack of space, file permissions problems, or whatever else can possibly happen to a file (and has happened, in my experience). I also understand that in Swift, exceptions are different from Android/Java ones, so that's not what I'm asking about here. I am attempting to append to a file using NSFileHandle, like so: let fileHandle: NSFileHandle? = NSFileHandle(forUpdatingAtPath: filename) if

How to properly handle NSFileHandle exceptions in Swift 2.0?

守給你的承諾、 提交于 2019-11-29 06:22:26
问题 First of all, I am new to iOS and Swift and come from a background of Android/Java programming. So to me the idea of catching an exception from an attempt to write to a file is second nature, in case of lack of space, file permissions problems, or whatever else can possibly happen to a file (and has happened, in my experience). I also understand that in Swift, exceptions are different from Android/Java ones, so that's not what I'm asking about here. I am attempting to append to a file using

iOS ALAssetsLibrary and NSFileHandle reading file contents

谁说胖子不能爱 提交于 2019-11-28 05:36:23
问题 I want to read the contents of an assets library file in iOS NSFileHandle fileHandleForReadingFromUrl using the asset defaultRepresentation url seems to always return 0x0 ... I'll keep looking for a solution in the mean time. EDIT: Looks like the answer from Anomie might be what I want: NSUInteger length = [representation getBytes:bytes fromOffset:0 length:[representation size] error:&error]; 回答1: For larger files you probably want to copy out via a loop to read X bytes in chunks, otherwise

NSString containing hex convert to ascii equivalent

隐身守侯 提交于 2019-11-28 00:20:21
I have an NSString that has hex information such as <00000020 66747970 4d344120 00000000 4d344120 6d703432 69736f6d 00000000 00031203 which came from NSData. What I need to do is convert that NSString of Hex data to Ascii which would be: [0][0][0] ftypM4A [0][0][0][0]M4A mp42isom[0][0][0][0][0][3][18][3] As you might be able to tell this is a M4A file. I loaded the first part of the file in to NSData using NSFileHandle. I then stored it into NSString: NSData *data = [filehandle readDataOfLength:1000]; NSString *str = [[NSString alloc]initWithString:[NSString stringWithFormat:@"%@",data]];