-1

I am making a NSObjectClass that has a method in it that returns self.

This is what it looks like roughtly

storageclass.h

// storageclass vars go here

- (storageclass)assignData:(NSDictionary *)dictionary;

storageclass.m

//@synthesise everything

- (storageclass)assignData:(NSDictionary *)dictionary {

//assign values from dictionary to correct var types (i.e. NSString, Int, BOOL)
//example
 Side = [dictionary valueForKey:@"Side"];
return self;

}

Then what I want to do is use this class by passing a NSDictionary var through its method to return a object of type storageclass that I can then use to access the vars using dot notation.

this is how I am trying to access this class at the moment

accessorViewController.h

storageclass *store;

@property (strong, nonatomic) storageclass *store;

accessorViewController.m

@synthesize store;

- (void)getstoreready {

[store assignData:someDictionary];

nslog(@"%@", store);

}

this NSLog returns nothing and in the debugger all of stores class vars are empty showing nothing has been assigned. I am 100% positive the dictionary vars being used in the assignData method have the correct valueForKey values.

I think it has something to do with how I am using it here [store assignData:someDictionary]; how do i catch the turned data so I can use it?

any help would be appreciated.

HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • okay I will try this now and let you know how I go.. but jsut to reiterate after I have initialized it I call it like **[store assignData:someDictionary]** and then store will have the values avalible? – HurkNburkS Jun 19 '13 at 05:04
  • Yeah, I explained it a bit better in the answer – Zen Jun 19 '13 at 05:05

2 Answers2

1
In User.h

    @interface User : NSObject

    @property (nonatomic, copy)   NSString *name;

    - (id)initWithDictionary:(NSDictionary *)dictionary;

    + (NSArray *)usersFromArray:(NSArray *)array;

    @end

In User.m
    - (id)initWithDictionary:(NSDictionary *)dictionary
    {
        self = [super init];
        if (self) {
            if (dictionary)
            {
                self.name = dictionary[@"kUserName"];
            }
        }
            return self;
    }

    + (NSArray *)usersFromArray:(NSArray *)array
    {
        NSMutableArray *users = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            User *user = [[User alloc]initWithDictionary:dict];
            [users addObject:user];
        }
        NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                     ascending:YES];
        return [users sortedArrayUsingDescriptors:@[descriptor]];
    }

In ViewController.m

import "User.h"

    self.currentArray = [User usersFromArray:array];
Vivek Sehrawat
  • 6,560
  • 2
  • 26
  • 39
1

The store object is never initialized so it will be nil thats obvious isn't it. Initialize the store object first, then call its instance methods onto it. And by doing that, you'll have a storageclass object which is properly assigned with some dictionary already.

And if you want to have a storageclass object like your code shows, you should make your (storageclass)assignData:(NSDictionary *)dictionary method a class method instead of an instance method by putting a + sign

+(storageclass*)assignData:(NSDictionary *)dictionary;

Then properly initialize it and assign the data (dictionary to variables) accordingly and return it to the caller. For example :- in .m file

+(storageclass*)assignData:(NSDictionary *)dictionary{
        storageclass *test = [[storageclass alloc] init];
        if (test) {
           test.someDict = dictionary;
        }
    return test;
}

Then use this class method in your view controller as

- (void)getstoreready {
    store =  [storageClass assignData:someDictionary];
    nslog(@"%@", store);
}

Also Do follow the naming convention for classes and instances. A class's name must start with a capital letter only and the opposite for any class instances.

Zen
  • 3,047
  • 1
  • 20
  • 18
  • okay I have done the things you have outlined, however I am getting errors on the variables in my storageClass **Instance variable 'manu' accessed in class method** – HurkNburkS Jun 19 '13 at 05:32
  • @HurkNburkS What did you do in your class method declaration. I edited for the class method declaration. – Zen Jun 19 '13 at 05:35
  • I got rid of that instance var problem, I missed the point where you used **test.someDict** I was missing the classname **test** I have not got it looking exatly like you have above how ever I am getting an error **cannot init a class object** I would like to know where **store** came from in your method getstoreready.. – HurkNburkS Jun 19 '13 at 05:46
  • store is the property you declared in your view controller file. – Zen Jun 19 '13 at 05:48
  • Ok i missed something, now check the class method definition. – Zen Jun 19 '13 at 05:51
  • whoops.. had a brain fade there :P:P So currently when I call "store = [storageClass assignData:someDictionary];" the thread enters that then when it reaches the line with the init I get this error ** *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[storageclass<0xf902c> init]: cannot init a class object.'** – HurkNburkS Jun 19 '13 at 05:53
  • @HurkNburkS Its because the faulty class method definition. I corrected it. – Zen Jun 19 '13 at 06:01