Re: Slice a .txt file in lines
Re: Slice a .txt file in lines
- Subject: Re: Slice a .txt file in lines
- From: Ben Lachman <email@hidden>
- Date: Tue, 20 Jun 2006 02:11:52 -0400
Check out NSString's text functions
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSString_Class/Reference/Reference.html#//apple_ref/doc/c_ref/
NSString>
particularly componentsSeparatedByString: is useful for splitting
files by line endings.
->Ben
On Jun 20, 2006, at 1:44 AM, Neto wrote:
Hi all,
Another newbie question...
I need to import a .txt file to my app.
Each line of the .txt must be assigned to a key of an array.
With my little knowledge and some Google, I managed to do this:
-(IBAction)importFileMethod:(id)sender
{
NSString *path = @"/Users/neto/Desktop/import.txt";
NSError *error;
NSString *stringFromFileAtPath = [[NSString alloc]
initWithContentsOfFile:path encoding:NSUTF8StringEncoding
error:&error];
NSString *cN, *sN; //(and so on - an NSString for each key)
MyArray *newItem = [[MyArray alloc] init];
cN=[stringFromFileAtPath substringWithRange: NSMakeRange(3,19)];
sN=[stringFromFileAtPath substringWithRange: NSMakeRange(20,25)];
[newItem setValue:cN forKey:@"keyOne"];
[newItem setValue:sN forKey:@"keyTwo"];
[arrayController addObject:newItem];
[newItem release];
}
The Problem: I can't use NSMakeRange(x,y) because in the
different .txt files the lengths will vary. So I need a method that
detects the beginning and the end of each line (each line ends with
an Enter)
Any ideas?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden