动态创建变量名
在 Objective-C 中,有一种方法可以动态创建变量名。就像我将一个字符串传递到一个方法中,该方法创建一个 NSString ,其名称就是传入的字符串。类似
-(void)create:(NSString *)str{
NSString *[NSString stringWithFormat:@"%@", str];
}
或者也许有一种方法可以更改已经存在的变量名称。
NSString *password;
password.name = entryPassword;
In objective-c is there a way to dynamically create a variable name. Like I pass a string into a method and the method creates an NSString whos name is the string that was passed in. Something like
-(void)create:(NSString *)str{
NSString *[NSString stringWithFormat:@"%@", str];
}
Or maybe there is a way to change an already existing variables name.
NSString *password;
password.name = entryPassword;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我确信有人会指出一些模糊的异常,但一般来说,这在任何编译语言中都是不可能的。
I'm sure that someone will point out some obscure exception, but in general, this is not possible in any compiled language.
我会调查 NSMutableArray< /a>.
NSMutableArray 的大小可以更改,并且所有对象都保留在其中。
因此,您可以将本地创建的字符串存储在 NSMutableArray 中并稍后访问该字符串。
因此,您可以拥有一个
NSMutableArray
类型的 ivar mutableArray,然后您可以存储本地创建的字符串:这并不完全符合您的说法,但它可能是您将获得的最接近的结果。 (或者我误解了你的问题)。
I would look into NSMutableArray.
An NSMutableArray's size can change and all objects are retained in it.
So, you can store locally created strings inside the
NSMutableArray
and access the string later.So you can have a ivar mutableArray of type
NSMutableArray
and then you can store locally created strings:This doesn't do exactly what you are saying, but it probably is the closest you are going to get. (Or I am misunderstanding your question).
在 C#(也是编译语言)中,它称为反射:http://msdn.microsoft。 com/en-us/library/ms173183.aspx 并且根据定义,您可以“动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。”。
我不明白为什么人们会问“为什么一个人想做他们想做的事”而不是简单地回答这个问题。
In C# (also compiled language) it's called reflection : http://msdn.microsoft.com/en-us/library/ms173183.aspx and as per definition you can "dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.".
I don't get why ppl ask "why would one want to do what they want to do" instead of simply answering the question.
几周前,我提出了这个问题,并将得分最多的答案(通过 mmc)视为事实,但是在至少 1 个上下文中,您可以在 Objective C 中动态“创建”变量名称。
我的老师 Jacek Lampart 向我展示了如何操作,我想我会与其他新手分享。
肉:
I came to this question a few weeks ago and treated the answer with the biggest number of points (by mmc) as a fact, but in at least 1 context you can dynamically “create” variable name in Objective C.
My teacher, Jacek Lampart, showed me how and I thought I’ll share it with other newbies.
The meat: