gpt4 book ai didi

ios - 具有强引用的对象属性变为空

转载 作者:可可西里 更新时间:2023-11-01 03:47:19 25 4
gpt4 key购买 nike

在具有强引用的另一个对象(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: %@", self.test);
Bird *bird = [self.dataController objectInListAtIndex:0];
NSLog(@"bird object: %@", bird);
NSLog(@"bird name: %@", 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 nib
2013-05-31 11:36:47.834 BirdWatching[69565:c07] test: Test var
2013-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: Pigeon
2013-05-31 11:36:47.839 BirdWatching[69565:c07] view did load
2013-05-31 11:36:47.840 BirdWatching[69565:c07] test: Test var
2013-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 中它将为空,我也在那里需要它。

最佳答案

您使用本地 BWDataController *dataControllerinitializeDefaultDataList 中创建 Bird 对象。 dataController 在此方法结束时自动释放,这可能意味着托管对象上下文 dataController.managedObjectContext 也不再存在。

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

可能的解决方案:

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

关于ios - 具有强引用的对象属性变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16853521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com