how to create and allocate a C buffer as part of an objective C class

前端 未结 1 1094
悲&欢浪女
悲&欢浪女 2020-12-04 01:00

best to explain with an example:

in my AudioItem.h

#define ITEM_CAPACITY 100

typedef struct DataStruct {
    void *                          conten         


        
相关标签:
1条回答
  • 2020-12-04 01:37

    it fails because data is a null pointer when you set its content.

    the easy way to do this is:

    enum { ITEM_CAPACITY = 100 };
    
    typedef struct DataStruct {
        char content[ITEM_CAPACITY];
        UInt32 size;
    } DataStruct;
    
    @interface AudioItem : NSObject
    {        
    @private
        DataStruct data;
    }
    
    @implementation AudioItem
    - (id)initWithID:(NSString *)itemID
    {
        self = [super init];
        if (0 == self) return;
        data.size = ITEM_CAPACITY;
        return self;
    }
    
    0 讨论(0)
提交回复
热议问题