I have multiple large dynamically created NSDictionaries which contain (among other) multiple NSData objects.
I need to "pretty-print" this dictionary. Using -description I get a quite good result except for the NSData stuff which is not completely printed as it is cropped by the method.
Is there a way (except for iterating over the whole dictionary with a nested for-loop) to convert all the NSData objects to a Hex-String representation?
Related
I'm receiving data in XML from a server through a NSData object and I would like to convert this data to a NSMutableArray object.
How do I convert the data from the NSData object (knowing that there should be different objects transferred into the NSMutableArray - UIImages ,CLLocation2D objects, strings,...)
Give you a URL, check it by yourself, it is easy to solve.
I want to save the following NSDictionary to a MySQL database:
NSDictionary notesDictionary:
object: NSString key:"note"
object: NSArray key:'subNotes"
object: NSString key:"publishDate"
The tricky part is that the NSArray subNotes is just another array of dictionaries with the same keys as above. So a note is put in a dictionary, which in turn has an array of dictionaries, which in turn has an array of dictionaries, and so on (each note has a subnote, and subnotes have subnotes, and so on).
I'm new to MySQL, so the following is the solution I've found to storing the above dictionary in the database:
NSString *jsonString = [notesDictionary JSONRepresentation];
Then I just store the string into the database, and retrieve it with similar methods. This works, but I'm not sure if it scales. If I had a 1000 notes, would this cause any performance setbacks, as the entire dictionary is saved as a string, and then later converted from a string back to a dictionary. Is this a good, fast, and secure way? Or is there something else I should be looking into?
JSON is a perfectly good serialization format. I wouldn't worry about performance or anything like that. The only concern I'd have is making sure the MySQL column is set up to take an arbitrarily long string. (Not sure how to do that in MySQL offhand, but it shouldn't be too tough.)
I have a Class for handling my data in my project, and now I need to store the data.
I'd like to use a Plist but I'm a bit unsure of how to start.
My class is pretty simple - 6 pieces of data, flat (no hierarchy).
I want my app to start with no data, so can I assume that I should create the PList programmatically once the User creates their first piece of data? (That is, don't create a .plist file in 'Supporting Files' prior to distribution?)
Then, when the app starts the next time, read the data and create an NSMUtableArray array of Class instances?
To create a property list, all you need to do is use appropriate types (i.e. those that support the property list format: NSData, NSString, NSDictionary, NSNumber, NSDate, NSArray), store them in a single container, and tell the containing object to write itself to a file. To read the data, you can initialize that same type using a path. For example:
// writing some data to a property list
NSString *somePath = ... // replace ... with the path where you want to store the plist file
NSMutableDictionary myDict = [NSMutableDictionary dictionary];
[myDict setObject:#"Caleb" forKey:#"name"];
[myDict setObject:[NSNumber numberWithInt:240] forKey:#"cholesterolOrIQ"];
[myDict writeToFile:somePath atomically:YES];
// reading the file again
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:somePath];
The simplest way is to simple save an NSArray or NSDictionary to disk. Caleb's answer goes into detail there so I won't repeat it, other than to say you might have to convert a non-compatible object like NSColor to an property list object like NSData. It's up to you to do this each time you save or load your data.
NSKeyedArchiver and NSKeyedUnarchiver give you a little more control over the process, but work pretty much the same way. You provide (or get back) a plist compatible root object (usually an NSDictionary) that contains your data. I recommend creating a dictionary that includes your data structure as well as an arbitrary number (your app's build number is a good choice) to use as a version indicator. This way if you ever update your data model you can easily determine if you need to do anything to convert it to the new version.
If you're putting your own objects into the data file, look into NSCoding. The protocol gives you two methods using NSKeyedArchiver and NSKeyedUnarchiver to save and restore your data. This is by far the most straightforward approach if your data model consists of anything more than a few simple strings and numbers, since you're dealing with your own native objects. In your case, you would have your data class implement NSCoding and use the NSKeyedArchiver and NSKeyedUnarchiver methods to encode your six instance variables. When it's time to save or load, pack the instance of your class into an NSDictionary (along with a versioning number as I mentioned above) and call NSKeyedArchiver's archiveRootObject:toFile:. Your save an load methods deal only with your own data object, which makes things easy for you. The common pitfall to watch out for here is if your custom data object contains other custom object. This is fine, but you have to make sure every object that's going to be saved has its own NSCoding implementation.
Two things you can do:
Use NSUserDefaults:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
The objectForKey method is the one you want to use to store your class. But, as pointed out in the comments, this shouldn't really be used for storing lots of user data; it's best for saving preferences.
For storing more data, you might want to look at Core Data. It's more complex, but should be better suited to your needs. Here's a tutorial on it:
http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
Neither of these seems best for your simple application, but I leave this answer up since it gives alternatives for saving data to the iPhone.
I have to merge two data objects and later separate them.Can anybody suggest me the way to go about it.It's urgent!!!!!!
please respond fast....
Thanks
Have a look at NSMutableData. Specifically, look at the appendData: and appendBytes:Length methods.
You can 'merge' two NSData objects by appending them to an empty NSMutableData object. You can then probably retrieve the data using getBytes:range: and reconstruct your NSData object with dataWithBytes:Length:.
Have a look at this article for working with mutable binary data (includes sample code).
I have an interesting problem.
I have 4 arrays stored in a .plist file, they are called "vertices", "normals", "texCoords" and "polygons" (this file is attached, along with GLViewController.m).
l want to load these arrays into arrays of type Vertex3D, Vector3D, GLfloat and GLubyte respectively, and then render them using OpenGL.
However, I am unsure how load the arrays and was hoping you might be able to help.
Bear in mind that I will want to modify the size of the arrays in the plist, so their size cannot be assumed to be constant (they could have any number of indices).
Links:
Plist: pastie.org/782396
GLViewController.m: pastie.org/782399
Plists are always loaded into arrays or dictionaries of "plist objects": NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary. So the only way to deal with them is to load the plist into an array or dictionary, then iterate through the resulting objects in order to build new arrays or whatever of the appropriate type.
So basically you'll need to write some sort of code to "translate" from plist to the object types needed by OpenGL.
Check out NSArray arrayWithContentsOfFile: and NSDictionary dictionaryWithContentsOfFile:
Also see the Property List Programming Guide.