0

I am trying to implement the countByEnumeratingWithState method in my objective-c class (say MyClass In this method I do an

MyOtherClass *cl = [[MyOtherClass alloc] init];
buffer[count++] = cl;

The reason why I have to allocate objects on the fly is because those objects are stored 'elsewhere'. However, when using this method from an application, it will crash:

for (const MyOtherClass *cl in myClassObj){
    NSLog(@"obj: %@", cl.description);
}

The reason for this is most likely that ARC throws away my MyOtherClass object in countByEnumeratingWithState because the buffer is 'unretained'. How can I make sure the MyOtherClass object 'retains' ?

More relevant information:

  • thread #4: tid = 0x5ca941, 0x0000000101a4cf8b libobjc.A.dylibobjc_msgSend + 11, stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) frame #0: 0x0000000101a4cf8b libobjc.A.dylibobjc_msgSend + 11
4

1 回答 1

0

为什么要使用 const 关键字?const 让您分配对象仅用于初始化,然后在您想尝试更改它时抛出异常。试试看:

for (MyOtherClass *cl in myClassObj){
    NSLog(@"obj: %@", cl.description);
}
于 2013-12-19T10:35:10.117 回答