[self.foo setBlock:^{
[self doSomething]; // causes warning
}];
Run Code Online (Sandbox Code Playgroud)
与
[self.foo setBlock:^{
self.bar = baz; // does not cause warning
}];
Run Code Online (Sandbox Code Playgroud)
警告是" self在这个区块中强势捕获可能会导致保留周期."
两者都不会导致强烈的参考?
memory memory-management objective-c ios automatic-ref-counting
假设我有一个分支A,从那里我分支B.我在A上做了一堆修改,然后结账B并做了一个git pull.现在我对B进行了更改但是意识到它应该已经在A中.如果我现在尝试git checkout A,我得到"您对以下文件的本地更改将被"结帐"覆盖到我触摸的文件.
如果我刚刚在B中执行git pull并且之后没有在A中触及该文件,为什么我的更改会被覆盖?
我提出了一个模态视图控制器.在这种模式视图控制器viewDidLoad,我似乎无法找到父VC ....任何引用parentViewController,presentingViewController等等.如果这还没有设置,我怎么得到它的参考?
说我有The Dark Knight Rises at 7:45pm,我需要将其放入固定宽度的UILabel(适用于iPhone).我怎么能把那个截形变成"黑暗骑士Ris ......在晚上7:45",而不是"黑暗骑士在7:4升起......"?
您必须在addObserverForName指定的任何对象之前调用removeObserver:或removeObserver:name:object:object:queue:usingBlock:is deallocated
为什么在我正在观察的通知被解除分配之前停止观察是否重要?我理解为什么我作为观察者需要停止观察我是否会消失而且块依赖于我的存在,但我不明白为什么被观察物体的寿命很重要.我误解了这个吗?
假设我有一个采用CLLocationCoordinate2D的方法.我无法将nil直接传递给代码中的nil; 编译器抱怨.但是,以下编译.那么在运行时会发生什么?
CLLocation* loc = nil;
[self passMeCoordinates:loc.coordinate];
Run Code Online (Sandbox Code Playgroud) 我知道ARC中的__block保留了变量.然后,在分配变量之前访问块内的变量时,可以使用此方法,如下所示:
__block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notif){
// reference the observer here. observer also retains this block,
// so we'd have a retain cycle unless we either nil out observer here OR
// unless we use __weak in addition to __block. But what does the latter mean?
}];
Run Code Online (Sandbox Code Playgroud)
但是我在解析这个问题时遇到了麻烦.如果__block导致观察者被块保留,那么有效地强弱都意味着什么呢?这是__weak做什么的?