我最近将c数组更改为NSMutable数组,因为我遇到了很多内存问题。但它起作用,但从
开始/* If two starttimes are within interval millisec, make them the same */
for (int i = 0; i < [starttimes count] - 1; i++) {
if ([[starttimes objectAtIndex:i+1] integerValue] - [[starttimes objectAtIndex:i] integerValue] <= interval) {
[starttimes insertObject: [starttimes objectAtIndex:i] atIndex:(i+1)];
}
}
CPU使用率接近100%。我不知道发生了什么事?逻辑很简单。它只是从这个NSMutable数组中获取两个元素,然后进行比较和插入。顺便说一句,这段代码永远不会停止。它持续运行3秒,然后我的应用程序崩溃。
(TimeSignature*)time {
/* Get all the starttimes in all tracks, in sorted order */
NSInteger initsize = 1;
if ([tracks count] > 0) {
MidiTrack *track = [tracks objectAtIndex:0];
initsize = [track.notes count];
initsize = initsize * [tracks count]/2;
}
NSMutableArray* starttimes = [[NSMutableArray alloc]initWithCapacity:initsize];
for (int tracknum = 0; tracknum < [tracks count]; tracknum++) {
NSLog(@"tracknum is %d",tracknum);
MidiTrack *track = [tracks objectAtIndex:tracknum];
NSLog(@"WE ARE HERE %ld",(long)initsize);
for (int j = 0; j < [track.notes count]; j++) {
MidiNote *note = [track.notes objectAtIndex:j];
NSLog(@"%@",note);
[starttimes addObject:[NSNumber numberWithInteger:note.startTime]];
}
}
/* Notes within "millisec" milliseconds apart should be combined */
int interval = time.quarter * millisec * 1000 / time.tempo;
/* If two starttimes are within interval millisec, make them the same */
for (int i = 0; i < [starttimes count] - 1; i++) {
if ([[starttimes objectAtIndex:i+1] integerValue] - [[starttimes objectAtIndex:i] integerValue] <= interval) {
[starttimes insertObject: [starttimes objectAtIndex:i] atIndex:(i+1)];
}
}
非常感谢
答案 0 :(得分:2)
一般Objective-C建议:阅读方法名称。他们告诉你想要这些方法。
您误解了-insertObject:
。 insertObject
做的是... 插入对象。它没有做什么:替换一个对象。每次拨打insertObject
时,您都会增加数组的大小。实际上,如果if
条件通过一次,你就会创建一个无限循环。
你可能想要-replaceObjectAtIndex:withObject:
。与insertObject:
一样,其名称将与名称相同。