ios – 具有强引用的Object属性变为null

ios – 具有强引用的Object属性变为null,第1张

概述在具有强引用的另一个对象(BWMasterViewController中的dataController)的属性上设置的数组的键变为null.我不明白为什么. BWMasterViewController 标题: #import <UIKit/UIKit.h>@class BWBirdSightingDataController;@interface BWMasterViewControll 在具有强引用的另一个对象(BWMasterVIEwController中的dataController)的属性上设置的数组的键变为null.我不明白为什么.

BWMasterVIEwController

标题:

#import <UIKit/UIKit.h>@class BWBirdSightingDataController;@interface BWMasterVIEwController : UItableVIEwController@end

执行:

#import "BWMasterVIEwController.h"#import "BWBirdSightingDataController.h"#import "Bird.h"#import "BWWebvIEwController.h"@interface BWMasterVIEwController()@property (strong,nonatomic) BWBirdSightingDataController *dataController;@property (copy,nonatomic) Nsstring *test;@end@implementation BWMasterVIEwController- (voID)awakeFromNib{    [super awakeFromNib];    NSLog(@"awake from nib");    self.dataController = [[BWBirdSightingDataController alloc] init];    self.test = @"Test var";    NSLog(@"test: %@",self.test);    Bird *bird = [self.dataController objectInListAtIndex:0];    NSLog(@"bird object: %@",bird);    NSLog(@"bird name: %@",bird.name)}- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    NSLog(@"vIEw dID load");    NSLog(@"test: %@",bird.name)}// ....

BWBirdSightingDataController

标题:

#import <Foundation/Foundation.h>@class Bird;@interface BWBirdSightingDataController : NSObject- (NSUInteger)countOfList;- (Bird *)objectInListAtIndex:(NSUInteger)theIndex;@end

执行:

#import "BWBirdSightingDataController.h"#import "BWDataController.h"#import "Bird.h"@interface BWBirdSightingDataController ()-(voID)initializeDefaultDataList;@property (nonatomic,copy) NSMutableArray *birds;@end@implementation BWBirdSightingDataController- (ID)init{    if (self = [super init]) {        [self initializeDefaultDataList];        return self;    }    return nil;}- (voID)initializeDefaultDataList{    BWDataController *dataController = [[BWDataController alloc] init];    NSEntityDescription *birdsEntity = [NSEntityDescription                                        entityForname:@"Bird"                                        inManagedobjectContext:dataController.managedobjectContext];    NSFetchRequest *fetchBirds = [[NSFetchRequest alloc] init];    [fetchBirds setEntity:birdsEntity];    NSError *fetchError = nil;    NSArray *birdsObjects = [dataController.managedobjectContext                             executeFetchRequest:fetchBirds                             error:&fetchError];    self.birds = [birdsObjects mutablecopy];}- (NSUInteger)countOfList{    return [self.birds count];}- (Bird *)objectInListAtIndex:(NSUInteger)theIndex{    return self.birds[theIndex];}@end

Bird是基于CoreData实体的NSManagedobject.

标题:

#import <Foundation/Foundation.h>#import <CoreData/CoreData.h>@interface Bird : NSManagedobject@property (nonatomic,retain) NSDate * date;@property (nonatomic,retain) Nsstring * name;@property (nonatomic,retain) Nsstring * location;@property (nonatomic,retain) Nsstring * image;@property (nonatomic,retain) Nsstring * url;@end

执行:

#import "Bird.h"@implementation Bird@dynamic date;@dynamic name;@dynamic location;@dynamic image;@dynamic url;@end

产量

当我运行它时,输出:

2013-05-31 11:36:47.824 BirDWatching[69565:c07] awake from nib2013-05-31 11:36:47.834 BirDWatching[69565:c07] test: Test var2013-05-31 11:36:47.834 BirDWatching[69565:c07] bird object: <Bird: 0x8545ec0> (entity: Bird; ID: 0x8545040 <x-coredata://D24A664F-8E8F-4AF0-891C-098C8A7DD860/Bird/p1> ; data: <fault>)2013-05-31 11:36:47.835 BirDWatching[69565:c07] bird name: Pigeon2013-05-31 11:36:47.839 BirDWatching[69565:c07] vIEw dID load2013-05-31 11:36:47.840 BirDWatching[69565:c07] test: Test var2013-05-31 11:36:47.840 BirDWatching[69565:c07] bird object: <Bird: 0x8545ec0> (entity: Bird; ID: 0x8545040 <x-coredata://D24A664F-8E8F-4AF0-891C-098C8A7DD860/Bird/p1> ; data: <fault>)2013-05-31 11:36:47.840 BirDWatching[69565:c07] bird name: (null)

如您所见,在vIEwDIDLoad函数中,bird.name已变为null.怎么会?我宣称该物业是一个强有力的参考.

我在vIEwDIDLoad中定义self.dataController时遇到同样的问题,然后在prepareForSegue中它将为null,我也需要它.

解决方法 您可以使用本地BWDataController * dataController在initializeDefaultDataList中创建Bird对象. dataController在此方法结束时自动释放,这可能意味着托管对象上下文data​​Controller.managedobjectContext也不再存在.

但是,托管对象只能存在于创建它的上下文中.如果上下文被破坏,则恰好发生这种情况:访问所有属性将返回nil.

可能的解决方案:

>使dataController成为BWBirdSightingDataController的强属性,而不是局部变量.>在整个应用程序中使用共享的托管对象上下文.

总结

以上是内存溢出为你收集整理的ios – 具有强引用的Object属性变为null全部内容,希望文章能够帮你解决ios – 具有强引用的Object属性变为null所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1114964.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存