Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a data object class and use its properties by passing this object in Objective C

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 218
    Comment on it

    Access your data using class object.

    Steps to follow:- 1. Create a NSObject class called DataObject. 2. Declare properties as below...

    Write below code in DataObject.h file

    @interface DataObject : NSObject
    
    @property (strong, nonatomic) NSString *userId;
    @property (strong, nonatomic) NSString *companyName;
    @property (strong, nonatomic) NSString *name;
    @property (strong, nonatomic) NSString *imgStr;

    3. Write below code in DataObject.m file

    @synthesize userId, companyName, name, imgStr;
    

    4. In AppDelegate.h create an Array..

    @property (strong, nonatomic) NSMutableArray *arrModelData;

    5. In AppDelegate.m

    @synthesize arrModelData;

    6. In some class where you filled these properties using your Data (Here I am using Web json data example.. ).

     NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:urlData options:0 error:nil];
        DataObject *model = [[DataObject alloc]init];
            [model setName:[dict valueForKey:@"name"]];
            [model setCompanyName:[dict valueForKey:@"companyName"]];
            [model setImgStr:[dict valueForKey:@"imgStr"]];
            [model setUserId:[dict valueForKey:@"userId"]];

    7. And copy model object to arrModelData of AppDelegate

    AppDelegate *objAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                            [appDelegate.arrModelData addObject: model];    

    8. Now you can use these properties where you required. Create and initialize DataObject and access all properties then,

     DataObject *model = [objAppDelegate.arrModelData objectAtIndex:0 ];
        NSLog(@"image string is :  %@", model.imgStr);
        NSLog(@"Name is :  %@", model.name);
        NSLog(@"Company Name is :  %@", model.companyName);
        NSLog(@"UserId is : %@", model.userId);
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: