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