【发布时间】:2012-02-02 04:00:12
【问题描述】:
在过去的几天里,我进行了相当广泛的搜索,除了从我头上拔出的几根头发外,几乎没有什么可显示的。
我正在尝试通过@properties 将我的对象数据从一个类传递到另一个类,并且我的 NSString 对象一切正常。但我无法访问我的 NSDictionary(和/或 NSMutableDictionary)数据。事实上,我尝试将我的 NSDictionary 切换为 NSMutableDictionary,在保留和复制之间切换,以及其他一些变体都无济于事。
这是一个细分。我传递的 NSDictionary 对象是 projectMessagesList。我希望代码足够简短,但要提供足够的信息:
DBSProject.h
@interface DBSProject : NSObject <NSCoding>
@property (nonatomic, retain) NSDictionary *projectMessagesList;
@end
DBSProject.m
@implementation DBSProject
@synthesize projectMessagesList=_projectMessagesList;
-(id)init {
self = [super init];
if (self) {
[self setProjectCode:@"CODE"];
NSMutableDictionary *projectMessagesList = [[NSMutableDictionary alloc] init];
DBSMessage *message = [[DBSMessage alloc] init];
[message setMessageDate:[NSDate date]];
[message setMessageText:[NSString stringWithFormat:@"This is a message"]];
[projectMessagesList setObject:message forKey:@"msg"];
NSLog(@"%@", [projectMessagesList objectForKey:@"msg"]);
}
}
return self;
}
我的对象类末尾的 NSLog 在控制台中正确打印出“这是一条消息”。
现在让我们跳到我的另一个类,一个 ViewController:
DBSDetailViewController.h
#import "DBSProject.h"
#import "DBSMessage.h"
@interface DBSDetailViewController : UIViewController
@property (strong, nonatomic) DBSProject *myProject;
@property (strong, nonatomic) IBOutlet UILabel *clientCode;
@end
DBSDetailViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *clientProjectString = [NSString stringWithFormat:@"%@ %@", self.myProject.clientCode, self.myProject.projectCode];
self.title = self.myProject.projectCode;
self.projectCode.text = clientProjectString;
self.projectDescription.text = self.myProject.projectDescription;
self.projectBudget.text = self.myProject.projectBudget;
self.projectDueDate.text = self.myProject.projectDueDate;
DBSMessage *tmpMessage = [[DBSMessage alloc] init];
tmpMessage = [self.myProject.projectMessagesList objectForKey:@"msg"];
NSLog(@"%@", self.myProject.projectCode);
NSLog(@"%@", [tmpMessage messageText]);
}
这会正确打印出我的 projectCode,但 projectMessagesList(我的 NSDictionary)是(null)。
所以,我可以访问除我的 NSDictionary 之外的所有内容。有人对我的错误有什么建议吗?非常感谢!
【问题讨论】: