Showing posts with label iPhone File Reading. Show all posts
Showing posts with label iPhone File Reading. Show all posts

Friday, July 29, 2011

Core Data Tutorial: How To Use NSFetchedResultsController

So far, we're at exactly the same point we were using the SQLite3 method. However, we didn't have to write nearly as much code (notice the absence of a FailedBankDatabase class constructing raw SQL statements), and adding other functionality such as insert/delete operations would be much simpler.
However, there's one notable thing that we could add pretty easily with Core Data that could give us huge benefits to performance: use NSFetchedResultsController.
Right now we're loading all of the FailedBankInfo objects from the database into memory at once. That might be fine for this app, but the more data we have the slower this will be, and could have a detrimental impact to the user.
Ideally we'd like to load only a subset of the rows, based on what the user is currently looking at in the table view. Luckily, Apple has made this easy for us by providing a great utility class called NSFetchedResultsController.
So, start by opening up FailedBanksListViewController.h, removing out our old NSArray of failedBankInfos, and adding a new NSFetchedResultsController instead:

Saturday, April 9, 2011

iPhone Tutorials

iPhone Tutorials

This site contains a ton of fun tutorials – so many that they were becoming hard to find! So I put together this little page to help everyone quickly find the tutorial they’re looking for. Hope you enjoy! :]

Beginning iPhone Programming

iPhone programming is like a ladybug - fun and only a little scary!
If you’re completely new to iPhone programming, start here! First there’s a tutorial series that will walk you through the process of creating an iPhone app from start to finish – using the most common APIs that almost every app uses. Next there’s a tutorial about memory management – the area where beginners most often get confused about!

Game Programming with Cocos2D, Box2D, and Chipmunk

Ninjas Going Pew-Pew!
If you want to make games on the iPhone, the easiest way by far is to use the Cocos2D iPhone framework and the physics libraries that come with it – Box2D and Chipmunk. These tutorials will help get you started by showing you how to make some simple games and solve common problems.
In addition to these tutorials, you might be interested in the Cocos2D book Rod Strougo and I are working on.

Game Programming and Development

Tomato-San says: w00t, it's done!
While we’re on the topic of game programming, here are a few posts with some tips and tricks for game developers.

Saving and Loading Data

Core Data Failed Banks Model Diagram
Almost every app needs to save and load data on the iPhone – and there are many different ways to do so. In these tutorials, you can get hands-on experience with many of the most common methods.

Graphics and Animation

Welcome to Core Graphics 101!
In order to be successful on the App Store these days, your app needs to look good. Here are a few tutorials that you can use to up the quality level of your apps, and your gain mad skills with graphics and animation programming.


Screenshot from BasicSounds sample project
When I first started iOS programming, I knew a WAV file played sounds and that was about it. These posts explain a lot about audio files and formats, and explain how you can play audio in your apps.

iPad Development

What it will look like when we're done!
If you know how to program for the iPhone, it’s a simple matter to program for the iPad as well! These tutorials walk you through some of the differences and help get you started with some of the new APIs available on the iPad.

3rd Party Libraries

I have a soft spot for malteses!
There are a lot of third party APIs and SDKs you might want to include in your apps. These tutorials cover a few of them and show you how to get started.

Training and Announcements

One day class introducing iOS programming for beginners!
From time to time I announce upcoming training, books, and other types of announcements from this site. Here’s the news so far!


1) Integrate iAd 2) ??? 3) PROFIT!
There’s always something that doesn’t fit anywhere else! Here’s a hodgepodge of other posts and tutorials you may find interesting.

Tuesday, October 5, 2010

How To Read a File From Your Application Bundle

First you need to add your file to the Resources folder of your Xcode project. Then you can access the file like this (assuming the file is called MyFile.txt):

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
  2. NSData *myData = [NSData dataWithContentsOfFile:filePath];
  3. if (myData) {
  4. // do something useful
  5. }

Here’s a complete example reading a help text file into a UIWebView.

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];
  2. NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
  3. if (htmlData) {
  4. [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
  5. }

If you want to read the file into a string, which you can then display in a UITextView, for example, then do this:

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
  2. if (filePath) {
  3. NSString *myText = [NSString stringWithContentsOfFile:filePath];
  4. if (myText) {
  5. textView.text= myText;
  6. }
  7. }

Or simply
use this..

NSString *fileContents = [NSString stringWithContentsOfFile:@"myfile.txt"];
NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];

Tuesday, June 1, 2010

iPhone Tutorial - iPhone SDK Load Text File Into NSString

1NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];
2NSString *fileText = [NSString stringWithContentsOfFile:path];

iPhone Tutorial - iPhone SDK Save Application State

NSUserDefaults is your friend when you need to save a small amount of data. Here is some sample code for saving various data types.

01NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
03//Setting defaults
04[defaults setInteger:1 forKey:@"myint"];
05[defaults setObject:@"my string" forKey:@"mystring"];
06[defaults setFloat:100.23 forKey:@"myfloat"];
08//Getting defaults
09int myint = [defaults intForKey:@"myint"];
10NSString *mystring = [defaults intForKey:@"mystring"];
11float myfloat = [defaults floatForKey:@"myfloat"];