nsmutable 数组中字段的动态命名?

发布于 2024-11-17 03:55:51 字数 462 浏览 0 评论 0原文

我希望我能清楚地解释这一点。 我有一个 NSMutableArray * myMut。 我有 NSString * anyoldString 形式的用户输入。 我有用户输入的一些数值存储在变量 someNumber 中;

当用户输入字符串和值时,我想更新 myMut,并将字段命名为 anyoldString

如下所示:myMut.anyoldString=someNumber; 根据用户的输入,名称会有所不同,当然

我是 Objective C 的新手。

Matlab 中,我会这样做:

myMut.(anyoldString)=someNumer. 

我知道这对于目标 c 来说太简单了!但有什么快速的方法可以做到这一点吗?

I hope i can explain this clearly.
I have a NSMutableArray * myMut.
I have user input in the form of an NSString * anyoldString.
I have user input of some numeric values stored in the variable someNumber;

As users input their string and values, I want to update myMut, and name the fields anyoldString

like this: myMut.anyoldString=someNumber;
depending on the user's input, the name will be different of course

i'm new to objective c.

in Matlab, I would do this:

myMut.(anyoldString)=someNumer. 

I know that's too easy for objective c! but any fast way to do this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

唱一曲作罢 2024-11-24 03:55:51

你可能会想做这样的事情:

NSMutableDictionary *myMutableDictionary; // A mutable **dictionary**, not an array!
NSInteger someNumber; // filled by the number
NSString *key; // filled by anyOldString

[myMutableDictionary setObject:[NSNumber numberWithInteger:someNumber] forKey:key];

NSArray 和朋友不是关联的;但已编入索引。你想要的是 NSDictionary 和朋友们;您友好的邻居键值映射!

编辑:用于娱乐;给定任何旧字符串,获取某个数字:

NSMutableDictionary *myMutableDictionary; // The same dictionary as above
NSInteger someNumber; // out number
NSString *key; // filled by anyOldString

NSNumber *storedNumber = [myMutableDictionary objectForKey:key];

if (storedNumber) {
  someNumber = [storedNumber integerValue];
} else {
  someNumber = APPLICATION_APPROPRIATE_DEFAULT_VALUE;
}

You'll probably want to do something like so:

NSMutableDictionary *myMutableDictionary; // A mutable **dictionary**, not an array!
NSInteger someNumber; // filled by the number
NSString *key; // filled by anyOldString

[myMutableDictionary setObject:[NSNumber numberWithInteger:someNumber] forKey:key];

NSArray and friends aren't associative; but indexed. What you want is NSDictionary and friends; your friendly neighborhood key-value mapping!

Edit: For funsies; getting someNumber back out, given anyOldString:

NSMutableDictionary *myMutableDictionary; // The same dictionary as above
NSInteger someNumber; // out number
NSString *key; // filled by anyOldString

NSNumber *storedNumber = [myMutableDictionary objectForKey:key];

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