![](/img/trans.png)
[英]How to access the original video using UIImagePickerController or other alternative methods?
[英]How to access dictionaries within other methods?
下面是我用来尝试通过另一种方法访问初始化字典的代码:
#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之前执行。 有人可以帮我解决问题吗?
我似乎收到的错误消息是:
使用未声明的标识符“ userFoodDictionary”
编译器说:
1.我没有在本地看到任何变量,例如userFoodDictionary或-(void)setName:(NSString *)foodName和Carbs:(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
我假设您在发布的第二种方法中遇到了错误。
- (void)setName:(NSString *)foodName andCarbs:(NSNumber *)carbAmount{
[userFoodDictionary setObject:foodName forKey:foodName];
}
该方法未定义userFoodDicitionary
。 如果您希望字典在方法调用之间持久存在,请将其存储在@property
。
您应该真正阅读了Objective-C的入门资料,您可以从这份Apple文档开始
这里有多个问题...
一方面,您的for循环有一个语法错误……不仅如此,而且您正尝试通过一个名为userFoodDictionary
的字典进行迭代, userFoodDictionary
使用一个迭代器变量也称为userFoodDictionary
。 更改为类似:
for (id dictKey in userFoodDictionary) {
NSLog(@"%@", dictKey);
}
您还试图访问超出范围的变量。 userFoodDictionary
在loadUserFoodData
的范围内声明。 该方法完成后,将释放该变量,因此您当然不能在setName:andCarbs:
访问它。 为此,您需要在更广泛的范围内进行定义-创建属性,静态变量,iVar等。
这里缺少许多OOP概念...如果您是编程的新手,那么Objective-C是一个很难入门的地方,它可能会使您陷入很多循环(双关语)。
尝试这个...
#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];
}
@结束
让我知道是否有帮助... :)
我认为问题是您的NSMutableDictionary应该是在.h中声明为NSMutableDictionary的ivar,如下所示
NSMutableDictionary *userFoodDictionary
在.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];
}
所以问题在下面的行中
for (userFoodDictionary in userFoodDictionary){
NSLog(@"%@", userFoodDictionary);
}
替换为
for (NSMutableDictionary *userFoodDic in userFoodDictionary){
NSLog(@"%@", userFoodDic);
}
可以解决您的问题
好,也有其他问题
请也将userFoodDictionary
声明为属性之一,如@Sebastian在其回答中所述:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.