This is a simple class ive created in C++ for a musical application im creating for an iOS device which will store some values of notes and their timings :
class info {
public:
float attackTime;
Note noteStriked;
void setData(float timeOfAttack, Note nameOfStrikeNote){
attackTime = timeOfAttack;
noteStriked = nameOfStrikeNote;
}
};
above… Note is a struct which can only contain default values such as {SNARE, DRUM, HIHAT} and so on. The idea is to create a Note object and store those objects in a NSMutableArray for later access.
Then in my main .h file i have a NSMutableArray sequenceOfNotes; and in my .m file i am trying to add an object to my mutablearray… but i dont know how to do this. Ive tried various things but with fail it does not work!
//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];
The error produced on that line is
Cannot initialise a parameter of type ‘id’ with an lvalue of type ‘NoteData *’
what id then like to do after the error is fixed is to be able to retrieve my object at any position of the array i choose and then be able to choose an attribute variable from that object at that specific index.
//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;
Obviously theres a conversion issue.. if anyone can help me with this that would be awesome
You have to wrap your C++ object pointer into a NSValue:
Alternatively, why don’t you just use a
vector<MyCPPObject>or alist<MyCPPObject>?