Iphone 除NSDictionary之外的其他类可以访问的所有类属性?有人知道为什么吗?
在过去的几天里,我到处寻找,除了从我头上拔下的几根头发外,几乎没有什么可看的 我试图通过@properties将对象数据从一个类传递到另一个类,所有这些都可以很好地处理我的NSString对象。但我无法访问我的NSDictionary和/或NSMutableDictionary数据。事实上,我曾尝试将我的NSDictionary切换到NSMutableDictionary,在retain和copy之间切换,以及其他一些变体,但都无济于事 这里有一个故障。我要传递的NSDictionary对象是projectMessagesList。我希望这些代码足够简短,但也能提供足够的信息: DBS项目.h 对象类末尾的NSLog正确地打印出控制台中的消息 现在让我们跳到我的另一个类,ViewController: dbsdailViewController.h dbsdailViewController.m 这会正确地打印出我的项目代码,但projectMessagesList my NSDictionary为空Iphone 除NSDictionary之外的其他类可以访问的所有类属性?有人知道为什么吗?,iphone,ios,ios5,Iphone,Ios,Ios5,在过去的几天里,我到处寻找,除了从我头上拔下的几根头发外,几乎没有什么可看的 我试图通过@properties将对象数据从一个类传递到另一个类,所有这些都可以很好地处理我的NSString对象。但我无法访问我的NSDictionary和/或NSMutableDictionary数据。事实上,我曾尝试将我的NSDictionary切换到NSMutableDictionary,在retain和copy之间切换,以及其他一些变体,但都无济于事 这里有一个故障。我要传递的NSDictionary对象是p
所以,除了我的字典,我可以访问所有内容。有人对我的错误有什么建议吗?非常感谢你 更改代码,这是一个非常简单的错误
-(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,因为我的@synthesis设置为_projectMessagesList,但你搞定了。谢谢
@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;
}
#import "DBSProject.h"
#import "DBSMessage.h"
@interface DBSDetailViewController : UIViewController
@property (strong, nonatomic) DBSProject *myProject;
@property (strong, nonatomic) IBOutlet UILabel *clientCode;
@end
- (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]);
}
-(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;
}