简体   繁体   中英

How to access dictionaries within other methods?

Below is the code I am using to try and access the initialised dictionary with another method:

#import "UserFoodData.h"

@implementation UserFoodData

-(void)loadUserFoodData{
    NSString *appDataPath = [[NSBundle mainBundle] pathForResource:@"UserFoodData"     ofType:@"plist"];
    NSMutableDictionary *userFoodDictionary = [[NSMutableDictionary alloc]     initWithContentsOfFile:appDataPath];

    for (userFoodDictionary in userFoodDictionary){
        NSLog(@"%@", userFoodDictionary);
    }

}

-(void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount{
    [userFoodDictionary setObject:foodName forKey:foodName];

}

@end

The error messages I seem to be getting is: Use of undeclared identifier 'userFoodDictionary'

Now what I believe is wrong is that the compiler believes that there is a possibility that the setName:andCarbs method could but executed before the loadUserFoodData Which is called when the apps main screen has been initialised. Could someone please help me with a work around?

The error messages I seem to be getting is:

Use of undeclared identifier 'userFoodDictionary'

Compiler says:

1. I didn't see any variable like userFoodDictionary locally or instance variable in -(void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount.

2. But you are trying to access it.

 and also your for loop has mistakes. please verify my answer

Compiler is trying to look for userFoodDictionary in "UserFoodData" class. Because you are accessing userFoodDictionary outside of loadUserFoodData method. So in order to access outside of local method you must keep that reference in header file then you can access it any where in class.

@interface UserFoodData : NSObject

@property(nonatomic,retain)NSMutableDictionary *userFoodDictionary;

@end

#import "UserFoodData.h"

@implementation UserFoodData
//@synthesize userFoodDictionary; synthesise is automatically entered by compiler

-(void)loadUserFoodData{
    NSString *appDataPath = [[NSBundle mainBundle] pathForResource:@"UserFoodData"     ofType:@"plist"];

    self.userFoodDictionary = [[NSMutableDictionary alloc]     initWithContentsOfFile:appDataPath];

//Please not down this
    for (id aFood in userFoodDictionary){
        NSLog(@"%@", aFood);
    }

}

-(void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount{
    [self.userFoodDictionary setObject:foodName forKey:foodName];

}

@end

I'm assuming you get the error in the second method that you posted.

- (void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount{
    [userFoodDictionary setObject:foodName forKey:foodName];
}

userFoodDicitionary is not defined in that method. If you want the dictionary to persist between method calls, store it in a @property .

You should really read some introductory material to Objective-C, you could start with this Apple document

There are multiple issues here...

For one, your for loop has a syntax error...not only that, but you're trying to iterate through a dictionary named userFoodDictionary with an iterator variable also named userFoodDictionary . Change that to something like:

for (id dictKey in userFoodDictionary) {
    NSLog(@"%@", dictKey);
}

You're also trying to access a variable out of scope. userFoodDictionary is declared within the scope of loadUserFoodData . After that method completes, the variable is deallocated, so of course you can't access it in setName:andCarbs: . To do that, you need to define it in a wider scope -- make a property, a static variable, an iVar, etc.

There are many lacking OOP concepts here...if you are new to programming, Objective-C is a difficult place to start, and it may throw you for many loops (pun intended).

Try this...

#import "UserFoodData.h"

@implementation UserFoodData

-(void)loadUserFoodData{
    NSString *appDataPath = [[NSBundle mainBundle] pathForResource:@"UserFoodData"     ofType:@"plist"];
    NSMutableDictionary *userFoodDictionary = [[NSMutableDictionary alloc]     initWithContentsOfFile:appDataPath];

    for (userFoodDictionary in userFoodDictionary){
        NSLog(@"%@", userFoodDictionary);
    }

}

-(void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount{
    NSString *appDataPath = [[NSBundle mainBundle] pathForResource:@"UserFoodData"     ofType:@"plist"];
    NSMutableDictionary *userFoodDictionary = [[NSMutableDictionary alloc]     initWithContentsOfFile:appDataPath];
    [userFoodDictionary setObject:foodName forKey:foodName];
}

@end

Let me know if that helps... :)

I think the issue is your NSMutableDictionary should be ivar that is declare NSMutableDictionary in .h as follow

NSMutableDictionary *userFoodDictionary

in .m

-(void)loadUserFoodData{
    NSString *appDataPath = [[NSBundle mainBundle] pathForResource:@"UserFoodData"     ofType:@"plist"];
    userFoodDictionary = [[NSMutableDictionary alloc]     initWithContentsOfFile:appDataPath];

for (userFoodDictionary in userFoodDictionary){
    NSLog(@"%@", userFoodDictionary);
}

}
-(void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount{
    [userFoodDictionary setObject:foodName forKey:foodName];

}

So the issue is in the below line

for (userFoodDictionary in userFoodDictionary){
    NSLog(@"%@", userFoodDictionary);
}

replace it with

for (NSMutableDictionary *userFoodDic in userFoodDictionary){
    NSLog(@"%@", userFoodDic);
}

May this will solve your issue

ok Got the other issue too

Please declare the userFoodDictionary as one of the property too as described by @Sebastian in his answer :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM