Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2009, 02:41 PM   #1 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 44
Default 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.
soku is offline   Reply With Quote
Old 07-03-2009, 03:27 PM   #2 (permalink)
New Member
 
Join Date: May 2009
Posts: 18
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
Old 07-03-2009, 03:32 PM   #3 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 44
Default

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.
soku is offline   Reply With Quote
Old 07-03-2009, 03:43 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,577
Default

Quote:
Originally Posted by soku View Post
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
__________________
smasher is offline   Reply With Quote
Old 07-03-2009, 04:05 PM   #5 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 44
Default

Quote:
Originally Posted by smasher View Post
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 04:51 AM.
soku is offline   Reply With Quote
Old 07-04-2009, 04:52 AM   #6 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 44
Default

Thanks mate, I've played around with it and its all working perfectly. Cheers!

Last edited by soku; 07-04-2009 at 05:04 AM.
soku is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Online Users: 388
22 members and 366 guests
aniuco, appbox, DonomaGames, Hololont, houssem, jamesrww, jeff974, KaBoom, lepetitapps, linuxgood, Locker, mac514, maddams, Momeks, Nir Tager, nub98, ovycom, shweta13, spinyanteater, thadre, yohliying, yunas
Most users ever online was 779, 05-11-2009 at 09:55 AM.
» Stats
Members: 24,342
Threads: 39,136
Posts: 171,587
Top Poster: smasher (2,577)
Welcome to our newest member, gabrieljose
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0