Obj-c,如何创建将填充 NSDictionary 并从函数中获取值的函数?

发布于 2024-10-12 16:16:25 字数 994 浏览 4 评论 0原文

我一直在阅读有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里梦着梦中梦 2024-10-19 16:16:26

你并不清楚什么不起作用,但有一些明显错误的事情(JonLOo 可能是正确的)......

首先。您使用了错误的方法,或者至少有更好的方法 - setValue:forKey: 应该/可以是 setObject:forKey: 。这可能是您出现问题的原因之一。

第二。你过度分配并且没有正确释放。 viewDidLoad 中的 dictCategories2 将消失在虚空中,并带来为 getCategories 中定义的 dictCategories 分配的内存方法。对此的一个简单的标准修复是将

NSMutableDictionary *dictCategories = [[NSMutableDictionary alloc] init];

getCategories 更改为

NSMutableDictionary *dictCategories = [NSMutableDictionary dictionary];

系统将使用后一种方法自动释放。

第三。您想阅读@property。 Ob-C 标准不是使用 getFoo、setBar,而是使用 @properties 来(预)定义 setter 和 getter 方法。然后,您可以在适当的时候覆盖这些以将默认数据填充到您的方法中。您还(可能)希望将字典作为实例变量存储在界面中,而不是让它一直被释放。执行此操作的 @property 实现的示例:

@interface foo {
    NSMutableDictionary *ingredients;
}

@property (nonatomic, retain) NSMutableDictionary *ingredients;

@end

// ....

@implementation foo

@synthesize ingredients;

// ...

// the @synthesize command above will create getter and setter methods for us but 
// we can override them, which we need to do here

- (NSMutableDictionary *)ingredients
{
    if (ingredients != nil) {
        // we've already got an ingredients variable so we just return it
        return ingredients;
    }
    // we need to create ingredients
    ingredients = [[NSMutableDictionary alloc] init];
    [ingredients setObject:@"foo" forKey:@"bar"]
    return ingredients;
}

在 viewDidLoad 方法中(或您认为 ingredients 可能尚未初始化的其他任何地方),您可以执行例如

NSMutableDictionary *dict = self.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 be setObject:forKey: instead. This might be one of the reasons for your issue.

Secondly. You're over-allocating and not releasing properly. dictCategories2 in your viewDidLoad will vanish into the void and bring with it the allocated memory for dictCategories defined in the getCategories method. An easy standard fix for this is to change

NSMutableDictionary *dictCategories = [[NSMutableDictionary alloc] init];

in getCategories into

NSMutableDictionary *dictCategories = [NSMutableDictionary dictionary];

It 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:

@interface foo {
    NSMutableDictionary *ingredients;
}

@property (nonatomic, retain) NSMutableDictionary *ingredients;

@end

// ....

@implementation foo

@synthesize ingredients;

// ...

// the @synthesize command above will create getter and setter methods for us but 
// we can override them, which we need to do here

- (NSMutableDictionary *)ingredients
{
    if (ingredients != nil) {
        // we've already got an ingredients variable so we just return it
        return ingredients;
    }
    // we need to create ingredients
    ingredients = [[NSMutableDictionary alloc] init];
    [ingredients setObject:@"foo" forKey:@"bar"]
    return ingredients;
}

In the viewDidLoad method (or anywhere else where you think ingredients might not have been initialized yet), you would do e.g.

NSMutableDictionary *dict = self.ingredients;

Anywhere else you can opt to use just ingredients without self, but if it's nil, your method will never be called, and you will get nil 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.

岁月染过的梦 2024-10-19 16:16:25

您调用的方法错误,请尝试 [self getCategories]

you are calling the method wrong,try [self getCategories]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文