Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-03-2009, 04:27 PM   #2 (permalink)
barrrt
Registered Member
 
Join Date: May 2009
Posts: 23
Default

Quote:
Originally Posted by soku View Post
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
barrrt is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,838
Threads: 89,210
Posts: 380,647
Top Poster: BrianSlick (7,129)
Welcome to our newest member, hariky007
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:04 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.