Reading and writing to files in Objective-C

158 Просмотры
Издатель
Subscribe to our channel here for notifications on new video trainings. For more videos on technology, visit our website at http://www.techytube.com.
By pavel@techytube, for more from this author visit:
http://www.techytube.com/author/pavel

In Objective-C we can easily read and write text files by using the NSString class methods, and the way we do that is as follows:

NSString *fileContent = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

where url is the location of our actual file.



NSUTF8StringEncoding parameter is an Enum values specified by the system and it is widely used for .txt files.


To write strings/text to files we use the following syntax:
[newFileContent writeToURL:
url atomically:YES encoding:NSUTF8StringEncoding error:nil];

where newFileContent is an object of NSString class, "atomically:YES" means that the system will first create a temporary file, and if everything executes successfully, it will rename that to the file in our url.
Категория
Язык программирования Objective-C
Комментариев нет.