使用self in objective c方法

时间:2014-08-21 14:27:40

标签: ios objective-c linked-list self

我试图在Objective C中为LinkedList编写代码,我偶然发现了一个方法的实现。

- (void)removeDuplicates
{
FALinkedList *head = self; //Self is FALinkedList object inherited from NSObject.
if (!head || !head.next) return;

FALinkedList *pre = nil;
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
while (head) {
    if (![dict valueForKey:[NSString stringWithFormat:@"%lu",head.data]]) {
        [dict setObject:[NSNumber numberWithBool:YES]
                 forKey:[NSString stringWithFormat:@"%lu",head.data]];
        pre = head;
    } else {
        pre.next = head.next;
    }
    head = head.next;
}
}

这个方法正在运行,但我不确定我是否可以直接将self分配给方法内的临时对象。

对资源的任何建议/意见/指示都会非常有用。

感谢。

1 个答案:

答案 0 :(得分:1)

  

这个方法正在运行,但我不确定我是否可以直接将self分配给方法内的临时对象。

是的,self只是指向执行该方法的对象的指针。您可以将self分配给本地变量,而不会造成任何问题。