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

时间:2014-01-24 04:36:35

标签: ios iphone objective-c methods dictionary

下面是我用来尝试使用另一种方法访问初始化字典的代码:

#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

我似乎得到的错误消息是: 使用未声明的标识符'userFoodDictionary'

现在我认为错误的是编译器认为setName:andCarbs方法有可能在loadUserFoodData之前执行,而在启动应用程序主屏幕时调用loadUserFoodData。有人可以帮我解决一下吗?

6 个答案:

答案 0 :(得分:3)

我似乎得到的错误消息是:

使用未声明的标识符'userFoodDictionary'

编译说:

<强> 1。我没有看到像userFoodDictionary本地或实例变量这样的变量 - (void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount。

<强> 2。但是你正试图访问它。

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

编译器试图在“UserFoodData”类中查找userFoodDictionary。因为您在loadUserFoodData方法之外访问userFoodDictionary。因此,为了在本地方法之外访问,您必须在头文件中保留该引用,然后您可以在类中的任何位置访问它。

@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

答案 1 :(得分:0)

我假设您在发布的第二种方法中收到了错误。

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

userFoodDicitionary未在该方法中定义。如果您希望字典在方法调用之间保持不变,请将其存储在@property

你应该真正阅读Objective-C的一些介绍材料,你可以从this Apple document开始

答案 2 :(得分:0)

这里有多个问题......

首先,你的for循环有一个语法错误......不仅如此,你还试图用一个名为userFoodDictionary的迭代器变量迭代一个名为userFoodDictionary的字典。将其更改为:

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

您还试图访问超出范围的变量。 userFoodDictionary声明在loadUserFoodData范围内。该方法完成后,该变量将被释放,因此当然您无法在setName:andCarbs:中访问它。为此,您需要在更广泛的范围内定义它 - 创建属性,静态变量,iVar等。

这里有许多缺乏OOP概念......如果你是编程新手,Objective-C是一个难以启动的地方,它可能会引发你很多循环(双关语)。

答案 3 :(得分:0)

试试这个......

#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

如果有帮助,请告诉我......:)

答案 4 :(得分:0)

我认为问题是您的NSMutableDictionary应该是ivar,它在.h中声明NSMutableDictionary,如下所示

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];

}

答案 5 :(得分:-2)

所以问题出在下面一行

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

替换为

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

这可以解决您的问题

ok还有其他问题

请将userFoodDictionary声明为其中一个属性 正如@Sebastian在他的回答中所描述的那样:)