在ios swift中的不同ViewController中设置struct属性的值

在ios swift中的不同ViewController中设置struct属性的值,ios,swift,struct,Ios,Swift,Struct,我使用struct设置常量。我有整数形式的maxTextLength。我必须为不同的控制器设置不同的值,比如300和1000。这是我的代码 struct Validations { static let maxAudioRecSec:Int = 150 static var maxTextLength = 300 // Default value } SecondVC :ViewController { override func viewDidL

我使用struct设置常量。我有整数形式的maxTextLength。我必须为不同的控制器设置不同的值,比如300和1000。这是我的代码

    struct Validations {
    static let maxAudioRecSec:Int = 150
    static var maxTextLength = 300 // Default value
    }
    SecondVC :ViewController {
    override func viewDidLoad () {
          Validations.maxTextLength = 1000
    } 
   }

因此,SecondVC中更改的值仅保留在该控制器中,即1000。如果我在另一个控制器中访问此值,则默认值应为300。

您必须像下面一样使用,不要将静态变量用于
maxTextLength

struct Validations {
    static let maxAudioRecSec:Int = 150
    var maxTextLength = 300 // Default value
 }
现在,在任何
ViewController
中,您必须像下面那样使用

class SecondVC :ViewController {
var validations = Validations() // create Validations struct object
    override func viewDidLoad () {
          validations.maxTextLength = 1000 // use like this
    } 
   }

任何疑问,请发表评论。

如果您想要不同的值,您需要不同的结构实例。谢谢您的回复。但是在viewController中使用此代码后,我收到了一个错误“静态成员“maxTextLength”不能用于“Validations”类型的实例”。您需要从maxTextLength变量中删除静态关键字,检查我的ans。正确。Bhavin&Vivek非常感谢。它很有效。如果我的问题有效,那么你可以投票表决。