小编Hen*_*ang的帖子

self.variable和_variable之间的区别,关于KVO

第一个图像使用self.name来改变,第二个图像使用_name来改变.它应该是相同的结果,但是第二个图像输出没有.为什么?

在此输入图像描述

在此输入图像描述

这是代码

#import "ViewController.h"

@interface kvo : NSObject

@property (nonatomic,strong) NSString *name;

@end

@implementation kvo

- (void)change
{
    _name = @"b";
}

@end

@interface ViewController ()

@property (nonatomic, strong) kvo *a1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.a1 = [[kvo alloc] init];
    _a1.name = @"a";
    [self.a1 addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    [_a1 change];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"1");
}
Run Code Online (Sandbox Code Playgroud)

所不同的是self.name_name在变化的方法

编辑:这与"Objective-C中_variable和self.variable之间有什么区别?[复制]"的问题不一样,我知道这是关于getter方法和setter方法的,我的问题是为什么setter方法会触发KVO并且_name = @"b"不会解雇KVO.

objective-c key-value-observing ios swift

6
推荐指数
1
解决办法
528
查看次数

方法[load]是否需要调用[super load]

我知道很多方法需要调用它的超类方法,有些方法不需要,

我正在寻找关于方法混合的方法.它在load方法中初始化,在教程中没有[super load].

我想知道它是错还是没有必要打电话[super load].

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        // When swizzling a class method, use the following:
        // Class class = object_getClass((id)self);

        SEL originalSelector = @selector(pushViewController:animated:);
        SEL swizzledSelector = @selector(flbs_pushViewController:animated:);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

#pragma mark - Method …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c ios method-swizzling

5
推荐指数
1
解决办法
508
查看次数

使用Objective-C和Swift创建内存泄漏

我接受了采访,并被要求使用Objective-C和Swift创建内存泄漏.那么如何使用Objective-C和Swift创建内存泄漏?

memory-leaks objective-c objective-c-runtime ios swift

0
推荐指数
1
解决办法
709
查看次数