views:

78

answers:

1

Hi, I have a problem. i want convert my object "Position" in a CFDataRef for sending it in the TCP Socket. I don't know how i can do. Can you help me?

@interface Position : NSObject {

    NSString *name;

    float valueX;
    float valueY;
    float valueZ;

}

-(BOOL)setValue:(NSString*)name:(float)valueX:(float)valueY:(float)valueZ;

@property (nonatomic,readwrite, retain)   NSString      *name;

@property (readwrite)   float      valueX;
@property (readwrite)   float      valueY;
@property (readwrite)   float      valueZ;


@end




//In another class after the correct connection.
    Position *tempPosition = [[Position alloc]init];

    for(int i=0; i<[arrayPosition count]; i++){
        tempPosition = [arrayPosition objectAtIndex:i];
        //I want have a CFDataRef of tempPosition for sending. dataSend is my CFDataRef variable
        CFSocketSendData(socketAccept, NULL, dataSend, 0);
        [NSThread sleepForTimeInterval:1];

    }
A: 

The short answer is to use NSKeyedArchiver. Looks like this and returns an NSData object (which is the same as -- has a toll-free bridge with -- CFDataRef):

NSData *tileData = [NSKeyedArchiver archivedDataWithRootObject:theTile];

(This from the CopyPasteTile sample code.)

However, if you're copying data between machines... I wonder if this is the right approach? According to the Binary Data Programming Guide:

In particular, it will not handle byte-order swapping when distributed between big-endian and little-endian machines. (In Cocoa, use NSValue for typed data.)

I'm not saying it won't work, but it's certainly something to think about.

Stephen Darlington