How to initialize NSData with array of shorts? - objective-c

How do I create an instance of NSData with an array of shorts? As I understand [NSData dataWithBytes] method expects input as an array of unsigned chars?
Thanks

dataWithBytes: takes a void *. You can pass anything you want in; just make sure you take endianness into account, if cross-architecture is an issue.

Related

Objective-c - NSData initWithContentsOfFile vs. dataWithContentsOfFile

What is the difference between these two objective-c statements?
NSData *documentBytes = [NSData dataWithContentsOfFile:filePath];
versus this:
NSData *documentBytes = [NSData initWithContentsOfFile:filePath];
From Apple's NSData Class Reference page, it states the following about each
dataWithContentsOfFile - Creates and returns a data object by reading every byte from the file specified by a given path.
initWithContentsOfFile - Returns a data object initialized by reading into it the data from the file specified by a given path.
To me, these seem functionally equivalent but I highly doubt they do the same thing in all cases, right?
Thanks in advance...
-Ergin
When you use init, you always have to use alloc, like so:
NSData *documentBytes = [[NSData alloc] initWithContentsOfFile:filePath];
This returns an NSData object with a retain count of 1, you now own the reference and are responsible for releasing it.
When using dataWithContentsOfFile
NSData *documentBytes = [NSData dataWithContentsOfFile:filePath];
You get back an autoreleased NSData object. You can use it and forget about it, the autorelease pool will take care of it. If you want to store it, you have to retain it.
Of course, when you are using ARC, you can forget about all of this ;-), the methods are essentially the same.
The second
NSData *documentBytes = [NSData initWithContentsOfFile:filePath];
Will not compile you will need to alloc it first, will look something like:
NSData *documentBytes = [[NSData alloc] initWithContentsOfFile:filePath];
But one or another will work the same, in the end you will have an NSData that has the contet of the file. The first one is a shortcut.
So about your doubt:
The first one you do not need to alloc the object first, the method will return the object for you, if you are not using ARC (I do not think so), the first one will return an object that the system will take care.
The second one you will need to alloc the object first, the method only initialize your object, and if you are not using ARC you will need to take care to release it.

NSData isEqualtoData

I really don't understand what's going on here.
I have a function that is getting the first 3 bytes from an NSData object, receivedStream, and putting them into another NSData object, temp, via a char array. Then comparing that to an NSData object created from a char array buffer. Both new NSData objects are created and have the correct contents. However, when isEqualtoData is called, I get an error:
[NSConcreteData isEqualtoData:]: unrecognized selector sent to instance (instance refers to tmp2)
I also get the warning
Instance method '-isEqualtoData:' not found (return type defaults to 'id')
which I don't understand as it's clear that this is a valid method in the docs. Do I need to declare NSData.h somewhere?
-(BOOL)checkHeader{
char tmp[3];
[receivedStream getBytes:&tmp length:3];
NSData *temp = [NSData dataWithBytes:tmp length:3];
NSData *tmp2 = [NSData dataWithBytes:header length:3];
BOOL test = [tmp2 isEqualtoData:temp];
return test;
}
The method is called isEqualToData:. Note the capital T – Objective-C is case-sensitive, as most programming languages.

Is there a method for adding one NSString for NSData?

I'm working on writing to a file one user input on a textField.
So far I have one NSFileManager which writes data to a file. Still, I have no way of putting the textField input inside a file.
Is there a way do add a string value to NSData so I can write it?
you can get NSData from NSString,
NSData *newData = [yourString dataUsingEncoding:NSUTF16StringEncoding];
use encoding that fits your case.
append the obtained data to existing NSData,
[existingData appendData:newData]
Note: "existingData" should be an instance of NSMutableData.
Use below code as your reference.
NSString* myString = #"Is there a method for adding one NSString for NSData";
NSData* myData= [myString dataUsingEncoding:NSUTF8StringEncoding];

Cocoa & Cocoa Touch. How do I create an NSData object from a plain ole pointer?

I have malloc'd a whole mess of data in an NSOperation instance. I have a pointer:
data = malloc(humungous_amounts_of_god_knows_what);
uint8_t* data;
How do I package this up as an NSData instance and return it to the main thread? I am assuming that after conversion to an NSData instance I can simply call:
free(data);
Yes?
Also, back on the main thread how do I retrieve the pointer?
Thanks,
Doug
You want one of the -dataWithBytes:length: or its variants:
NSData *d = [NSData dataWithBytes:data length:lengthOfDataInBytes];
which copies the bytes and you can then free(data). To save a copy, assuming data is allocated using malloc use:
NSData *d = [NSData dataWithBytesNoCopy:data length:lengthOfDataInBytes];
You should not call free on your buffer in this case, as the NSData instance will free it for you.
Note that all of these methods return an autoreleased instance, so you will probably have to retain it if you want to keep it around between threads (and aren't using GC). You can use the equivalent alloc/initWithBytes:... initializers instead.
To get a pointer to an NSData's contents, use bytes.
(I think a few minutes with the NSData documentation will serve you well)
Thanks Barry,
I actually went with something even simpler.
Put an NSValue wrapper around the pointer:
NSValue *value = [NSValue valueWithPointer:imageData];
Stuff it in a dictionary to be returned to the main thread:
[result setObject:value forKey:cubicFaceName];
Back on the main thread, when I'm done with the data I discard it:
uint8_t *bits = [value pointerValue];
free(bits);
Cheers,
Doug

Converting NSMutableData to NSString Problem

initWithData does not convert my data object into a string properly. When I check the length of the data object, it has a value.
NSMutableData* receivedData =[[NSMutableData data] retain];
NSString* json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
Am I doing something wrong creating the string?
As posted, the code is nonsense. You are creating an empty immutable data and then trying to create a string from said empty data.
What does * When I check the length of the data object, it has a value* mean? Do you mean that you have more code that you aren't showing? Something that is filling the mutable data with some bytes?
Also, if the received data is not actually encoded as a UTF-8 string, the conversion will fail. There are a number of methods on NSString that allow for lossy conversion. Try one of those.
I didn't fully complete the NSURLConnection delegate methods. This is where my data is being built.