 |
 |
|
 |
07-03-2009, 03:41 PM
|
#1 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 44
|
Parsing a text file
I have a text file with 3 words in it, in this format:
Quote:
Word1 = Boat
Word2 = Car
Word3 = Plane
|
But I want to store them into the variables varword1, varword2 and varword3 in my application. Apparently I need to parse them but I have no idea if theres an easier way or if not then how to do it or where to even begin.
Any help would be greatly appreciated.
|
|
|
07-03-2009, 04:27 PM
|
#2 (permalink)
|
|
New Member
Join Date: May 2009
Posts: 18
|
Quote:
Originally Posted by soku
I have a text file with 3 words in it, in this format:
But I want to store them into the variables varword1, varword2 and varword3 in my application. Apparently I need to parse them but I have no idea if theres an easier way or if not then how to do it or where to even begin.
Any help would be greatly appreciated.
|
Any particular reason why you want to use the same variable name with different numbers appended to them, rather than one array with "Boat", "Car","Plane" in it?
In any case, I suggest you take a look at documentation for NSString. It has two useful methods:
1. stringWithContentsOfFile:encoding:error: - this will let you load the file into a string
2. componentsSeparatedByString: - this will let you split the string from the above operation into array elements. You would use new line ("\n") to split the string. Then you can create a loop for each array element, and call this method on each element, using " = " as argument. This will give you an array (let's call it myArray) where the first element (at index 0) is what is before " = ", second element is what is after " = ". You could then append the second element to a mutable array.
If you still want to have varword1, varword2, etc, you can do that too, but they have to be defined as member variables in your class, so you have to know how many variable = value pairs you will have in your file.
To do this you would use something call key-value coding. In order for this to work, make sure to define your varword variables as properties, then in the loop in step #2 above do something like this:
[self setValue:[myArray objectAtIndex:1] forKey:[myArray objectAtIndex:0]];
To learn more about key-value coding, check out this:
Key-Value Coding Programming Guide: Introduction
Bart
|
|
|
07-03-2009, 04:32 PM
|
#3 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 44
|
Reading into an Array is fine. Is there not an easy way of doing it? So like
Quote:
Array[1] = Line 1 of text file
Array[2] = Line 2 of text file etc.
|
Cheers.
|
|
|
07-03-2009, 04:43 PM
|
#4 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,449
|
Quote:
Originally Posted by soku
Reading into an Array is fine. Is there not an easy way of doing it? So like
Cheers.
|
If you're willing to change the format of your input file, there's a method called arrayWithContentsOfFile that will load the entire array for you. The text file must be in "plist" format.
NSArray Class Reference
__________________
|
|
|
07-03-2009, 05:05 PM
|
#5 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 44
|
Quote:
Originally Posted by smasher
If you're willing to change the format of your input file, there's a method called arrayWithContentsOfFile that will load the entire array for you. The text file must be in "plist" format.
NSArray Class Reference
|
Wow! Thats great. Presumably I can load it from an external resource?
Last edited by soku; 07-04-2009 at 05:51 AM.
|
|
|
07-04-2009, 05:52 AM
|
#6 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 44
|
Thanks mate, I've played around with it and its all working perfectly. Cheers!
Last edited by soku; 07-04-2009 at 06:04 AM.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 522 |
| 57 members and 465 guests |
| alexcrick209, atsd, AXP, bbc z, BostonMerlin, brianmethod, cakeisalie, cornishGeeza, davek, dbarrett, dda, deden, DevJustin, Dorald, embedded, Gamer211, ggalante, gonk, harrytheshark, iPhoneDevelopment, IphoneSdk, Jeremy1026, jharrah, kjelleinar, krowczyk, Link, markbuchanan, MarkC, masc2279, mebarron, naomipunkclan, nibby, Noise, pbcbvba, pieter78, raheel, Sckah, Sesa, SimonK, sjdev, Snappy, soulless, StefanL, Tambourin, the1nz4ne, treazer, tturk, Type25, virvalid, Vivek Nirkhe, Vonswanko, wassupdoc, winklllll, x2on |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,507
Threads: 35,791
Posts: 156,790
Top Poster: smasher (2,449)
|
| Welcome to our newest member, soulless |
|