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 Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2008, 01:24 PM   #1 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default NSKeyedArchivers and NSKeyedUnarchivers

Here's a link to a screen-cast that I just finished uploading to vimeo.com. It details how to use NSKeyedArchivers and NSKeyedUnarchivers to load and save data. Although I use the iPhone SDK in the tutorial, the same principals can be used in your projects for the Mac OS as well.

It may still be converting, but it should be ready sometime on August 3. Also, the compression I used when recording the screen-cast did terrible things to the audio in a few parts. However, it doesn't obscure any of the info. I'm going to play around with the settings and see if I can iron out the kinks for the next tutorial I do.

Chris asked me to come back and embed the videos in these pages. But I didn't realize until I did it that the embedded versions weren't HiDef. However, you can click the link below the video to be taken to vimeo.com and you can view it in HiDef from there, or you can click the download link and download a HiDef version to your computer. All the videos except for the multiple-xib one are 75-ish meg in size.


iPhone Tutorial - Archivers, unarchivers, and serialization from Nick Myers on Vimeo.
Download the HiDef version to your computer

Last edited by myersn024; 09-04-2008 at 09:53 AM.
myersn024 is offline   Reply With Quote
Old 08-02-2008, 03:49 PM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Location: Germany
Posts: 154
ChriB is an unknown quantity at this point
Default

Great tutorial. Awesome screen quality, sound is ok.. Hope your next tutorial will have better sound :P
That will come in handy very soon.
Thank you very much!
ChriB is offline   Reply With Quote
Old 08-02-2008, 03:53 PM   #3 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

Yeah, I don't know if it was the compression or if it was the headset that I used. For my first tutorial, I used a USB microphone and recorded with no compression. Then I converted it to something else. For this tutorial, I recorded with compression to start out with, and I also changed to a bluetooth headset. I'm not sure which one caused the problem....it could be a combination of both.
myersn024 is offline   Reply With Quote
Old 08-02-2008, 06:23 PM   #4 (permalink)
Administrator
 
Chris Stewart's Avatar
 
Join Date: Mar 2008
Location: Richmond, VA
Age: 29
Posts: 743
Chris Stewart is an unknown quantity at this point
Default

Thank you for posting the first tutorial! It is greatly appreciated.
__________________
Founder, Locomo Labs, LLC
SocialBlast -- iPhone/iPad -- Update Twitter/Facebook/LinkedIn/Foursquare

Previous owner, and original founder, of iPhone Dev SDK
Chris Stewart is offline   Reply With Quote
Old 08-03-2008, 01:33 PM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 19
Garrett is on a distinguished road
Default

This got me started! I just can't figure out how to add entries for each row.

Like

- First
-- Entry
-- Entry
- Second
-- Entry
-- Entry
- Third
-- Entry
-- Entry
Garrett is offline   Reply With Quote
Old 08-03-2008, 02:12 PM   #6 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

Quote:
Originally Posted by Garrett View Post
This got me started! I just can't figure out how to add entries for each row.

Like

- First
-- Entry
-- Entry
- Second
-- Entry
-- Entry
- Third
-- Entry
-- Entry
You mean for a table? I didn't go over that.
myersn024 is offline   Reply With Quote
Old 08-03-2008, 02:15 PM   #7 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 19
Garrett is on a distinguished road
Default

Quote:
Originally Posted by myersn024 View Post
You mean for a table? I didn't go over that.
I understand that, but you showed how to insert rows (which the my table reads from). So I was just curious how you could setup the data or array to allow for sections with rows inside.
Garrett is offline   Reply With Quote
Old 08-10-2008, 10:35 PM   #8 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London
Posts: 21
hypercrypt is on a distinguished road
Default

Wow, thank you for an amazing tutorial. Just what I was looking for.

