I'm working on a Bluetooth App, where I have to store the 8Bit values from the sensor into a byte array like so:
- (void) getDataFromSensor: (NSData *)tempData {
const Byte *rawSensorBytes = [tempData bytes];
}
The confusion I am having is with the specific line [tempData bytes]. So, I know that tempData is the NSData variable and bytes is a property on NSData. According to the developer doc, the byte property is "A pointer to the receiver’s contents." I have heard of this concept of reciever before, but havent really paid attention to it, does the definition basically mean this byte property just points to the the data that is stored somehwere in memory? Also, if anyone could explain receiver that would be great!
Thanks!
>>Firstly, the way I explained everything here, is it correct?
Yes, it looks technically correct to me.
>> Also, in the method call when I pass characteristic.value, am I actually passing in the actual value or the address to that value?
"characteristic" refers another object, of class CBCharacteristic, so we have 3 blocks of memory involved, 2 instances of classes, and a "plain" block of memory with the raw data. The CBCharacteristic instance contains a reference to the NSData instance, which in turn contains a pointer to the raw data.
Because CBCharacteristic is an Obj-C class, and instances of classes are passed (in, say, function parameters) as references, not by copying their data. That's why such classes are called "reference types" — any variable of that type is actually a reference to the instance — to distinguish them from "value types", where a variable of that type contains the actual data.
Those terms are the most useful, and are what Swift uses. The traditional Obj-C terminology would be "class type" and "scalar type", since things that are passed around by value tend to be simple scalar values such as integers and doubles.
Finally, that's why variables referring to class instances in Obj-C have a "pointer-to" asterisk in their declaration (e.g. "CBCharacteristic*"), though the compiler knows that without you telling it. It just reinforces the fact that they are references. (In C++, just to confuse the issue, class instances can be created as value types or reference types.)