如何使用Bool(Globals - struct)xcode8-swift3类型定义变量

时间:2017-08-03 11:49:15

标签: swift3 xcode8

我在swift文件中写下面的代码:

import Foundation

struct globals {

    static var test = "someString"
    static var isContentLoadedFor1Events : Bool = false
    static var isContentLoadedFor2Events : Bool = false
    static var isContentLoadedFor3events : Bool = false
    static var isContentLoadedFor4Events : Bool = false

}
在行代码中的

(localMemory是UserDefaults.standard):

localMemory.setObject( true , forKey: globals.isContentLoadedFor1Events  )
  

无法转换类型' Bool'预期参数类型'字符串'

1 个答案:

答案 0 :(得分:1)

将var type更改为String -

import Foundation

struct globals {

    static var isContentLoadedFor1Events : String = "isContentLoadedFor1Events"

}

并像这样保存 -

localMemory.setObject( true , forKey: globals.isContentLoadedFor1Events )