Only question, what are the benefits of
Code:
firstName = [[coder decodeObjectForKey:@"firstName"] retain];
over using the setter
Code:
self.firstName = [coder decodeObjectForKey:@"firstName"];
or
[self setFirstName:[coder decodeObjectForKey:@"firstName"]];
That way you cannot forget the retain (it is built into the setter) and it is easier to read.

Thanks again!
hypercrypt is offline   Reply With Quote
Old 08-11-2008, 08:02 AM   #9 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

That's how the Apple documentation that I got the info from recommended it done. However, if your way works, then it's probably ok.
myersn024 is offline   Reply With Quote
Old 08-12-2008, 10:45 AM   #10 (permalink)
1026 Development
 
Join Date: Jul 2008
Age: 23
Posts: 1,094
Jeremy1026 is on a distinguished road
Send a message via AIM to Jeremy1026
Default

I followed this tutorial all the way and ended up with about 90+ errors. I think I need to clear the app file and try it again. So much work, for such a simple function.
Jeremy1026 is offline   Reply With Quote
Old 08-12-2008, 10:54 AM   #11 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

Download the source from the link under the video on vimeo.com and take a look at it.
myersn024 is offline   Reply With Quote
Old 08-12-2008, 11:32 PM   #12 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 15
bjpate is on a distinguished road
Default

You are a saint for adding this, thanks so much. I found this site two days ago and I can't tell you how helpful it has been in learning.
bjpate is offline   Reply With Quote
Old 08-23-2008, 01:56 AM   #13 (permalink)
Some guy
 
MattCairns's Avatar
 
Join Date: Jul 2008
Location: Canada
Posts: 103
MattCairns is on a distinguished road
Send a message via AIM to MattCairns
Default

This was a great tutorial! Thanks, I learned a lot!
MattCairns is offline   Reply With Quote
Old 09-20-2008, 03:17 AM   #14 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 160
Vortec4800 is on a distinguished road
Default

Quick question, I noticed you never released some of the temporary objects you used like the NSMutableData or the temp array when you were done with them, only the decoder. Was this on purpose, or just a quick and dirty tutorial? I'm still trying to wrap my head around when I should be releasing objects, autoreleasing, when things need to be retained or don't, and so on. I just finished my first app and it just hemorrhages memory like you wouldn't believe, so it's something I really need to learn.
Vortec4800 is offline   Reply With Quote
Old 09-20-2008, 09:37 AM   #15 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

the NSData and NSMutableData objects are autorelease variables, as is the temp array.
myersn024 is offline   Reply With Quote
Old 09-20-2008, 06:20 PM   #16 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 160
Vortec4800 is on a distinguished road
Default

Quote:
Originally Posted by myersn024 View Post
the NSData and NSMutableData objects are autorelease variables, as is the temp array.
You'll have to forgive me, but why/how are they autorelease variables if you don't declare them as autorelease when you set them up? How do you know if something is an autorelease like those or is something that you'll need to release manually later? Will an error occur if you try to manually release an autorelease object?
Vortec4800 is offline   Reply With Quote
Old 09-21-2008, 07:36 PM   #17 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

Normally, anything that you don't specifically alloc and init are autorelease variables by default. I don't pretend to understand it fully, but after working with this stuff for a while I just kind of know when to specifically retain or release something.
myersn024 is offline   Reply With Quote
Old 09-21-2008, 10:05 PM   #18 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: Portland, Oregon
Posts: 46
higgins is on a distinguished road
Default

Hey myersn024,

Awesome tutorial! I've got my app, well, "sorta" working as a result. I'm using your code to set an in-app preference: a float value that controls the timing of some sound playback. (My app plays three sounds, and varies the timing between them based on a UISlider that sets the float value -- the preference.)

My app builds and runs okay on the simulator, and I can set the pref and read it back, and it does seem to actually affect sound playback as I want. So that's cool.

However, after I quit the app and run it again, it crashes out with errors ("*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFType floatValue]: unrecognized selector sent to instance 0x52d510'")

