All class properties accessible from another class EXCEPT NSDictionary? Anyone know why?
Change your code, it's a very simple bug
-(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;
}
Related videos on Youtube
Scott Sullivan
Updated on November 21, 2022Comments
-
Scott Sullivan over 1 year
I've searched pretty far and wide for the past several days and have little to show except a few hairs pulled from my head.
I'm trying to pass my object data from one class to another via @properties, and all is working well with my NSString objects. But I cannot access my NSDictionary (and/or NSMutableDictionary) data. In fact, I've tried switching my NSDictionary to an NSMutableDictionary, switching between retain and copy, and a few other variations to no avail.
Here's a breakdown. My NSDictionary object I'm passing is projectMessagesList. I hope this is enough of the code to be brief, yet, provide enough information:
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; }
The NSLog at the end of my object's class correctly prints out "This is a message" in the console.
Now let's jump to my other class, a 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]); }
This properly prints out my projectCode, but the projectMessagesList (my NSDictionary) is a (null).
So, I'm able to access everything except my NSDictionary. Would anyone have any suggestions as to my error? Thank you so much!
-
Scott Sullivan about 12 yearsSlaps hand on face. Wow. Thank you. That makes perfect sense and explains why I missed it. I was looking way too deep and missed the obvious. (I did change it to be: "self.projectMessageList" since I have my @synthesize set to _projectMessagesList, but you nailed it. THANKS!