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

我使用了一些源代码:

KGModalContainerView *containerView = self.containerView = [[KGModalContainerView alloc] initWithFrame:containerViewRect]; 

它给了我: Assigning retained object to weak property; object will be released after assignment 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]; 

我想你的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 } 

我在目标C中作为初学者2美分:

提供警告的线的右侧,

 [[KGModalContainerView alloc] initWithFrame:containerViewRect] 

在堆中创建一个对象,此时任何指针都不会引用该对象。 然后将此对象分配给self.containerView 。 因为self.myContainerView很弱,所以赋值不会增加右侧创建的对象的引用计数。 因此,当赋值完成时,对象的引用计数仍为0,因此ARC立即释放该对象。