简体   繁体   English

如何在其他方法中访问字典?

[英]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' 我似乎收到的错误消息是:使用未声明的标识符'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. 现在我认为是错误的,因为编译器认为setName:andCarbs方法有可能只能在初始化应用程序主屏幕时调用的loadUserFoodData之前执行。 Could someone please help me with a work around? 有人可以帮我解决问题吗?

The error messages I seem to be getting is: 我似乎收到的错误消息是:

Use of undeclared identifier 'userFoodDictionary' 使用未声明的标识符“ userFoodDictionary”

Compiler says: 编译器说:

1. I didn't see any variable like userFoodDictionary locally or instance variable in -(void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount. 1.我没有在本地看到任何变量,例如userFoodDictionary或-(void)setName:(NSString *)foodName和Carbs:(NSNumber *)carbAmount中的实例变量。

2. But you are trying to access it. 2.但是您正在尝试访问它。

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

Compiler is trying to look for userFoodDictionary in "UserFoodData" class. 编译器正在尝试在“ UserFoodData”类中查找userFoodDictionary。 Because you are accessing userFoodDictionary outside of loadUserFoodData method. 因为您要在loadUserFoodData方法之外访问userFoodDictionary。 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. 该方法未定义userFoodDicitionary If you want the dictionary to persist between method calls, store it in a @property . 如果您希望字典在方法调用之间持久存在,请将其存储在@property

You should really read some introductory material to Objective-C, you could start with this Apple document 您应该真正阅读了Objective-C的入门资料,您可以从这份Apple文档开始

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 . 一方面,您的for循环有一个语法错误……不仅如此,而且您正尝试通过一个名为userFoodDictionary的字典进行迭代, userFoodDictionary使用一个迭代器变量也称为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 . userFoodDictionaryloadUserFoodData的范围内声明。 After that method completes, the variable is deallocated, so of course you can't access it in setName:andCarbs: . 该方法完成后,将释放该变量,因此您当然不能在setName:andCarbs:访问它。 To do that, you need to define it in a wider scope -- make a property, a static variable, an iVar, etc. 为此,您需要在更广泛的范围内进行定义-创建属性,静态变量,iVar等。

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). 这里缺少许多OOP概念...如果您是编程的新手,那么Objective-C是一个很难入门的地方,它可能会使您陷入很多循环(双关语)。

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应该是在.h中声明为NSMutableDictionary的ivar,如下所示

NSMutableDictionary *userFoodDictionary

in .m 在.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 :) 请也将userFoodDictionary声明为属性之一,如@Sebastian在其回答中所述:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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