p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional iPhone and iPad Database Application Programming (http://p2p.wrox.com/forumdisplay.php?f=603)
-   -   Chapter 1 - Help! (http://p2p.wrox.com/showthread.php?t=83572)

tecky10 May 7th, 2011 09:18 PM

Chapter 1 - Help!
 
Went through example in chapter 1 fine. However, on pg. 17, book says you can modify DataModel to use a text file as a datasource. On page 18 there is code for reading a text file. However, the book does not explain how to change the previous code in order to actually get this to work. Does anyone know where to input the code on page 18? (e.g. what file?, what location in the file?, also - shouldn't this replace other code-e.g. myData = [[NSArray alloc] initWithObjects:@"Albert", @"Bill"...?) Thanks in advance for any help!

thepianoguy May 8th, 2011 08:05 AM

You are replacing the initialization code in the DataModel.m file. This is a way to do it. Comment out or remove the existing allocation and assignment to myData and assign to myData the contents of the textfile.

Code:

-(id)init
{
    if (self = [super init])
    {
    /*  // Initialization code
        myData = [[NSArray alloc] initWithObjects:@"Albert", @"Bill", @"Chuck",
                  @"Dave", @"Ethan",@"Franny", @"George", @"Holly", @"Inez",
                  nil];
          */
                NSError *error;
                NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
                                                                                                                                                pathForResource:@"myTextFile" ofType:@"txt"]
                                                                                                                          encoding:NSUTF8StringEncoding
                                                                                                                                  error:&error];
                if (textFileContents == nil) {
                        NSLog(@"Error reading text file. %@", [error localizedFailureReason]);
                }
               
                myData = [textFileContents componentsSeparatedByString:@"\n"];
       
    }
    return self;
}

This assumes that you have created the .txt file (Create it in TextEdit e.g., and save as plain text not .rtf) and added (copied the file) to the project.

Bob

tecky10 May 9th, 2011 03:06 AM

Thanks a million!
 
Yea, it worked! Thanks so much for the clear instructions along with the code. I really appreciate it! :-)

Nosirb May 16th, 2011 01:43 PM

Where did you put the file?
 
My project compiles without error but there is no output, the lines are blank! I need to know where do you put the myTextFile.txt file? It is not clear in the instructions where to put this file. I'm not sure Xcode is reading the file

thepianoguy May 16th, 2011 01:59 PM

You should add the .txt file to the project. There are 3 ways to do this:

1.Drag and drop it into the Project Navigator (Xcode 4) or the Groups & Files Pane (Xcode 3.x).
2. Choose File>Add Files to… (Xcode 4) or Project>Add to Project (Xcode 3.x), navigate to your file and select it.
3. Step 2 is also available as a context menu in the Project Navigator (Xcode 4) and the Groups & Files pane (Xcode 3.x)

In all cases a dialog sheet will present. Make sure the Copy file(s) into the destination… is checked, and Click Add

Hope this makes sense.

Bob

tecky10 May 16th, 2011 02:38 PM

File location for .txt file
 
<<My project compiles without error but there is no output, the lines are blank! I need to know where do you put the myTextFile.txt file? It is not clear in the instructions where to put this file. I'm not sure Xcode is reading the file>>

I just put the .txt file in the main 'groups & files' location. It works there, although I suspect it should really be in the 'resources' folder. But, like I said, it did work there. You can tell that Xcode is reading the file if you try 'build and run'. If it reads the file, you'll see your text from the .txt file appear in the simulator.

Hope that helps.

Best wishes.

Nosirb May 16th, 2011 04:13 PM

Thanks! it Worked!!!!

thepianoguy May 16th, 2011 10:49 PM

Quote:

I just put the .txt file in the main 'groups & files' location. It works there, although I suspect it should really be in the 'resources' folder
.

The groups in the Groups & Files Pane are purely organizational and have no impact on how the program compiles. The folders (groups) are present in the Xcode interface but not in the actual project directory. So files can actually be arranged anyway you wish in the Groups & Files Pane. (Obviously, it is a good idea to set up a structure that makes sense for a more efficient workspace)

Bob

tecky10 May 17th, 2011 12:34 AM

Thanks for info.
 
<<The groups in the Groups & Files Pane are purely organizational and have no impact on how the program compiles. The folders (groups) are present in the Xcode interface but not in the actual project directory. So files can actually be arranged anyway you wish in the Groups & Files Pane. (Obviously, it is a good idea to set up a structure that makes sense for a more efficient workspace)>>

Thanks for this info. This is good to know!

bubba hawk January 31st, 2012 05:42 PM

thanks
 
This worked for me too - thanks.


All times are GMT -4. The time now is 12:33 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.