将类C++实例转换为 NSObject,然后添加到 NSMutableArray
convert instance of C++ class to NSObject and then add to NSMutableArray
这是一个
简单的类,我C++为iOS设备创建的音乐应用程序创建,它将存储一些音符值及其计时:
class info {
public:
float attackTime;
Note noteStriked;
void setData(float timeOfAttack, Note nameOfStrikeNote){
attackTime = timeOfAttack;
noteStriked = nameOfStrikeNote;
}
};
以上。。。注意是一个结构,它只能包含默认值,如{军鼓,鼓,HIHAT}等。这个想法是创建一个 Note 对象并将这些对象存储在 NSMutableArray 中以供以后访问。
然后在我的主 .h 文件中,我有一个 NSMutableArray sequenceOfNotes;在我的 .m 文件中,我正在尝试将一个对象添加到我的可变数组中......但我不知道该怎么做。我尝试了各种事情,但失败了,它不起作用!
//Create one instance of the class
NoteData *currentNoteData;
// Update the instance of the class.. its two variables: attackTime and noteStriked
currentNoteData->attackTime = timeHit;
currentNoteData->noteStriked = SNARE;
//Then im trying to add the above instance to my mutableArray below
[sequenceOfNotes addObject:currentNoteData];
该行产生的错误是无法使用类型为"NoteData *"的左值初始化类型为"id"的参数
修复错误后,id 喜欢做的是能够在我选择的数组的任何位置检索我的对象,然后能够从该特定索引处的该对象中选择一个属性变量。
//PsuedoCode
array {
position 0: NoteData object {
attackTime = 45.34,
noteStriked = HIHAT
}
position 1: NoteData object {
attackTime = 65.32,
noteStriked = SNARE
}
position 2: NoteData object {
attackTime = 78.53,
noteStriked = HIHAT
}
position 3: NoteData object {
attackTime = 98.44,
noteStriked = KICK
}
etc etc
}
//and then convert NSObject to normal c++ object something like this...
NoteData temp = [noteSequence objectAtIndex:0];
//so that i can then do this:
float currentTime = temp.attackTime;
Note currentNote = temp.noteStriked;
显然有一个转换问题..如果有人可以帮助我解决这个问题,那就太棒了
您必须将
C++对象指针包装到 NSValue 中:
[MyArray addObject:[NSValue valueWithPointer:new MyCPPObject()]];
...
MyCPPObject *obj = [[MyArray objectAtIndex:index] pointerValue];
或者,您为什么不只使用vector<MyCPPObject>
或list<MyCPPObject>
?
相关文章:
- 如何添加mp3文件作为资源,然后播放
- 将集合的随机元素添加到列表中,然后将其从原始集合中移除
- C++ std::vector 创建对象然后添加对象与在向量中创建对象之间的区别?
- QT-仅用密钥填充QMAP,然后添加每个键的值
- C++基本的转换,然后添加用户输入
- 如何将文件添加到Android项目中,然后使用NDK加载它
- 如何在数组中的范围内添加所有数字?然后找到该范围的平均值
- 在编译时识别所有调用函数 1 的所有 T,<T>然后添加行函数 2<T>()
- 将结构添加到字节向量,然后添加另一个
- STD :: MAP-如何查找或添加,然后可以在一次搜索中删除项目
- 从txt文件中读取数字,添加一些数字,然后在c ++中将其重写为其他文件
- 使用 CMake 构建项目,然后向 VS 添加更多库
- 读取文件内容,存储在数组中,向数组添加更多内容,然后将新数组存储在文件C++中
- 如何将多个整数添加到字符数组中,然后将它们拉出
- 首先创建字符串,然后通过移动语义将其添加到 vector 或在 vector 中创建元素是否具有内存效率
- 将类C++实例转换为 NSObject,然后添加到 NSMutableArray
- 添加 15 然后应用 &~0xf 有什么作用?
- 裁剪图像,然后添加边框
- 如何将随机数组值一起添加到另一个数组中,然后在计算出现的值时显示它们?C++
- 读取文本文件,然后添加字符到列表