Xcode How To Read Raw Information From Pipe-Delimited Text File

Open Xcode too produce a novel Single View Application. For production name, purpose ReadPipeDelimitedTextfield too and thus produce amount out the Organization Name too Company Identifier amongst your customary values. Select iPhone for Devices.


I bring prepared a text file to move used inwards this project, it tin move download inwards this link. Download the text file too add together it within the project. 

Once the file has been added, become within the "ViewController.m" too add together the next lines of code within the viewDidLoad:

    NSError *error;     NSString* filePath = [[NSBundle mainBundle] pathForResource:@"US States" ofType:@"txt"];     NSString *USStates = [NSString stringWithContentsOfFile:filePath                                                           encoding:NSUTF8StringEncoding error:&error];     NSString *statesStripped = [USStates stringByReplacingOccurrencesOfString:@"\r" withString:@""];      NSArray *rows = [statesStripped componentsSeparatedByString:@"\n"];          NSArray *components;          for(int i = 0; i < [rows count]; i++){         if(i == 0 || [[rows objectAtIndex:i] isEqualToString:@""]){             continue;         }                  components = [[rows objectAtIndex:i] componentsSeparatedByString:@"|"];                  NSLog(@"State: %@  Abbr: %@", [components objectAtIndex:0], [components objectAtIndex:1]);     } 

Notice how the value of "pathForResource" is equivalent to the elevate of the text file.

Build too Run the project, within the console yous should run into the contents of the text files which are immediately separated.




You tin download the rootage code of the ReadPipeDelimitedTextfield at my repository on bitbucket.
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.