Obj-c,如何创建将填充 NSDictionary 并从函数中获取值的函数?
我一直在阅读有关 NSArrays 和 NSDictionaires 的内容,我想我需要后者。我正在尝试从一个小型数据库表中填充一个对象。所以我可以通过记录 ID 访问字符串值。我必须多次执行此操作,因此将其放入对象中是有意义的。
我有基础...
- (void)viewDidLoad {
// WORKING START
NSMutableDictionary *dictCategories = [[NSMutableDictionary alloc] init];
[dictCategories setValue:@"Utility" forKey:@"3"];
[dictCategories setValue:@"Cash" forKey:@"5"];
NSString *result;
result = [dictCategories objectForKey:@"3"];
NSLog(@"Result=%@", result);
// WORKING END
// Can't get this bit right, current error Request for member
// 'getCategories' in something not a structure or union
NSMutableDictionary *dictCategories2 = self.getCategories;
NSLog(@"Result2=%@", [dictCategories2 objectForKey:@"5"]);
[super viewDidLoad];
}
-(NSMutableDictionary*)getCategories {
NSMutableDictionary *dictCategories = [[NSMutableDictionary alloc] init];
[dictCategories setValue:@"Utility" forKey:@"3"];
[dictCategories setValue:@"Cash" forKey:@"5"];
return dictCategories;
}
I've been reading about NSArrays and NSDictionaires and I think I need the later. I'm trying to populate an object from a small database table. So I can access the string values via a record id. I have to do this several times so putting it into an object makes sense.
I have the basics...
- (void)viewDidLoad {
// WORKING START
NSMutableDictionary *dictCategories = [[NSMutableDictionary alloc] init];
[dictCategories setValue:@"Utility" forKey:@"3"];
[dictCategories setValue:@"Cash" forKey:@"5"];
NSString *result;
result = [dictCategories objectForKey:@"3"];
NSLog(@"Result=%@", result);
// WORKING END
// Can't get this bit right, current error Request for member
// 'getCategories' in something not a structure or union
NSMutableDictionary *dictCategories2 = self.getCategories;
NSLog(@"Result2=%@", [dictCategories2 objectForKey:@"5"]);
[super viewDidLoad];
}
-(NSMutableDictionary*)getCategories {
NSMutableDictionary *dictCategories = [[NSMutableDictionary alloc] init];
[dictCategories setValue:@"Utility" forKey:@"3"];
[dictCategories setValue:@"Cash" forKey:@"5"];
return dictCategories;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你并不清楚什么不起作用,但有一些明显错误的事情(JonLOo 可能是正确的)......
首先。您使用了错误的方法,或者至少有更好的方法 -
setValue:forKey:
应该/可以是setObject:forKey:
。这可能是您出现问题的原因之一。第二。你过度分配并且没有正确释放。
viewDidLoad
中的dictCategories2
将消失在虚空中,并带来为getCategories
中定义的dictCategories
分配的内存方法。对此的一个简单的标准修复是将getCategories 更改为
系统将使用后一种方法自动释放。
第三。您想阅读
@property
。 Ob-C 标准不是使用 getFoo、setBar,而是使用 @properties 来(预)定义 setter 和 getter 方法。然后,您可以在适当的时候覆盖这些以将默认数据填充到您的方法中。您还(可能)希望将字典作为实例变量存储在界面中,而不是让它一直被释放。执行此操作的 @property 实现的示例:在 viewDidLoad 方法中(或您认为
ingredients
可能尚未初始化的其他任何地方),您可以执行例如任何其他您可以选择仅使用 < code>ingredients 没有
self
,但如果它是 nil,你的方法将永远不会被调用,并且你会得到nil
抛出给你。这在许多情况下很有用,如果我们想从类外部读取或写入成分变量,这是必要的。这超出了您所询问的范围,但我提出它是因为您正在尝试对 self.getCategories 执行类似的操作。
希望有帮助。
You're not being clear on what isn't working, but a few things that are obviously wrong (JonLOo might be spot on though) ...
Firstly. You're using the wrong methods, or at least there's a better one --
setValue:forKey:
should/could besetObject:forKey:
instead. This might be one of the reasons for your issue.Secondly. You're over-allocating and not releasing properly.
dictCategories2
in yourviewDidLoad
will vanish into the void and bring with it the allocated memory fordictCategories
defined in thegetCategories
method. An easy standard fix for this is to changein
getCategories
intoIt will be autoreleased using the latter method by the system.
Thirdly. You want to read up on
@property
. Instead of getFoo, setBar, the Ob-C standard is to use @properties to (pre)define setters and getter methods. You can then override these to populate default data into your methods when appropriate. You also (probably) want to store the dictionary in your interface as an instance variable, rather than letting it be deallocated all the time. Example of a @property implementation that does this:In the viewDidLoad method (or anywhere else where you think
ingredients
might not have been initialized yet), you would do e.g.Anywhere else you can opt to use just
ingredients
withoutself
, but if it's nil, your method will never be called, and you will getnil
thrown at you.This is useful in many cases, and is necessary if we want to ever read or write the ingredients variable from outside of our class. It's outside of what you're asking about, but I brought it up because you're trying to do something similar with
self.getCategories
.Hope that helps.
您调用的方法错误,请尝试 [self getCategories]
you are calling the method wrong,try [self getCategories]