這是用C創建++用於音樂應用即時創建用於將存儲的筆記一些值和它們的定時iOS設備的簡單類IVE:轉換C++類的實例來NSObject的,然後添加到的NSMutableArray
class info {
public:
float attackTime;
Note noteStriked;
void setData(float timeOfAttack, Note nameOfStrikeNote){
attackTime = timeOfAttack;
noteStriked = nameOfStrikeNote;
}
};
上面...注意是一個只能包含{SNARE,DRUM,HIHAT}等默認值的結構。這個想法是創建一個Note對象並將這些對象存儲在NSMutableArray中以供以後訪問。
然後在我的主要.h文件中我有一個NSMutableArray sequenceOfNotes;並在我的.m文件中,我正在嘗試向我的mutablearray添加一個對象...但我不知道如何執行此操作。我嘗試了各種各樣的東西,但失敗了,它不起作用!
//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];
在該行所產生的誤差是 無法初始化類型「ID」的參數類型的左值「NoteData *」
什麼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;
顯然,那裏有一個轉換的問題..如果任何人都可以幫我這個,這將是真棒
(+1)或者編寫一個可以乾淨地處理底層C++對象的生命週期的包裝器。或者讓NoteData成爲Objective-C類的開始。 – 2012-04-20 18:10:26
@FirozeLafeer我同意,這個類不應該用C++編寫,只是爲了皮特的緣故使用C結構。 – 2012-04-20 18:11:53
是的,我實際上已經使用了一個結構.... 並且以這種方式累了...然後我讀了一些地方,也許使用一個類,而不是可能使它更容易處理....那就是我現在... 等等...我可以使用我的C結構....並創建一個向量...嗯,偉大的,應該工作!感謝評論傢伙。我感謝你的時間! – Pavan 2012-04-21 00:17:56