-1

I have a class called Account with 6 properties and one of the properties is a c struct. I can't figure out how to use NSCoding to comply with the struct. How would I go about encoding and decoding the c struct property. The struct is the type app_config_t.

the struct

typedef struct app_config_t
{
    pj_pool_t               *pool;

    pjsua_config            cfg;
    pjsua_logging_config    log_cfg;
    pjsua_media_config      media_cfg;

    pjsua_transport_config  udp_cfg;
    pjsua_transport_config  rtp_cfg;


    pj_bool_t               ringback_on;
    pj_bool_t               ring_on;

    int                     ringback_slot;
    int                     ringback_cnt;
    pjmedia_port            *ringback_port;

    int                     ring_cnt;
    SystemSoundID           ring_id;
    CFRunLoopTimerRef       ring_timer;
} app_config_t;

An Objective-C class called 'Account' with property App_config

#pragma mark - NSCoding
- (void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.Username forKey:@"username"];
    [aCoder encodeObject:self.Password forKey:@"password"];
    [aCoder encodeObject:self.Server forKey:@"server"];

    [aCoder encodeInt:self.Account_id forKey:@"Account_id"];
    [aCoder encodeInt:self.Call_id forKey:@"Call_id"];

    //trying to save this object, is this even correct
    NSMutableData *data = [NSMutableData dataWithBytes:&_App_config length:sizeof(app_config_t)];
    NSKeyedArchiver *App_config_Archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [aCoder encodeObject:App_config_Archiver forKey:@"App_config"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init])
    {
        self.Username = [aDecoder decodeObjectForKey:@"username"];
        self.Password = [aDecoder decodeObjectForKey:@"password"];
        self.Server = [aDecoder decodeObjectForKey:@"server"];

        self.Account_id = [aDecoder decodeIntForKey:@"Account_id"];
        self.Call_id = [aDecoder decodeIntForKey:@"Call_id"];

        self.App_config = ?????????

    }

    return self;
}
blitzeus
  • 485
  • 2
  • 10
  • 28

2 Answers2

0

You current encoding code has error. Mutable data you provide for NSKeyedArchiver is used by archiver to store encoded data. So copying app_config_t structure is useless.

You need to encode each field of your structure with NSKeyedArchiver. It has encoders for primitive types. Also in app_config_t structure I see fields that are not primitive types but complex types. So you need custom encoding for such fields too.

- (void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.Username forKey:@"username"];
    [aCoder encodeObject:self.Password forKey:@"password"];
    [aCoder encodeObject:self.Server forKey:@"server"];

    [aCoder encodeInt:self.Account_id forKey:@"Account_id"];
    [aCoder encodeInt:self.Call_id forKey:@"Call_id"];

    // this data will have encoded structure
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *App_config_Archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    // for all fields from structure you need to use such encoders
    [App_config_Archiver encodeBool:_App_config.ringback_on forKey:@"ringback_on"];

    // encode data itself
    [aCoder encodeObject:data forKey:@"App_config"];
}
John Tracid
  • 3,836
  • 3
  • 22
  • 33
0

You should be able to treat the struct as a memory buffer and use NSCoder's encodeBytes function to do this:

- (void)encodeBytes:(const void * _Nullable)byteaddr length:(NSUInteger)length

AmsalK
  • 416
  • 5
  • 9
  • He has several pointer fields in structure. It could be a problem if you just encode structure as a memory block. – John Tracid Aug 03 '15 at 23:11