나는 연결리스트를 구현하기 위해 노력하고있어에서 개체를 파괴, 그래서 내가 지금 같은 헤더 파일이있는 노드 클래스가 : 다른 클래스에서 다음목적-C
@interface Node : NSObject
@property(nonatomic,assign)int data;
@property(nonatomic,strong) Node *right;
@property(nonatomic,strong) Node *left;
@end
을, 나는 그들에게 할당하고있어 방법을 여기에 자신을 것
Node *node0 = [[Node alloc]init];
Node *node1 = [[Node alloc]init];
Node *node2 = [[Node alloc]init];
Node *node3 = [[Node alloc]init];
Node *node4 = [[Node alloc]init];
node0.data = 1;
node1.data = 2;
node2.data = 5;
node3.data = 5;
node4.data = 3;
node0.right = node1;
node1.right = node2;
node2.right = node3;
node3.right = node4;
node4.right = NULL;
[self removeNodeWithValue:node0 value:5];
NSLog(@"node %d, %d, %d, %d, %d", node0.data, node1.data, node2.data, node3.data, node4.data);
을 그리고 : 다음 주어진 값의 모든 발행 수를 파괴하는 방법을 호출
-(void)removeNodeWithValue:(Node *)head value:(int)value
{
Node *toDelete;
while (head != NULL) {
if (head.data == value)
{
toDelete = head;
head = head.right;
toDelete = nil;
}
else
{
head = head.right;
}
}
}
==> 1, 2, 5, 5, 3
I toDelete = nil
을 toDelete.data = 4
으로 변경하면 출력이 ==> 1, 2, 4, 4, 3
이므로 인스턴스를 변경할 수 있음을 알 수 있습니다. 내 질문은, 어떻게 그 인스턴스를 파괴합니까? 감사.
다음을 참조하십시오 : http://stackoverflow.com/questions/14508134/whats-the-correct-way-destroy-different-kinds-of-objects-in-objective-c?rq=1 –
그들은 객체를 무효로 만들지 만 내 코드에서는 작동하지 않는 것을 볼 수 있습니다. –