Deleting a line from a txt file in objective-c - iphone

Is there a way to remove a line from the end of a .txt file from Objective-C? I can't seem to find anything on manipulating text files from Objective-C, only reading them into a NSString.

Have you considered changing your data model to a plist? Plists are more easily read/written into/from NSDictionaries.
Otherwise, I think the only way is to read the file into a NSString, separate into a component NSArray by splitting on \n, remove the object at index n, write back into a string by joining with component \n, then writing back to the file.

Related

Remove quotation marks - Swift

From a .txt file I get strings that are full of quotation marks which of course are terrible to use in code. e.g.
3820,"20170217",8,752,119,"Rh",,"fr",,,,"iC1.2","iC1.2",,"IS6a","Z",,0,"IS6a",,201702161517,"-"
3821,"20170227",2,753,207,"Dd","Kru","sc",,,,"iB8","iB8",,"IS9b","Z",,2097152,"IS9b",,201702270804,"+~-"
3822,"20170227",3,753,8,"Dd",,"phH_1",,,,"iB8",,,"IS12~IT12","Z",,2097153,"IS12~IT12",C,201702270804,"-"
3823,"20170227",4,753,29,"Dd",,"phH_1",,,,"iB8",,,"IS11~IT11","Z",,2097153,"IS11~IT11",C,201702270804,"-"
How could I best get rid of these quotation marks??
Thanks in advance
This is CSV, so you'll need to parse it as CSV. There are several problems with your approach:
The quotes mess everything up
It's a mixture of strings and numbers - you won't be able to use an array
There are consecutive commas, that would be illegal in an array literal.
The best approach is to put the data in a file (in your application bundle if you like) and find a CSV parser to read the data. My quick Google yielded SwiftCSV which looks reasonable, but I've never used it.

Decode base64 array of bytes to txt file - iPhone

I'm geting an array of bytes (it's a txt file) from a server that I need to save at my app's documents folder. I,ve tried many solutions I found and I managed to save the file, but the encoding appears to be wrong, when I try to view the contents of the txt file saved the first line says:
<.base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
...then about 50 lines of random characters until the document ends with:
<./base64Binary>
I've tried decoding the NSData to nsstring using some examples i've found, but none could solve this problem
thanks
I've tried this solutions:
http://www.cocoadev.com/index.pl?BaseSixtyFour
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
Finally solved it!! Using part of the code in
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
into my classes and parsing the xml before encoding.

How would I go about parsing this .xml for iPhone table

I am making a directory for an app and I need to parse the the names, e-mail, phone #, and office for each item that I want to display in a UITableView. I have a class made but I have never really dealt with pasring anything past simple txt files.
I need to load a URL to a xml file, which consists of the following type of data at the bottom. It does not have xml tags, but it is saved as a .xml
I have read up on the NSXMLParsers, but I wasn't sure if that would be the correct way to do this or if there was an easier way.
Example of part of the .xml file below, this is just part of a few hundred lines that are organized in the same manner, by division, department, then person.
Thanks for any help!
http://cs.millersville.edu/School of Science and MathematicsDr.FirstH.LastRoddy Science CenterFirst.Last#millersville.edu872-3838Computer ScienceMrs.First.LastRoddy Science CenterFirst.Last#millersville.edu872-3858Computer ScienceDr.FirstH.LastRoddy Science CenterFirstH.LastRoddy#millersville.edu872-3470Computer ScienceDr.FirstH.LastRoddy Science CenterFirst.Last#millersville.edu872-3724Computer ScienceMs.FirstA.GilbertLast Science CenterFirst.Last#millersville.edu871-2214Computer ScienceDr.FirstH.LastRoddy Science CenterFirst.Last#millersville.edu872-3666
There's no way you can use a xml parser for this file.
Instead you may try to use NSScanner to parse the text file. A couple of tutorials are listed here:
Parsing CSV Data
Writing a parser using NSScanner
without the xml tags, your file is as good as a plain text file...
rows separated by new line character....
and each line contains data separated by a dot (.) or something like that. figure out the pattern and parse it like you would parse a text file...

Plist contains the "&" character

I'm having a .plist file which has some values with the "&" sign, for example "M&I". When I save the file to the document folder and load it from there, I'm getting an empty dictionary. Any idea to how to fix this issue?
If you are directly modifying the XML file, you have to escape certain characters - & should be escaped using the XML entity &. If you use the editors, this should be done automatically for you.
If you use CDATA sections instead, you don't have to escape the characters.
If you insert the values when open plist file as property list it would do it automatically.

Parse .strings file with Python

I'm trying to write a small Python script to parse the .strings file in my iPhone application project and determine which keys might not be in use. I'm, also doing some string matching to filter out some of the results. This is where my problems start :). If I try something like
for file_line in strings_file:
if 'search_keyword' in file_line:
...
the search keyword will often not match, even though if I print every file line in the same for I seem to be reading the text correctly and my search keywords appear.
The problem is these .strings files are in some binary format. Does anyone know of a proper way to parse these files?
Use correct encoding to open the .strings-file and in your source code. According to documentation the encoding of your file could be utf-16.
# -*- coding: utf-8 -*-
import codecs
for line in codecs.open(u'your_file.strings', encoding='utf-16'):
if u'keyword' in line:
# process line
No experience with those .strings files, but here is the reason why you don't find matches:
strings_file.read()
returns a string with the full content of the file. Iterating over a string iterates over single characters, i.e. in your for loop, file_line isn't a line, it's always just one single character (a string of length 1), which obviously can't contain a multi-character search word.
It sounds like the stings file was saved as data. If python can't read it as is you can convert it to a plain text file in Objective-c.
Just: (1) read the strings file into a file with the proper encoding. (2) Convert to dictionary (3) write dictionary to another file.
So:
NSString *strings=[NSString stringWithContentsOfFile:filePath encoding:NSUTF16StringEncoding error:&error];
NSDictionary *dict=[strings propertyList];
[dict writeToFile:anotherFilePath atomically:NO];