NSString 中的 Objective-C 标识符
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
变量的名称由编译器使用,并在编译时设置,而不是在运行时设置。
如果您需要能够将标签与数组关联起来,我建议您使用 NSDictionary 来做这样的事情
如果您愿意,您可以在字典中拥有多个“命名”数组,并且可以通过以下名称访问它们你给了他们
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
You could have multiple "named" arrays in the dictionary, if you wanted, and could access them by the names that you gave them
查看 键值编码用于通过属性名称设置现有属性的值,但似乎无法创建新属性 财产。为此,你应该只使用字典。
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.