gpt4 book ai didi

macos - 实例变量(NSString)在自定义类中不会改变

转载 作者:行者123 更新时间:2023-12-03 17:49:55 25 4
gpt4 key购买 nike

我创建了一个类来控制 NSOutlineView,并且该类使用通知与我的 AppDelegate 进行通信。控制outlineview行为的类是使用awakefromnib初始化的,以便立即添加标题(我的意愿),而稍后通过调用此类的方法由子项填充。一切正常,但是什么时候为我的 AppleDelegate 创建通知的时刻我发现实例变量为 null,而在初始调用时则正常。

    @interface MyClass : NSObject <NSApplicationDelegate, NSTextViewDelegate, NSTextFieldDelegate, NSOutlineViewDelegate, NSOutlineViewDataSource>
{
NSString * _plistPath;
}

@implementation MyClass

- (void)awakeFromNib {
static dispatch_once_t once;
dispatch_once(& once, ^{
self.Outline.delegate = self;
self.Outline.dataSource = self;
self.Outline.floatsGroupRows = NO;
[_treeController addObject: @{@"title": @"Model list", @"isLeaf": @(NO)}.mutableCopy];
[self.Outline expandItem:[self.Outline itemAtRow:0]];
[self.Outline selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];

// Enable Drag and Drop
[self.Outline registerForDraggedTypes: [NSArray arrayWithObject: @"public.text"]];
});
}

- (void)loadPlist:(NSDictionary *)dict path:(NSString *)path
{
_plistPath = path;
NSLog(@"at loadPlist _plistPath = %@, self is = %@", _plistPath, self); // here is ok!
}

// Here all the NSOutlineViewDelegate methods
// .....

// finally after editing value in the interface using the outline view
// and some field attacched to the tree controller..
// I need to notificate AppleDelegate for the changes made to the plist,
//reindicating the path stored in _plistPath variable..but:

- (void)update
{

NSMutableArray *List = [NSMutableArray array];
NSInteger rows = [_Outline numberOfRows];
NSInteger i;
i = 0;
while (i != rows) {
id obj = [_Outline itemAtRow:i];
if (i != 0) {
Tree *entry = (Tree *)(((NSTreeNode *)obj).representedObject);

if (entry.title.length > 0 && ![entry.title isEqualToString:kNullString]
&& entry.size.length > 0 && ![entry.size isEqualToString:kNullString]
&& entry.model.length > 0 && ![entry.model isEqualToString:kNullString]
&& entry.year.length > 0 && ![entry.year isEqualToString:kNullString]) {

NSMutableDictionary *childDict = [NSMutableDictionary dictionary];
[childDict setObject:entry.title forKey:@"Name"];
[childDict setObject:entry.size forKey:@"Size"];
[childDict setObject:entry.model forKey:@"Model"];
[childDict setObject:entry.year forKey:@"Year"];

[List addObject:childDict];
}
}
i++;
}

NSMutableDictionary *uf = [NSMutableDictionary dictionary];
NSLog(@"at update _plistPath = %@, self is = %@", _plistPath, self); // this time here is nil....

[uf setObject:_plistPath forKey:@"path"]; // here crash because _plistPath is nil
[uf setObject:List forKey:@"dictionary"];

NSNotification *note = [NSNotification notificationWithName:@"UpdatePlist" object:_Outline userInfo:uf];
[[NSNotificationCenter defaultCenter] postNotification:note];

}

基本上第二次我需要的“_plistPath”值为nil(我有一些非常具体的原因将原始路径发送到MyClass然后将其返回给发送者(即AppleDelegate))并且我声明了更多实例变量在 MyClass 中效果很好..所以我不明白为什么。有什么建议吗?

编辑根据 @matt 的要求,我更改了 NSLog,包括“self”

那么输出是:

2015-05-11 22:06:36.433 TestApp[24990:56140] at loadPlist _plistPath = /Volumes/DATI/test.plist, self is = <MyClass: 0x600000127760>
2015-05-11 22:07:08.552 TestApp[24990:56140] at update _plistPath = (null), self is = <MyClass: 0x608000125c80>

最佳答案

您的日志显示了问题:

at loadPlist _plistPath = /Volumes/DATI/test.plist, 
self is = <MyClass: 0x600000127760>
at update _plistPath = (null),
self is = <MyClass: 0x608000125c80>

这是 MyClass 的两个不同实例。但是 plistPath 是一个实例变量 - 因此它可以完全合理地在一个实例中具有一个值,而在另一个实例中为零。

关于macos - 实例变量(NSString)在自定义类中不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30176185/

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