HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 具有强引用的Object属性变为null大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在具有强引用的另一个对象(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 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中它将为null,我也需要它.

解决方法

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

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

可能的解决方案:

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。