Search code examples
objective-cnsobject

Accessing Property from another NSObjectClass


I am using 2 nsobject class ,

First nsobject class some property and it holds some values. In Second NSObject class i want to access the first nsobject class properties. I have tried little bit ,it showing null values Here is my code My First NSObject class FieldData.M

-(instancetype)initWithDictionary:(NSDictionary *)dictFieldData
{
if (self = [super init]) {

    ///set the field id
    self.fieldId = [[dictFieldData objectForKey:@"f_id"]intValue];
    self.display = [[dictFieldData objectForKey:@"f_display"]boolValue];
    self.fieldLength = [[dictFieldData objectForKey:@"f_length"]intValue];
    self.mandatory = [[dictFieldData objectForKey:@"f_mandatary"]boolValue];
    self.strFieldLabel = [dictFieldData objectForKey:@"f_label"];

    NSString *strFieldAttrib = [dictFieldData objectForKey:@"f_attribute"];
    if ([strFieldAttrib.lowercaseString isEqualToString:@"alpha"]) {

        self.fieldAttribute = FieldAttributeAlpha;
    }
    else if ([strFieldAttrib.lowercaseString isEqualToString:@"numeric"]) {

        self.fieldAttribute = FieldAttributeNumeric;
    }
    else if ([strFieldAttrib.lowercaseString isEqualToString:@"alpha_numeric"]) {

        self.fieldAttribute = FieldAttributeAlphaNumeric;
    }

    }return self;
    }

****Second NSObjectClass** SiteData.m**

-(instancetype)initWithDictionary:(NSDictionary *)dictSiteData
{

self.fieldData = [FieldData new];

if (self = [super init]) {


    self.siteId = [[dictSiteData objectForKey:@"s_id"]intValue];

    self.siteName = [dictSiteData objectForKey:@"site_name"];

    NSLog(@"%d",self.fieldData.fieldId);





}

return self;
}

Please anyone helpme to do this Please what i am doing wrong Thanks in Advance !!!


Solution

  • Please write this code in Appdelegate declare this property. After that SetValue your Dictionary Data

    @property(strong,nonatomic)NSDictionary * dictFieldData;
    

    Write this code My First NSObject class FieldData.M Create object in ViewdidLoad

     AppDelegate *appdelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate]);
    
    
    ///set the field id
    self.fieldId = [[appdelegate.dictFieldData SetValue:(NSString*) objectForKey:@"f_id"]intValue];
    
    ///Get the field id
    self.siteId = [[appdelegate.dictSiteData Valueforkey:@"s_id"]intValue];
    
    NSLog(@"%d",appdelegate.dictSiteData);
    

    Check this post answer for Accessing Property from another NSObjectClass