system.getPreference()

类型 函数
system.*
返回值 各种
修订 版本 2024.3703
关键词 系统偏好设置, getPreference
另请参见 system.deletePreferences()
system.setPreferences()

概览

返回请求的偏好设置值。

问题

语法

system.getPreference( category, name [, type] )
类别 (必需)

字符串. 指示从何处读取偏好设置。必须设置为以下之一

  • “app” — 应用程序的自定义偏好设置。由 Corona 应用开发者定义。
  • “locale” — 用于访问操作系统只读 与区域相关的偏好设置。
  • “ui” — 用于访问操作系统只读 与 UI 相关的偏好设置。
名称 (必需)

字符串. 要读取的偏好设置的唯一名称,取决于使用的类别

  • “app” — Corona 应用开发者通过 system.setPreferences() 函数写入存储的偏好设置名称。
  • “locale” — 仅支持偏好设置名称 “country”“identifier”“language”
  • “ui” — 仅支持偏好设置名称 “language”
类型 (可选)

字符串. 表示此函数返回的值类型。此类型应与存储中偏好设置值类型相符。例如,属于 “locale”“ui” 类别下的所有偏好设置都以字符串形式存储,应读取为字符串,而 “app” 类别的类型则应与传递给 system.setPreferences() 函数的值类型相符。接受的值包括

  • “string”(默认)
  • “boolean”
  • “number”

实例

-- Print the operating system's read-only preferences
print( system.getPreference( "ui", "language" ) )        -- Print the UI (device) language, i.e. "en-US"
print( system.getPreference( "locale", "country" ) )     -- Print the locale country, i.e. "US"
print( system.getPreference( "locale", "identifier" ) )  -- Print the locale language identifier, i.e. "en_US"
print( system.getPreference( "locale", "language" ) )    -- Print the locale language code, i.e. "en"

-- Write this app's custom preferences to storage
local appPreferences =
{
    myBoolean = true,
    myNumber = 123.45,
    myString = "Hello World"
}
system.setPreferences( "app", appPreferences )

-- Read the preferences that were written to storage above
local myBoolean = system.getPreference( "app", "myBoolean", "boolean" )
local myNumber = system.getPreference( "app", "myNumber", "number" )
local myString = system.getPreference( "app", "myString", "string" )