Byte Buffers NSData - ios

I'm just confused about when to use NSData. I'm about to get some data stored in the server (I'm just using dropbox here instead for some practice.)
What I did so far is
Made some p-list storing 2 arrays, one stores string object , and the other stores string objects of the public URL for the audio data.
When I created the NSString from the plist , I didn't use NSData object
When I created the NSAudio Player with data stored in the same project folder, I didn't use NSData object
When I created the NSAudio Player with data stored in a server, I used the NSData
I just don't understand when to use NSData object properly. I checked the app document, but it says "Data objects let simple allocated buffers" What is the allocated buffers here?
Thanks,

NSData is just a wrapper for a byte array. Anywhere that you specifically need a byte array you can either use a byte array and do all manipulations manually (accessing and manipulating the data) or you can wrap it in an NSData (or NSMutableData if you need to modify the bytes) and use Apple provided functions to easily access or modify the data.
The allocated buffer is the byte array stored inside the NSData wrapper. Say you have an audio object on disk that you want to modify one byte in the middle of. You could load that data from disk into an audio element, but then you can't modify it. If you load it's byte values from disk into an NSMutableData you can have access to the bytes directly, modify whatever you would like using simple methods provided to the NSMutableData class, then same the audio element back to disk (or load that data directly into your audio element).
The best use of an NSData object is only when you need it, just like any other class. If you specifically need the functionality to run your app, then use it. Otherwise it is likely just an added step that is not required (ex data-on-disk -> audio-element vs data-on-disk -> NSData -> audio-element).

Related

iOS - NSFIleManager - How to deal with low disk space

When writing data to file (e.g. Thumbnails for caching, user data, etc.), how do you deal with the fact that the iDevice could not be able to write your data to file since the disk is full?
Will NSFileManager throw an exception in case of low disk space?
What's the designated way to deal with this and to inform my user that there's very little disk space left for his data? (I'm saving a fair amount of different data at different places in my app and searching for a common way to deal with it.)
As you mentioned in the comments that you want to save NSDictionary. If you only want to know whether the file is saved successfully or not, you can inspect the return value of the
writeToFile:atomically: function.
Return Value
YES if the file is written successfully, otherwise NO.
More information under the NSDictionary's Storing Dictionaries Section.
Alternatively,
If you want to get a more detail error message for the failure (such as out of disk space, folder not exist and etc.), then you can convert the NSDictionary to NSData before saving it.
NSDictionary to NSData:
NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
NSData to NSDictionary:
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
The benifits is that you will also have access to this API -writeToFile:options:error:.
If there is an error writing out the data, upon return contains an NSError object that describes the problem.
Also more detail could be found under the Storing Data Section of NSData.
I think that's the best you can do in case there is a low disk space problem on the device.

audio NSData to NSString encoding

I'm using Parse as a backend and it doesn't like NSData. I have an array that I need to store that holds the data of different audio files in each of its elements. I know I can convert my data to NSString, but I'm not sure what kind of encoding is appropriate. The strings will need to be converted back to an NSData objects. Any suggestions?
PFFile is the class you want, you can create it from NSData if you want to save binary files: https://parse.com/docs/osx/api/Classes/PFFile.html

UIImage from NSInputStream

I'm downloading a 2400x1600 image from Parse and I don't want it to hold all that data in memory at once. PFFile object from Parse has a convenient method to get NSData as NSInputStream so when the data is finally downloaded I end up with a NSInputStream.
So now I want to use that NSInputStream to get my UIImage. It should work like creating a UIImage with contents of file method i.e. not the whole image is loaded into memory at once.
I think that writing to a file from NSInputStream and then use the UIImage's contents of file method should work fine in my case, but I have found no way to write to a file from a NSInputStream.
Any code example or some guideline would be really appreciated.
Thanks in advance.
To accomplish this you can set up an NSOutputStream to then stream the received data to a file. Create your output stream using initToFileAtPath:append: with YES for append. In your input stream call back, pass the data to your output stream by calling write:maxLength: (read more in the docs). Once the stream is complete, you then have the full image on file without ever having it fully in memory.
Henri's answer above is more appropriate since you're using Parse, but this is the general solution.
In the documentation on iOS/OS X, Parse brings this an example.
Retrieving the image back involves calling one of the getData variants on the PFFile. Here we retrieve the image file off another UserPhoto named anotherPhoto:
PFFile *userImageFile = anotherPhoto[#"imageFile"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
}
}];
Now, I don't quite see the reason for you to use NSInputStream, mainly for two reasons:
NSInputStream is supposedly meant for INPUTTING data, not taking it from somewhere
NSInputStream is meant for streaming, so for scenarios in which you want to do something with the data as it is coming in, from your description it seems as if you only ever care about the data once it has completed the download.
In short, you should be using the aforementioned way, unless you truly care about the way the data is loaded in, for example wanting to manipulate it as it comes in (highly unlikely in the case you describe).
As to having it all in memory at once, the dimensions you give are not that large, yes you could stream it into a file, but assuming you want to show it full-size in the app, the problem of memory would appear at some point nevertheless, i.e you would just be postponing the inevitable. If that is not the case (not showing full-size), then it might be a good idea to chop the source image up into tiles and use those instead, far quicker to download specific tiles and easier on memory.

