converting NSDictionary object to NSData object and vice-versa
I have to convert an
NSDictionary object into
NSData and further I have to get the same
NSDictionary out of the
NSData object. How should I go about it?
- How do I draw a shadow under a UIView?
- iOS UIView Animation Issue
- How to preserve identifierForVendor in ios after uninstalling ios app on device?
- What's the difference between the “global queue” and the “main queue” in GCD?
- UINavigationBar Hide back Button Text
- Retweet, reply and favorite in iOS 5 Twitter with the Accounts framework
2 Solutions Collect From Internet About “converting NSDictionary object to NSData object and vice-versa”
To convert NSDictionary To NSData
NSMutableData *data = [[NSMutableData alloc]init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data]; [archiver encodeObject:YOURDICTIONARY forKey: YOURDATAKEY]; archiver finishEncoding]; [data writeToFile:YOURFILEPATH atomically:YES]; [data release]; [archiver release];
To get the NSDictionary back from the stored NSData
NSData *data = [[NSMutableData alloc]initWithContentsOfFile:YOURFILEPATH]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; YOURDICTIONARY = [unarchiver decodeObjectForKey: YOURDATAKEY]; [unarchiver finishDecoding]; [unarchiver release]; [data release];
A much simpler version of Robert’s answer:
[NSKeyedArchiver archiveRootObject:YOURDICTIONARY toFile:YOURFILEPATH];
YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithFile:YOURFILEPATH];
Or to answer the question as originally set, without imputing a file into things:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:YOURDICTIONARY]; ... YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithData:data];
It’s all factory methods so it’s the same code with or without ARC; the methods used have been available since OS X v10.2 and on iOS since day one.
- How to round a Double to the nearest Int in swift?
- openURL in APPDelegate conversion error NSString -> String (Swift & iOS8)
- How to I turn off the ambient light in Scene Kit (with Swift)?
- “Ambiguous use of subscript” Error after new Swift update
- UICollectionView not updating inside UITableViewCell
- Retrieve SecKey from Keychain
- iPhone6 Display Zoom Functionality disturbs the whole view
- UITableView – scroll to the top
- Has anyone implemented the PayPal API through a native iPhone app?
- How can I set the “User-Agent” header of a UIWebView in Swift
- statusBar for iOS7 in Xcode 4.6
- iAd banner not clickable if banner moves
- How to match beginIgnoringInteractionEvents before adding endIgnoringInteractionEvents
- Set the title of Navigation Bar created in Interface Builder
- Unable to set custom font for the UILabel in XCode