NSString 中的 Objective-C 标识符

发布于 2024-12-02 23:44:31 字数 353 浏览 0 评论 0原文

我想将 NSString 传递给方法,并将该特定 NSString 命名为新的 NSSMutableArray。令人困惑?以编程方式看起来像这样:

+ (void)newMutableArrayWithName:(NSString*)theArrayName
{
     NSLog(@"Creating an array that is named: %@",theArrayName);
     NSMutableArray* theArrayName = [[NSMutableArray alloc] init];
}

不幸的是,“theArrayName”与传递给该方法的参数无关。有什么办法可以实现这一点吗?

I want to pass an NSString to a method and have that particular NSString name a new NSSMutableArray. Confusing? Programmatically looks like this:

+ (void)newMutableArrayWithName:(NSString*)theArrayName
{
     NSLog(@"Creating an array that is named: %@",theArrayName);
     NSMutableArray* theArrayName = [[NSMutableArray alloc] init];
}

Unfortunately, "theArrayName" is not affiliated with the argument passed to the method. Is there any way this is achievable?

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

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

发布评论

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

评论(2

你怎么敢 2024-12-09 23:44:31

变量的名称由编译器使用,并在编译时设置,而不是在运行时设置。

如果您需要能够将标签与数组关联起来,我建议您使用 NSDictionary 来做这样的事情

NSString *theArrayName = @"My Cool Array";
NSMutableArray *theArray = [NSMutableArray array];
NSDictionary *theDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
                                      theArray, theArrayName, nil];

如果您愿意,您可以在字典中拥有多个“命名”数组,并且可以通过以下名称访问它们你给了他们

[theDictionary objectForKey:@"My Cool Array"];

The name of a variable is used by the compiler, and is set at compile-time, not at run time.

If you need to be able to associate a label with an array, I suggest that you use an NSDictionary to do something like this

NSString *theArrayName = @"My Cool Array";
NSMutableArray *theArray = [NSMutableArray array];
NSDictionary *theDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
                                      theArray, theArrayName, nil];

You could have multiple "named" arrays in the dictionary, if you wanted, and could access them by the names that you gave them

[theDictionary objectForKey:@"My Cool Array"];
稍尽春風 2024-12-09 23:44:31

查看 键值编码用于通过属性名称设置现有属性的值,但似乎无法创建新属性 财产。为此,你应该只使用字典。

Look into key-value coding for setting the values of existing properties by the property's name, but it appears it can't create a new property. For that, you should just use a dictionary.

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