草庐IT

weakSelf

全部标签

ios - 使用 ARC 和 block 时保留循环

根据我的理解,当一个对象方法接收到一个block作为完成参数时,我可以在block中发送“self”:[objectdoMethodWithCompletion:^{[selfdoSomethingWhenThisMethodCompletes]}];但是如果这个对象“保留”了block(保存它以备将来使用)我应该发送一个我自己的“弱”副本:__weak__typeof__(self)weakSelf=self;object.savedBlock=^{[weakSelfdoSomethingWhenThisBlockIsCalledFromWithinObject];};但我也看到了变

ios - 当 block 中的 weakSelf 为 nil 时,什么时候应该添加 strongSelf

通过在block中使用weakSelf,你可以避免retaincycle。但有时你应该保持weakSelf直到blockretain,因此你需要像strongSelf一样使用__weak__typeof__(self)weakSelf=self;dispatch_group_async(_operationsGroup,_operationsQueue,^{__typeof__(self)strongSelf=weakSelf;[strongSelfdoSomething];[strongSelfdoSomethingElse];});我想知道weakSelf何时为nil,然后我们应该

ios - 在 block 内访问 BOOL

我正在尝试在block中使用BOOL设置标志。我这样声明BOOL:@property(nonatomic)BOOLflag;在block内:__strongtypeof(self)strongSelf=weakSelf;if(strongSelf->_flag)我的问题是,如果我这样做:__weaktypeof(self)weakSelf=self;if(weakSelf->_flag)我会得到一个错误:"dereferencinga__weakpointerisnotalloweddietopossiblenullvaluecausedbyaracecondition,assigni

ios - enumerateObjectsUsingBlock 中的 weakSelf?

在以下代码片段(self.searchResults)中调用self时,我是否应该使用weakSelf:[self.restaurantsenumerateObjectsUsingBlock:^(Restaurant*restaurant,NSUIntegeridx,BOOL*stop){if([scopeisEqualToString:@"All"]||[restaurant.nameisEqualToString:scope]){NSRangerange=[restaurant.namerangeOfString:searchTextoptions:(NSCaseInsensiti

objective-c - 引用自身和实例变量的 block

在block中引用“self”(和ivars)而不创建强引用(从而增加引用计数)的正确方法是什么?例如,我发现以下增加了“self”的引用计数:^(idsender){[self.navigationControllerpopViewControllerAnimated:YES];}为了规避上述情况,我一直在做以下事情:__weakWhateverController*weakSelf=self;^(idsender){[weakSelf.navigationControllerpopViewControllerAnimated:YES];};是的,我知道这是伪代码。

iOS - 在 NSURLSession 中重新尝试失败的 NSURLRequests

在我的应用程序中,在我的API类的NSURLSession中可以同时(异步)对我的服务器进行2-4个API调用。为了向我的服务器发出API请求,我必须在每个NSURLRequest的HTTPHeaderField中提供身份验证token。token有效期为一天,如果过了一天就失效了,我需要刷新token。我在API类的以下代码中执行此操作:/*!*@briefsendsarequestasanNSHTTPURLResponse.Thismethodisprivate.*@paramrequestTherequesttosend.*@paramsuccessAblocktobecalle

ios - UIAlertController 仅在 2 秒后关闭

我想在呈现后立即关闭,但只能在2秒后执行。如何做到这一点?这是我从UILabel上的UITapGestureRecognizer调用的方法。-(IBAction)labelTaped:(UITapGestureRecognizer*)sender{if(sender.state==UIGestureRecognizerStateEnded){CGRectframe=CGRectNull;NSString*message=nil;//...//somecode///...if(message){//showinfoalert__weak__typeof(self)weakSelf=sel

ios - 我应该在嵌套 block 中使用 weakSelf 吗?

我正试图正确地避免在ObjectiveC中使用block的保留循环,并且不确定是否具有嵌套block。如果我像这样写一个简单的block:[selfdoSomethingWithBlock:^{[selfdoSomethingElse];}];编译器捕获并警告我这可能会导致保留循环。我将其更改如下以避免循环:__weak__typeof(self)weakSelf=self;[selfdoSomethingWithBlock:^{__strong__typeof(weakSelf)strongSelf=weakSelf;[strongSelfdoSomethingElse];}];当我

ios - 理解SDWebImage下载器

文档不适合我。谁能给我示例代码或实例化SDWebImage下载程序并使用downloadImageWithURL方法的示例? 最佳答案 我已经为此创建了一个类,但下面是我如何使用它:-(void)setImageURL:(NSURL*)imageURL{_imageURL=imageURL;self.image=nil;[self.layerremoveAllAnimations];if(!imageURL){self.image=self.placeHolderImage;return;}__weakSDImageView*wea

ios - 对 block 内弱引用的强引用

为什么必须在block内对弱引用进行强引用?我知道在block中使用弱引用可以避免循环引用。但是为什么又要有强引用弱引用呢?背景:正如Mason所述,这是最佳实践。Iknowtheproperwaytorefertoselfinsideablockistocreateaweakreferenceoutsidetheblock,andthenastrongreferencetothatweakreferenceinsidetheblock[...]示例:__weaktypeof(self)weakSelf=self;void(^someBlock)(id)=^(iddata){typeo