IPhone SDK development: Reading local text files

Source: Internet
Author: User

Step 1: Create a project based on a single view and name it TextfileRead.

Step 2: Open the ViewController. h file and write it as follows:

[Cpp]
# Import <UIKit/UIKit. h>
 
@ Interface ViewController: UIViewController {


NSArray * contents;
}
@ Property (nonatomic, retain) NSArray * contents;
 
@ End
# Import <UIKit/UIKit. h>

@ Interface ViewController: UIViewController {
 

NSArray * contents;
}
@ Property (nonatomic, retain) NSArray * contents;

@ End
Step 3: Open the ViewController. m file and write it as follows:

[Cpp]
# Import "ViewController. h"
 
@ Implementation ViewController
@ Synthesize contents;
 
-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
 
# Pragma mark-View lifecycle
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSError * error;
NSString * textContents = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "book_list" ofType: @ "rtf"] encoding: NSUTF8StringEncoding error: & error];

If (textContents = nil)
{
NSLog (@ "Error reading text file. % @", [error localizedFailureReason]);
}
Contents = [textContents componentsSeparatedByString: @ ""];
NSLog (@ "Number of lines in the file: % d", [contents count]);
NSInteger idx;
NSLog (@ "% d", [contents count]);
For (idx = 0; idx <[contents count]; idx ++)
{
NSString * current = [contents objectAtIndex: idx];
NSLog (@ "% @", current );

}

}
# Import "ViewController. h"

@ Implementation ViewController
@ Synthesize contents;

-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

# Pragma mark-View lifecycle

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSError * error;
NSString * textContents = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "book_list" ofType: @ "rtf"] encoding: NSUTF8StringEncoding error: & error];

If (textContents = nil)
{
NSLog (@ "Error reading text file. % @", [error localizedFailureReason]);
}
Contents = [textContents componentsSeparatedByString: @ ""];
NSLog (@ "Number of lines in the file: % d", [contents count]);
NSInteger idx;
NSLog (@ "% d", [contents count]);
For (idx = 0; idx <[contents count]; idx ++)
{
NSString * current = [contents objectAtIndex: idx];
NSLog (@ "% @", current );

}
 
}

Step 4: Run. See the following figure for details:

 

 

 

From Evolution


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.