最近知识点 1

   今天是个好日子,入住新地方,深夜发帖纪念一下^^

    前段时间面试,有些知识点后知后觉,总结一二。

1.delegate和notification的区别?

    之前看文档的main difference,跟我以前想的差不多,它们只是功能上的区别,一对一和一对多:

But there are differences between notification and delegation, and these differences dictate what these mechanisms should be used for. As noted earlier, the main difference between the notification model and the delegation model is that the former is a broadcast mechanism whereas delegation is a one-to-one relationship. Each model has its advantages;

    最近细看文档,其实不然,估计面试时人家想考的就是这个:

But the one-to-one model of delegation has its advantages too. A delegate is given the opportunity to affect an event by returning a value to the delegating object. A notification observer, on the other hand, must play a more passive role; it can affect only itself and its environment in response to the event. Notification methods must have the following signature:

 

- (void)notificationHandlerName:(NSNotification *);

 

This requirement precludes the observing object from affecting the original event in any direct way. A delegate, however, can often affect how the delegating object will handle an event.

    简单来说delegate能affect delegating object,而通知则是发了就发了,啥都没返回。

2.property自定义实现?

    我以为只是setter要注意,if判断之后根据property处retain或copy。而getter只是简单的返回变量,其实可能会有问题,据文档描述:

However, there is still a potential danger with this approach. What if a client is using the object returned by the getter accessor and meanwhile the setter accessor autoreleases the old NSString object and then, soon after, that object is released and destroyed? The client object’s reference to the instance variable would no longer be valid.

    乍一看不晓得啥意思,写了代码测试才了解:

- (void)logTitle {
    NSLog(@"title %@",title);
}

- (void)testTitle {
    a = [[ClassA alloc] init];
    
    NSString *str = [[NSString alloc] initWithFormat:@"%d",888];
    a.title = str;
    [str release];
    
    title = a.title; 
            
    [a release];

    [self logTitle];
    [self logTitle];
    //[self performSelector:@selector(logTitle) withObject:nil afterDelay:0];
}

     如上,ClassB的testTitle使用A对象,A内部自定义title的set/get,get是简单返回title变量,当[a release];之后,a会dealloc,从而title被release,此时B调logTitle就会bad_access(调两次是因为title release后并没有立即释放,调第二次才error,跟输出两次retainCount类似,第一次还能输出1,第二次error,其中原因未明),因为B中只是简单把A返回的title赋值给B中的变量。要解决这个问题,要么B把title retain一次,要么用文档的方法,getter返回[[title retain] autorelease]。

 

     至此,OK^^

猜你喜欢

转载自hua397.iteye.com/blog/1859000