Objective-C Associative References(續)

來源:互聯網
上載者:User

Objective-C Associative References(關聯引用) 一篇文章,今天在看KVC方面的文章,所以就想到結合一起再講明白一點,更加結合實際一點

CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];keyPosi.path=path;keyPosi.delegate=self;[label.layer addAnimation:animation forKey:@"position"];

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{}

[animation setValue:view forKey:@"xx"];
然後我就修改回調方法:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    if (flag) {        UIView *vie=[anim valueForKey:@"xx"];        [vie removeFromSuperview];    }}
這樣正確嘛?

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0xf65f090> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key xx.'

Invoked by setValue:forKey: when it finds no property - ()setValue:()value forUndefinedKey:(NSString *DiscussionSubclasses can   method to handle the request  some other way. The  implementation raises an NSUndefinedKeyException.

#import <objc/runtime.h>@interface NSObject (KVC)- (id)valueForUndefinedKey:(NSString *)key;- (void)setValue:(id)value forUndefinedKey:(NSString *)key;@end@implementation NSObject(KVC)- (id)valueForUndefinedKey:(NSString *)key{    return objc_getAssociatedObject(self, key);}- (void)setValue:(id)value forUndefinedKey:(NSString *)key{    if ([value isKindOfClass:[NSString class]]) {        objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_COPY_NONATOMIC);    }else{        objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);    }}@end

這樣就實現了目的。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.