2. Create persistent objects with NSCoding

Cocoa’s NSCoding protocol makes it possible to quickly save and restore objects in binary archives. Here’s a small example. It encodes an array into an NSData object, then restores it.

Encoding Demonstration [ruby]
def encoding_demo
  x = ObjC::NSMutableArray.alloc.init
  x.addObject_ "one"
  x.addObject_ "two"
  x.addObject_ 3
  d = ObjC::NSKeyedArchiver.archivedDataWithRootObject_(x)
  y = ObjC::NSKeyedUnarchiver.unarchiveObjectWithData_(d)
  y.each {|item|
    puts item.to_s
  }
end

Did you find an error? Is something missing? Post your comment or suggestion below!

Comments (0) post