How to store and retrieve multiple NSStrings in NSData - ios

Is there any way I can store and retrieve multiple text strings in an NSData object?
I am looking into external accessories and just wondering how multiple arbitrary parameters are transmitted to and from the devices.
I assume that they will need to be decoded into NSString objects from an NSData object. So is there any way to distinguish between separate NSString objects from one stream of data?
Or, will multiple string parameters be stored in separate NSData objects? With a buffer for each?
Thanks

Use the NSKeyedArchiver to to archive an array of NSStrings.
+ (NSData *)archivedDataWithRootObject:(id)rootObject
NSKeyedArchiver Reference

Related

What is different between Data from String and Image in Swift?

In Swift, We can convert String to Data, and convert from UIImage to Data also. So, what is different between two Data? Can Anyone help. Thanks.
To quote Apple's documentation:
The Data value type allows simple byte buffers to take on the behavior of Foundation objects.
Essentially it provides a byte buffer representation of Foundation objects, which allows you to access/manipulate the object's bytes in memory.
You can read more about the Data structure on Apple's documentation: https://developer.apple.com/documentation/foundation/data

Objective-C - Serialize/De-serialize multiple arrays to/from a local file

I've read some SO posts here and there on how to write arrays to file but I'm having issues trying to figure out how to write (serialize) multiple arrays to a single file then read back later. Based on what I've read so far I think the easiest way might be to use
[array writeToFile:filePath atomically:YES];
but what's the best way to handle writing multiple arrays to a file? My understanding is that each time you use the above method that it will re-write the entire file.
I have 8 arrays I need to serialize, and I would like to use newline (\n) to separate each array onto a new line in the file. Can anyone provide guidance/psuedo code/Objective-c code for accomplishing this?
You can put all your arrays in to another array and save that array.
NSArray *arrayToSave = #[array1, array2, array3];
[arrayToSave writeToFile:filePath atomically:YES];
Put all the arrays into an NSDictionary, write the dictionary.
Methods like NSArray's writeToFile:atomically: method create property lists, which are specific kind of file unique to Cocoa/Cocoa touch. On recent platforms those methods create XML property lists.
You won't get an array per line with newline characters between them with property lists. The file will have XML tags in it, and newlines added for visual clarity. The newlines are not meaningful in the data format however.
If you want a particular byte format for your data, you'll need to write your own methods for serializing your arrays.
What makes you think you need newlines between the arrays?

best way to serialize native C array

I'm currently using NSCoding to serialize a tree of objects, but 1 of them contains as data member a native C float array with 1,000,000 entries, so in order to serialize it using encodeFloat:forKey: for each array entry, I need to apply 1,000,000 useless keys , that might be very slow. what the prefered way to handle this?
for each array entry, I need to apply 1,000,000 useless keys
No, you definitely do not need separate keys for each element. A C array is a contiguous block of memory, so you can simply create a NSData object from that block and store that as Hot Licks suggested. Or, since a million floats will require a fair bit of storage, you might compress the data before storing it. And in fact, you don't really even need NSData -- you can encode a range of bytes directly with -encodeBytes:length:forKey:.

Read Byte Array From URL and Convert To PDF in iOS

I have a service in Windows that returns a PDF data, which it's in the database in a varbinary field. This service returns the PDF data as a Byte Array in JSON format
How can I read this data and convert it to PDF again, in my app in Xcode?
Which is the correct structure data to retrieve this byte array? NSArray? NSMutableArray?
I want to store this data, and I'm using a NSData structure...
Thanks
An NSArray would be for arrays of objects, you want an array of bytes, which an NSData is.
To get the data out of JSON, you would use the NSJSONSerialization class to extract the data from your JSON object -- the deserializer will probably return your literal data as an NSString, which you then would convert into an NSData by decoding it.
Presumably your raw data is in the JSON as Base64 or some such.

Best practice to store un-mutable data on iOS

I am looking to create a navigation based reference app for the iOS.
I have considered the following ways to store the data:
hard coding
plist file
some kind of comma delimited file
The data structure that I will be using has a bunch of strings, an array, and a reference to a picture.
What do you think the best way to store this data is without getting into CoreData?
Thanks
Also I dont think it would be more than 500 entries.
Well, this is not a "best-practice" problem for any case.
The data structure that I will be using has a bunch of strings, an array, and a reference to a picture.
What do you think the best way to store this data is without getting into CoreData?
For your needs, I suggest you look into NSKeyedArchiver.
NSString, NSArray, and UIImage all know how to encode and decode themselves. Just use an NSKeyedArchiver. Note that the objects in your collections (e.g. NSArray) must adopt #protocol NSCoding.
If you need to open this on a mac, then convert the UIImage to NSData using a proper image file format representation (e.g. PNG or JPEG) because UIImage is not available.
In detail:
hard coding
That could mean a number of things.
plist file
You're working with large non-plist types. That would mean you would need to convert to and from UIImage<->NSData unnecessarily, which would add a lot of overhead -- memory, CPU, and potentially file size. All these types can encode themselves better than (or as good as) a plist representation.
some kind of comma delimited file
Your image will not allow that to happen (reliably).

Resources