如何使用变量构造命令

时间:2022-02-21 00:37:20

I have a variable in Xcode that I need to use to construct another command. How to I combine this? This is the concept

我在Xcode中有一个变量,我需要用它来构造另一个命令。我该如何结合这个?这是概念

I have a variable named "activechannel1" and buttons Overlay1 - Overlay12

我有一个名为“activechannel1”的变量和按钮Overlay1 - Overlay12

I want to set the image of a particular button and the button will depend on the value of activechannel1. All the numbers correspond to file names so I am trying to do this.

我想设置特定按钮的图像,按钮将取决于activechannel1的值。所有数字都对应于文件名,所以我试图这样做。

Let's say that

我们这样说吧

ActiveChannel1 = 6 // This changes and represents which button I want to change

I am trying to do this but am getting a consecutive statement error:

我试图这样做,但我得到一个连续的语句错误:

Overlay"\(ActiveChannel1)".image = UIImage(named: "y\(ActiveChannel1)")

Which I want to essentially yield:

我想基本上屈服于:

Overlay6.image = UIImage(named "y6")

1 个解决方案

#1


0  

You can't dynamically "type" variable names. You can, however, access them in such a way that a variable can change which one you use.

您无法动态“键入”变量名称。但是,您可以通过变量可以更改您使用的变量来访问它们。

Classes you might find useful (note that there are changes to how you use these in Swift):
NSArray
NSDictionary

您可能觉得有用的类(请注意,您在Swift中使用它们的方式有所变化):NSArray NSDictionary

Given X as the number (in the form of a string here):

给定X作为数字(在这里以字符串的形式):

For OverlayX, you can use an array.

对于OverlayX,您可以使用数组。

let overlays = [Overlay1, Overlay2, /*...*/]

Access the right variable using the indices 0-11.

使用索引0-11访问正确的变量。

For yX use NSString(format:).

对于yX使用NSString(格式:)。

//use %d for int
let imageName = NSString("y%@", X - 1) //%@ is Obj-C format for id IIRC, I'm not sure if it changes in Swift
overlays[X - 1].image = UIImage(named: imageName)
//or, in a single line
overlays[X - 1].image = UIImage(named: NSString(format: "y%@", argList: X - 1))

Note that this code in untested.
Also note that I use Objective-C more than Swift. If someone comes across this and finds the syntax incorrect, please comment or edit my post.

请注意,此代码未经测试。另请注意,我使用的是Objective-C而不是Swift。如果有人遇到此问题并发现语法不正确,请评论或编辑我的帖子。

#1


0  

You can't dynamically "type" variable names. You can, however, access them in such a way that a variable can change which one you use.

您无法动态“键入”变量名称。但是,您可以通过变量可以更改您使用的变量来访问它们。

Classes you might find useful (note that there are changes to how you use these in Swift):
NSArray
NSDictionary

您可能觉得有用的类(请注意,您在Swift中使用它们的方式有所变化):NSArray NSDictionary

Given X as the number (in the form of a string here):

给定X作为数字(在这里以字符串的形式):

For OverlayX, you can use an array.

对于OverlayX,您可以使用数组。

let overlays = [Overlay1, Overlay2, /*...*/]

Access the right variable using the indices 0-11.

使用索引0-11访问正确的变量。

For yX use NSString(format:).

对于yX使用NSString(格式:)。

//use %d for int
let imageName = NSString("y%@", X - 1) //%@ is Obj-C format for id IIRC, I'm not sure if it changes in Swift
overlays[X - 1].image = UIImage(named: imageName)
//or, in a single line
overlays[X - 1].image = UIImage(named: NSString(format: "y%@", argList: X - 1))

Note that this code in untested.
Also note that I use Objective-C more than Swift. If someone comes across this and finds the syntax incorrect, please comment or edit my post.

请注意,此代码未经测试。另请注意,我使用的是Objective-C而不是Swift。如果有人遇到此问题并发现语法不正确,请评论或编辑我的帖子。