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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 09-08-2011, 04:03 PM   #1 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default writeToFile NSData question?

I was writing and reading files just fine on the simulator.. saving them to my computers HardDrive..

My code:
Code:
stringByAppendingPathComponent:@"/savedFile1.txt"];
myString = [[NSString alloc] initWithFormat:@"test"];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
When I switched over to the iPhone it wouldn't work so I assumed I had to change the path...

Here is the current code I'm using but I'm having no luck, the app freezes and quits when I try to save:
Code:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
path = [documentsDirectory stringByAppendingPathComponent:@"savedFile1.txt"];
myString = [[NSString alloc] initWithFormat:@"test"];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
And my .h declarations:
Code:
NSArray *paths;
NSString *documentsDirectory;
NSString *path;
NSString *myString;

What should I do?
(also are there any frameworks I need for this?)
__________________
APPS4LIFE
search for me in the app store.

Last edited by orange gold; 09-08-2011 at 04:11 PM.
orange gold is offline   Reply With Quote
Old 09-08-2011, 04:07 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

#1. When your app crashes, there should be some info in the debug console. Can you post that please?

#2. The writeToFile: method has an error parameter. You can use it to get more specific info about when the writeToFile fails.
smithdale87 is offline   Reply With Quote
Old 09-08-2011, 04:07 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Why are you trying to initialize your myString variable with no data?
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 09-08-2011, 04:11 PM   #4 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

Sorry that was a copy and paste error..

Quote:
Originally Posted by MattW View Post
Why are you trying to initialize your myString variable with no data?
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-08-2011, 04:15 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Quote:
Originally Posted by orange gold View Post
Sorry that was a copy and paste error..
Then refer to smithdale's post.... also, put a breakpoint in that function; step through and see exactly which line is crashing your app.
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 09-08-2011, 04:21 PM   #6 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

Quote:
Originally Posted by MattW View Post
Then refer to smithdale's post.... also, put a breakpoint in that function; step through and see exactly which line is crashing your app.
unfortunately my NSLog is broken... when I got the new xCode it stopped working.. I can't display the errors with ease /:
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-08-2011, 04:24 PM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

How is it possible to break NSLog? Have you tried stepping through your code in the debugger then to see which line makes the app go boom?
smithdale87 is offline   Reply With Quote
Old 09-08-2011, 04:24 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Quote:
Originally Posted by orange gold View Post
unfortunately my NSLog is broken... when I got the new xCode it stopped working.. I can't display the errors with ease /:
I didn't say use NSLog.... I said use a breakpoint. They are invaluable for debugging.
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 09-08-2011, 04:25 PM   #9 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
How is it possible to break NSLog? Have you tried stepping through your code in the debugger then to see which line makes the app go boom?
But yes, good point! How do you break NSLog??!!
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 09-08-2011, 05:30 PM   #10 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by orange gold View Post
unfortunately my NSLog is broken... when I got the new xCode it stopped working.. I can't display the errors with ease /:
I bet it's not broken. I bet you can't find the debug console, since just about everything about Xcode 4 is different from Xcode 3, in a different place, works differently, etc.

In XCode 4 you have to show the debug area (Command 5), and then click the right cryptic little symbol at the top of the debug area:

__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-08-2011, 06:04 PM   #11 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

No, the phone doesn't return anything to my debugger.. it only works for the simulator. but yeah, xcode 4 does have alot of re-arranging... Does anyone know what path I need to use for writeToFile? ....


My 666th post
__________________
APPS4LIFE
search for me in the app store.

Last edited by orange gold; 09-08-2011 at 08:27 PM.
orange gold is offline   Reply With Quote
Old 09-08-2011, 07:23 PM   #12 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Quote:
Originally Posted by orange gold View Post
No, the phone doesn't return anything to my debugger.. it only works for the simulator. but yeah, xcode 4 does have alot of re-arranging... Does anyone know what path I need to use for writeToFile? ....
Put a breakpoint in the function. Step over each line. Check the variables, see what's going on.
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 09-08-2011, 07:38 PM   #13 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

The app freezes when I try to "writeToFile"
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-08-2011, 07:47 PM   #14 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Quote:
Originally Posted by orange gold View Post
The app freezes when I try to "writeToFile"
But what information do you get from the debugger when you step over that line? And, before you step over that line, are the values in your variables what you expect them to be?
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 09-08-2011, 07:51 PM   #15 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

They are the correct values... my debugger isn't working ahhhh this is annoying ahhaha! Maybe I just need to find some source code for writeToFie and copy it...
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-08-2011, 07:55 PM   #16 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Quote:
Originally Posted by orange gold View Post
They are the correct values... my debugger isn't working ahhhh this is annoying ahhaha! Maybe I just need to find some source code for writeToFie and copy it...
Wait a minute... NSLog is 'broken' and your debugger 'isn't working'? This sounds very strange. Are you sure you're running in XCode? And if you are, do you actually know how to use it?
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 09-08-2011, 08:26 PM   #17 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

Haha, yes I am (:0
My phone doesn't send anything back to the computer because when I launch the app ever since I updated to the new xcode it loads it on but never launches it so I have to manually open the app on the phone and by then it is not connected to xcode's debugger anymore...

On another note I followed this example code: http://www.cocoanetics.com/2009/03/g...tandard-paths/

It no longer freezes when I write to file but when I try to load the file nothing comes up which makes me think that it isnt actually writing or my loading method is wrong..

Here is how I load an html file I "wroteTo" using the writeToFile code:
Code:
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"testHTMLfile1" ofType:@"html"]isDirectory:NO]]];
This worked on the simulator when I was just writing to the hard drive..
Now that I am writing to a file in the resources folder using my "paths" variable it no longer works..
But I still haven't confirmed wether the loading isnt working or the writing isnt working or neither are, all I know is the writing doesnt freeze the app anymore. (by freeze I mean exit sigabrt)


/:
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-08-2011, 10:50 PM   #18 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

AHHHH!!! you can't writeToFiles in your resources folder! you have to write to files in your directory! A person answered on another forum and gave example code that worked perfectly!
YAY!
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 09-09-2011, 12:17 AM   #19 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Please heed this advice: find out why your debugger no longer works and fix it. Without a debugger you are going to have a massively uphill struggle while developing any type of software.
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Reply

Bookmarks

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: 383
9 members and 374 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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