如何创建和分配C缓冲区作为目标C类的一部分 [英] how to create and allocate a C buffer as part of an objective C class
本文介绍了如何创建和分配C缓冲区作为目标C类的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最好用一个例子来解释:
best to explain with an example:
在我的AudioItem.h中
in my AudioItem.h
#define ITEM_CAPACITY 100
typedef struct DataStruct {
void * content;
UInt32 size;
} DataStruct;
typedef DataStruct *DataStructRef;
@interface AudioItem : NSObject
{
DataStructRef data;
}
@property (assign, readwrite) DataStructRef data;
在AudioItem.m中 @合成数据;
in AudioItem.m @synthesize data;
-(id)initWithID:(NSString *)itemID
{
self = [super init];
data->content = malloc(ITEM_CAPACITY);
return self;
}
上面的代码看起来很像这个 ,但我收到BAD_EXEC_ERROR ..怎么回事?我想使用C缓冲区而不是某些NSMutableData或我尝试使用NSMutableData尝试的b/c的原因,我感觉这正在减慢我的实时应用程序
The above code looks a lot like this one, but I get a BAD_EXEC_ERROR.. how come? The reason why I would like to use a C buffer rather than some NSMutableData or whatever is b/c I've tried using NSMutableData and I feel like it's slowing down my real time application
推荐答案
它失败,因为在设置其内容时data
是空指针.
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;
}
这篇关于如何创建和分配C缓冲区作为目标C类的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文