Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-22-2010, 10:46 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 11
Question Simple App with problem: Read a random string from text file and put in UILabel

Hey guys.

New enough to this Iphone SDK coding and i have been trying to just create a simple app to see how the whole system works.

I have a main viewing page that has a button that when clicked displays a quote which should be taken randomly from .txt file in my resources folder.

I just am at a lose end as to why my array won't fill up with any data strings.
I have between 1 and 50 sentences.

Any help would be greatly appreciated

In my .h file i have the following:
Code:
@interface Quote_V_0ViewController : UIViewController {
	IBOutlet UILabel *quoteLabel;
}
-(IBAction)quote;
@end
in my .m file i have the following:
Code:
- (IBAction)quote:(id)sender {

      NSString *quotesFile = [[NSBundle mainBundle] pathForResource:@"quotes" ofType:@"txt"];
	NSString *fileContents = [NSString stringWithContentsOfFile:quotesFile];
	NSArray *quoteArray = [fileContents componentsSeparatedByString::@"\n"];
	NSString *quoteToDisplay;
	quoteToDisplay = [quoteArray objectAtIndex: 0];
	quoteLabel.text = quoteToDisplay;
As i say i'm new to this stuff and figure from having done some coding before that this should be possible without being overly complex. I'm just not sure how to do it in Xcode.
superstec is offline   Reply With Quote
Old 01-22-2010, 11:16 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Code:
NSArray *quoteArray = [fileContents componentsSeparatedByString::@"\n"];
Just one.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 01-22-2010, 11:19 PM   #3 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Maybe this one too:

Code:
@interface Quote_V_0ViewController : UIViewController {
	IBOutlet UILabel *quoteLabel;
}
-(IBAction)quote:(id)sender;
@end
rocotilos is offline   Reply With Quote
Old 01-22-2010, 11:24 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

^^^ I don't believe that's required, but certainly couldn't hurt.


Although the larger point raised is verifying that the method is actually being called.


Edit: Ah yes, whatever is in .h needs to match .m. That matters.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 01-23-2010, 07:44 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 11
Default

Hey guys, Thanks a million for the quick replies. I was clearly bleary eyed at 4am last night.
Thanks for pointing out those 2 mistakes. One of those things the longer you look at it the less you notice!

One warning appears when i run it now, saying that the stringWithContentsOfFile is deprecated

I know it refers to
Code:
	NSString *fileContents = [NSString stringWithContentsOfFile:quotesFile];
I have tried to fix it as recomended by the following line from other users.
Code:
[NSString stringWithContentsOfFile:fullpath encoding:NSUTF8StringEncoding error:nil];
This however then doesn't actually fill the array with the data.

Can i just run the program with the warning without a problem or is there a bigger picture?
On a side not can you submit your app to apple with this or any warnings?

Again Help is greatly appreciated
superstec is offline   Reply With Quote
Old 01-23-2010, 11:39 AM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Whenever you are informed of a deprecated method, search the documentation for that method and see what it tells you. In this particular case, I didn't find the deprecated notice for this one, but did find these methods:

stringWithContentsOfFile:encoding:error:
stringWithContentsOfFile:usedEncoding:error:

For the first one, the documentation states: If the file can’t be opened or there is an encoding error, returns nil.


There is nothing about a warning that will automatically stop you from submitting to the store. HOWEVER, there are a variety of reasons that you should address all warnings and errors before submitting your app, including:
1) Warning indicates that you are using a private method, which these days is a cause for rejection
2) Warning indicates an incorrect method name, which will crash if that portion of code is run
3) Deprecated warning should work right now, but may stop working at some point in the future. We don't yet know how aggressive Apple will be with removing stuff from the iPhone OS.

There are a variety of harmless warnings, too, but in general, fix them all.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 01-23-2010, 08:43 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 41
Posts: 1,184
Default

Quote:
I have tried to fix it as recomended by the following line from other users.
Code:
[NSString stringWithContentsOfFile:fullpath encoding:NSUTF8StringEncoding error:nil];
This however then doesn't actually fill the array with the data.
It should work... unless, of course, your text file isn't UTF8. Maybe it's UTF16?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 01-27-2010, 10:05 AM   #8 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 11
Default

Thanks again guys for the feedback.
Back from holidays and had a look into your suggestions. The file was UTF-16. Changing this and placing the other code in has worked.

Really appreciate Brainster about the details on loading apps to the App Store. NO warnings is good news now though
superstec is offline   Reply With Quote
Old 02-03-2010, 10:05 AM   #9 (permalink)
iPhoneDev
 
Join Date: Dec 2009
Posts: 33
Send a message via Yahoo to sivasankarp
Default

Quote:
Originally Posted by BrianSlick View Post
^^^ I don't believe that's required, but certainly couldn't hurt.


Although the larger point raised is verifying that the method is actually being called.


Edit: Ah yes, whatever is in .h needs to match .m. That matters.
i have made the view based application,
in the 1st view i have browse button to browse the image,and i even view that image to the same browse view,
when the user selects the other button it will take me to the next view where i want to show the same image of the browsed one,and have to add the texture effects to this image
may i know the steps or any sample code to this issues im facing...


Thanks in advance
sivasankarp is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray, nsstring, strings from text file

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 273
20 members and 253 guests
ADY, Bertrand21, Dani77, HemiMG, iDifferent, IphoneSdk, jakerocheleau, JasonR, jimbo, macquitzon216, MACralik, mer10, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84, Sunny46
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0