【问题标题】:All class properties accessible from another class EXCEPT NSDictionary? Anyone know why?除了 NSDictionary 之外,所有类属性都可以从另一个类访问吗?有谁知道为什么?
【发布时间】: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 之外的所有内容。有人对我的错误有什么建议吗?非常感谢!

【问题讨论】:

    标签: iphone ios ios5


    【解决方案1】:

    更改你的代码,这是一个非常简单的错误

    -(id)init {
        self = [super init];
        if (self) {
            [self setProjectCode:@"CODE"];
            //you are not assigning the object into ivar..you are creating a local object
            //NSMutableDictionary *projectMessagesList = [[NSMutableDictionary alloc] init];
            //into
            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;
    }
    

    【讨论】:

    • 拍拍脸。哇。谢谢你。这很有意义,并解释了我为什么错过它。我看得太深了,错过了明显的东西。 (我确实将其更改为:“self.projectMessageList”,因为我将 @synthesize 设置为 _projectMessagesList,但您成功了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    相关资源
    最近更新 更多