将保留对象分配给弱属性; 对象将在分配后释放

pen*_*ang 13 iphone ios

我使用了一些源代码:

 KGModalContainerView *containerView = 
     self.containerView = 
         [[KGModalContainerView alloc] initWithFrame:containerViewRect];
Run Code Online (Sandbox Code Playgroud)

它给了我: Assigning retained object to weak property; object will be released after assignment

编辑:

@interface KGModal()
  @property (strong, nonatomic) UIWindow *window;
  @property (weak, nonatomic) KGModalViewController *viewController;
  @property (weak, nonatomic) KGModalContainerView *containerView;
  @property (weak, nonatomic) UIView *contentView;
@end

KGModalContainerView *containerView = 
    self.containerView = 
        [[KGModalContainerView alloc] initWithFrame:containerViewRect];
containerView.modalBackgroundColor = self.modalBackgroundColor;
containerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
                                 UIViewAutoresizingFlexibleRightMargin |
                                 UIViewAutoresizingFlexibleTopMargin |
                                 UIViewAutoresizingFlexibleBottomMargin;
containerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
contentView.frame = (CGRect){padding, padding, contentView.bounds.size};
[containerView addSubview:contentView];
[viewController.view addSubview:containerView];
Run Code Online (Sandbox Code Playgroud)

gra*_*ver 30

我想你的containerView属性是用weak属性声明的.如果您想拥有某个weak属性的属性,则应该已经保留了该属性.这是一个例子:

@property (nonatomic, weak) KGModalContainerView *containerView;
...
-(void)viewDidLoad {
    [super viewDidLoad];
    KGModalContainerView *myContainerView = [[KGModalContainerView alloc] initWithFrame:containerViewRect]; // This is a strong reference to that view
    [self.view addSubview:myContainerView]; //Here self.view retains myContainerView
    self.containerView = myContainerView; // Now self.containerView has weak reference to that view, but if your self.view removes this view, self.containerView will automatically go to nil.

 // In the end ARC will release myContainerView, but it's retained by self.view and weak referenced by self.containerView
}
Run Code Online (Sandbox Code Playgroud)