If I reset the simulator and build & go again, things are fine for one launch.

So I'm guessing that what I'm actually writing to the data file (the preference array) is somehow corrupt...thus when the app parses it back in from disk (as it does after a launch if the preference.dat file exists), something goes horribly wrong.

SO HERE'S MY QUESTION: do you know of a way to examine that preference.dat file on either the simulator or a provisioned device? (My preference.dat is the same thing as your person.dat file in your example.) I feel like if I could look at the contents of that file, I'd at least have a hint.

Thanks again for the great tutorial!
higgins is offline   Reply With Quote
Old 09-21-2008, 10:55 PM   #19 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: Portland, Oregon
Posts: 46
higgins is on a distinguished road
Default

Quote:
Originally Posted by higgins View Post
Hey myersn024,

SO HERE'S MY QUESTION: do you know of a way to examine that preference.dat file on either the simulator or a provisioned device? (My preference.dat is the same thing as your person.dat file in your example.) I feel like if I could look at the contents of that file, I'd at least have a hint.
...Okay, and I managed to find the answer in another thread. These files live in the user's Library/Application Support/iPhone Simulator/User/Applications/UUID/Documents folder.
higgins is offline   Reply With Quote
Old 09-22-2008, 09:32 AM   #20 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

You can get the data file from the documents directory, but it isn't really human readable. From the looks of the error message, you're trying to do something with your float value that it can't do. Since the error message is happening when you launch the program, it sounds like you've got something wrong in either your load function or in your initWithCoder function.

My first question is do you get any warning when compiling? Something like "someInstanceVariable may not respond to selector ....."?
myersn024 is offline   Reply With Quote
Old 09-22-2008, 04:40 PM   #21 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: Portland, Oregon
Posts: 46
higgins is on a distinguished road
Default

Okay, so your response was the key that helped me find the error, though it's hard for me to properly explain what I fixed, as it was a several-hour festival of fiddling, until suddenly all at once things seemed to work. Which is cool but not entirely confidence inspiring. Anyway, yes, I think part of the problem I had was that I was encoding an object into the archived/unarchived key (even though it was "just" a float, I was treating it as an object under the theory that someday I'd be encoding something other than a float).

Anyway, when I'd read that object back out and try to do stuff with it, I was inconsistent in my code -- sometimes I passed the object a 'floatValue' to cast it to a float, and sometimes I didn't. Depending on the context, sometimes this worked and sometimes it didn't. I had several warnings like you said, where the compiler was saying I was using potentially mismatching types (for example when I'd try to set up an NSNumber using the contents of the key and an initWithFloat). Because it halfway worked, I ignored those. Turns out they were actually a problem.

Ultimately what seems to have helped is simply being really consistent with the floatValue thing. For example, this works:

Code:
float n;
n = [[[mainDelegate.preferencesArray objectAtIndex:0] myPreference] floatValue];
Back to the fray.

(BTW, I now have a fully working app that launches, builds a default pref file if the pref file is not found on disk (or reads the pref file from disk), there's a UISlider to set the pref, and the pref is written to disk upon app exit. This is lightyears from where I was yesterday afternoon!)

Last edited by higgins; 09-22-2008 at 04:49 PM.
higgins is offline   Reply With Quote
Old 09-22-2008, 10:30 PM   #22 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
myersn024 is an unknown quantity at this point
Default

If you're like me, you came to Objective-C from a C/C++ background, and all us C/C++ programmers know that any warnings while compiling a program usually mean absolutely nothing. That mindset is one that needs to be forgotten completely when working with Objective-C. 99 times out of 100 a compile-time warning in Objective-C means a run-time crash when that line of code is reached. Unless you know for sure that a warning can be ignored, you should stop the program and fix them immediately. Otherwise you're in for some headaches.
myersn024 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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 465
16 members and 449 guests
alexeir, David-T, Dj_kades, Elad, foslock, HemiMG, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, Pauluz85, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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