Is data handed over to UIPasteBoard deep copy or shallow copy?

If an app sets data in the UIPasteBoard of iOS, are they deep copied or shallow copied?
If you ask about - (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType than it is surely a Deep copy.
If you ask about strings, URL's, Colors they are also Deep copied.
Only about the Images I am preety sure that it uses the same CGImageRef, because UImage does not implement NSCopying protocol
You can test it just closing your application and see that data stays in the pasteboard.
UIPasteboard uses NSData to store the data. Hence, it is deep copy.
From the official documentation for "setData:forPasteboardType:"
"Use this method to put raw data on the pasteboard. For example, you could archive a graph of model objects and pass the resulting NSData object to a related app via a pasteboard using a custom pasteboard type. (To put objects—such as NSString, NSArray, NSDictionary, NSDate, NSNumber, UIImage, or NSURL objects—on the pasteboard, use the setValue:forPasteboardType: method.) This method writes data for the first item in the pasteboard. Calling this method replaces any items currently in the pasteboard."

Read plaintext document into NSArray in iOS?

I'm new to iOS but have plenty of experience with c++ and Python. I'm trying to figure out how to read a plaintext file I have on my computer into an NSArray in xcode. In c++ I would do this:
while(istr>>string) myArray.push_back(string);
However, I need to create a local copy to be stored on the iOS device. Is there a way I can package this data so that a local copy of JUST THE ARRAY will be stored on the device? I was thinking of maybe doing something with a JSON serialization or something.
Should I really just suck it up and do this:
NSArray myArray = [[NSArray alloc] initWithObjects: #"myInfo", nil];
I just want a more elegant way to handle this, I guess.
I think maybe you're thinking a little too C about this. In C and C++, strings are arrays of bytes. In ObjC, there's an object for that. It's called NSString, and it's probably what you should be storing plaintext in.
It even has an easy class method to help you out with this if you already have a byte array:
+(id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
See the NSString documentation for more details.
As to storing it on the device, there are solutions that range from the simple (NSUserDefaults) to the complex (Core Data), but pretty much anything will expect plain text be in an NSString.
EDIT:
The title of this question talks about reading the string from the filesystem. First step is to get the bytes of the file into an NSData object. The easy way:
+(id)dataWithContentsOfFile:(NSString *)path
Then make a string out of the data with this initializer of NSString:
-(id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
I don't know if this can help you, anyway if you just need to store an array of data to filesystem and deserialize it back to NSArray, an easy way is to use plists.
It is a convenient way to store a small amount of data, without any kind of relationship (there is Core Data for that). The main advantage is that you can store in it NSArray, NSDictionary, NSNumber, NSString, NSDate and NSData (so any kind of binary information) and they get automatically serialized and deserialized through some simple methods.
You can write an NSArray to a file in this way:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
and deserialize it back with this:
+ (id)arrayWithContentsOfFile:(NSString *)aPath
If you just want to provide some initial data to your app, and it is for example an array of strings or something similar, you can manually add a plist to your project by going to File->New->File and choosing Resources->Property list, and fill it by hand.
You can read more at https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html

Resources