Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 18, 2011, 03:09 PM   #1
nashyo
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
Encoding vs Array and Dictionary

I'm currently being introduced slowly to encoding in Objective C, and I have a fundamental question to ask.

Why would an Object orientated programmer want to encode an object to the file system, instead of create an array or dictionary of objects?

I know there's a sensible answer to this, so please don't think I'm contesting the logic of it.

Thanks
Rob
nashyo is offline   0 Reply With Quote
Old Dec 18, 2011, 03:43 PM   #2
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
See the document ObjC Runtime Guide, Chapter 6 - "Type Encodings".
lloyddean is offline   0 Reply With Quote
Old Dec 18, 2011, 05:36 PM   #3
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Are you asking why would someone want to "save" an object to load again later?
jared_kipe is offline   0 Reply With Quote
Old Dec 18, 2011, 05:51 PM   #4
subsonix
macrumors 68030
 
Join Date: Feb 2008
Quote:
Originally Posted by jared_kipe View Post
Are you asking why would someone want to "save" an object to load again later?
I was wondering the same thing.

"Why would an Object orientated programmer want to encode an object to the file system"

Sounds like archiving an object to disk.
subsonix is offline   0 Reply With Quote
Old Dec 18, 2011, 11:55 PM   #5
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by nashyo View Post
I'm currently being introduced slowly to encoding in Objective C, and I have a fundamental question to ask.

Why would an Object orientated programmer want to encode an object to the file system, instead of create an array or dictionary of objects?

I know there's a sensible answer to this, so please don't think I'm contesting the logic of it.

Thanks
Rob
The way encoding works, the NSKeyedArchiver asks the root object to encode itself. If the root object's -encodeWithCoder: method involves encoding any objects it owns (ivars), they will be encoded into the graph, and any objects those own or reference will be encoded recursively until the entire logical content of the root object is mapped. One advantage would be that no object is encoded more than once, the archiver just encodes cross-references. All this goes into a data object in the form of a binary plist which can then be written to a file or stored in a user defaults entry.

If the complementary -decodeWithCoder: methods are written correctly, NSKeyedUnarchiver can restore the entire object graph using the data object that was archived. This technique allows a much richer and more complex structure to be saved and restored easily and efficiently than you might be able to do with an array or dictionary. Once decoded, you program state is essentially fully restored to what it was when you saved it (except, usually, without the undo stack).

If you look at the documentation for NSArray or NSDictionary, you will see that there are strict limitations on what they can write to a file; encoding lets you get around these limitations.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Dec 19, 2011, 10:53 AM   #6
nashyo
Thread Starter
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
Got it

Thanks for all the responses.

Sydde you answered that really well.
nashyo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
array, dictionary, encoding, objective

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Objective C Help - Encoding nashyo Mac Programming 25 Dec 21, 2011 10:51 AM
Array and Dictionary naphatkrit iPhone/iPad Programming 2 Aug 7, 2011 09:49 PM
How to integrate a SearchBar from an Array of Dictionaries Nekbeth iPhone/iPad Programming 1 Mar 28, 2011 01:25 PM
An Array Of Dictionaries Not Writing To File Correctly? Darkroom iPhone/iPad Programming 6 Aug 29, 2009 10:22 AM
Filtering an array with dictionary objects Farani iPhone/iPad Programming 0 Nov 8, 2008 01:59 PM


All times are GMT -5. The time now is 07:29 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC