动态创建变量名

发布于 2024-11-30 03:25:32 字数 307 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(4

狼亦尘 2024-12-07 03:25:32

我确信有人会指出一些模糊的异常,但一般来说,这在任何编译语言中都是不可能的。

I'm sure that someone will point out some obscure exception, but in general, this is not possible in any compiled language.

回首观望 2024-12-07 03:25:32

我会调查 NSMutableArray< /a>.

NSMutableArray 的大小可以更改,并且所有对象都保留在其中。

因此,您可以将本地创建的字符串存储在 NSMutableArray 中并稍后访问该字符串。

因此,您可以拥有一个 NSMutableArray 类型的 ivar mutableArray,然后您可以存储本地创建的字符串:

//In your init method
mutableArray = [[NSMutableArray alloc] init];
//In your create method:
[mutableArray addObject: [NSString initWithString:str]];

这并不完全符合您的说法,但它可能是您将获得的最接近的结果。 (或者我误解了你的问题)。

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:

//In your init method
mutableArray = [[NSMutableArray alloc] init];
//In your create method:
[mutableArray addObject: [NSString initWithString:str]];

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).

无名指的心愿 2024-12-07 03:25:32

在 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.

灯下孤影 2024-12-07 03:25:32

几周前,我提出了这个问题,并将得分最多的答案(通过 mmc)视为事实,但是在至少 1 个上下文中,您可以在 Objective C 中动态“创建”变量名称

我的老师 Jacek Lampart 向我展示了如何操作,我想我会与其他新手分享。

肉:

// Sample for loop

for(int i=1; i<=3; i++)
{
    // Here you define the format of variable.
    // In this example it’s gonna be blablabla1, blablabla2, blablabla3.

    NSString *targetVariableName = [NSString stringWithFormat:@"blablabla%d", i];

    // Sample UIImageView variable.
    // You assign self.blablabla1 (self.blablabla2 and so on) 
    // to currentVariable in a way, which enables you to work
    // with this object (for example setImage to it etc.).

    UIImageView *currentVariable = [self valueForKey:targetVariableName];

    // […]

    // Profit & DRY power! 
}

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:

// Sample for loop

for(int i=1; i<=3; i++)
{
    // Here you define the format of variable.
    // In this example it’s gonna be blablabla1, blablabla2, blablabla3.

    NSString *targetVariableName = [NSString stringWithFormat:@"blablabla%d", i];

    // Sample UIImageView variable.
    // You assign self.blablabla1 (self.blablabla2 and so on) 
    // to currentVariable in a way, which enables you to work
    // with this object (for example setImage to it etc.).

    UIImageView *currentVariable = [self valueForKey:targetVariableName];

    // […]

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