处理通知和 UserDefaults 键名称的字符串名称的最常见方法是什么
What is the most common way to handle string names for Notifications and UserDefaults key names
我将在整个应用程序中使用一些字符串名称作为通知和 UserDefault 名称。
我听说为了类型安全,将 notification
名称或 UserDefaults
键名称定义为静态字符串并使它们成为 class 或结构的一部分是一个很好的做法。
处理 Notification 和 UserDefault 名称的字符串名称的最常用方法是什么?
我考虑过将它们作为全局变量放入我的 AppDelgate class 中,如下所示...
let MY_STRING_NAME = "My string name"
class AppDelegate: UIResponder, UIApplicationDelegate {}
/// Then just use MY_STRING_NAME in other classes.
或
class SomeClass {
let myStringName:String = "My string name"
}
/// Then use myClassInstance.myStringName
最好的方法是什么?
常见的方法是创建
struct Constants {
static let key1 = "value1"
static let key2 = "value2"
.....
}
// 用于通知
extension Notification.Name {
static let didReceiveData = Notification.Name("didReceiveData")
static let didCompleteTask = Notification.Name("didCompleteTask")
}
有些人更喜欢带有(k字母前缀)的全局变量
let kDomain = "value1"
日期为 #define
in Objective-C ,但使用常量是编码阅读器
的一种干净方式
UserDefaults 的一个选项是创建一个助手 class 来隐藏您正在使用 UserDefaults 的事实。这使得它在您的其余代码中看起来就像您正在使用一个简单的 class 及其属性一样。
像这样:
class MyAppPreferences {
static let shared = MyAppPreferences()
private let highScoreKey = "highScore"
var highScore: Int {
get {
return UserDefaults.standard.integer(forKey: highScoreKey)
}
set {
UserDefaults.standard.set(newValue, forKey: highScoreKey)
}
}
}
在其他代码上你可以这样做来设置它:
MyAppPreferences.shared.highScore = 100
或阅读以下内容:
let score = MyAppPreferences.shared.highScore
为您的应用程序所需的每个应用程序首选项添加计算的 属性 和私钥。这会使您的其余代码更加清晰。
并且由于某些原因,您以后需要更改一个或多个首选项的存储方式,您只需更改一个地方的代码。
对于通知名称,您可以向每个 class 通知关联的名称添加常量。您会在许多 iOS class 中看到这种模式,例如 UIApplication、UITextView 和其他。
class SomeClass {
static someClassEventNotification = NSNotification.Name("someUniqueName")
}
我认为使用枚举是更合适的定义常量的方式
用户默认值:
class MyAppPreferences {
static let shared = MyAppPreferences()
private enum Key: String {
case userName
case email
}
var userName: String? {
get {
return UserDefaults.standard.string(forKey: Key.userName.rawValue)
}
set {
UserDefaults.standard.set(newValue, forKey: Key.userName.rawValue)
}
}
}
通知:
enum AppNotification: String {
case didReceivedData
case didCompleteTask
}
extension Notification.Name {
init(_ appNotification:AppNotification) {
self.init(appNotification.rawValue)
}
}
//Use:
func notify() {
NotificationCenter.default.post(.init(name: .init(.didReceivedData)))
}
我将在整个应用程序中使用一些字符串名称作为通知和 UserDefault 名称。
我听说为了类型安全,将 notification
名称或 UserDefaults
键名称定义为静态字符串并使它们成为 class 或结构的一部分是一个很好的做法。
处理 Notification 和 UserDefault 名称的字符串名称的最常用方法是什么?
我考虑过将它们作为全局变量放入我的 AppDelgate class 中,如下所示...
let MY_STRING_NAME = "My string name"
class AppDelegate: UIResponder, UIApplicationDelegate {}
/// Then just use MY_STRING_NAME in other classes.
或
class SomeClass {
let myStringName:String = "My string name"
}
/// Then use myClassInstance.myStringName
最好的方法是什么?
常见的方法是创建
struct Constants {
static let key1 = "value1"
static let key2 = "value2"
.....
}
// 用于通知
extension Notification.Name {
static let didReceiveData = Notification.Name("didReceiveData")
static let didCompleteTask = Notification.Name("didCompleteTask")
}
有些人更喜欢带有(k字母前缀)的全局变量
let kDomain = "value1"
日期为 #define
in Objective-C ,但使用常量是编码阅读器
UserDefaults 的一个选项是创建一个助手 class 来隐藏您正在使用 UserDefaults 的事实。这使得它在您的其余代码中看起来就像您正在使用一个简单的 class 及其属性一样。
像这样:
class MyAppPreferences {
static let shared = MyAppPreferences()
private let highScoreKey = "highScore"
var highScore: Int {
get {
return UserDefaults.standard.integer(forKey: highScoreKey)
}
set {
UserDefaults.standard.set(newValue, forKey: highScoreKey)
}
}
}
在其他代码上你可以这样做来设置它:
MyAppPreferences.shared.highScore = 100
或阅读以下内容:
let score = MyAppPreferences.shared.highScore
为您的应用程序所需的每个应用程序首选项添加计算的 属性 和私钥。这会使您的其余代码更加清晰。
并且由于某些原因,您以后需要更改一个或多个首选项的存储方式,您只需更改一个地方的代码。
对于通知名称,您可以向每个 class 通知关联的名称添加常量。您会在许多 iOS class 中看到这种模式,例如 UIApplication、UITextView 和其他。
class SomeClass {
static someClassEventNotification = NSNotification.Name("someUniqueName")
}
我认为使用枚举是更合适的定义常量的方式
用户默认值:
class MyAppPreferences {
static let shared = MyAppPreferences()
private enum Key: String {
case userName
case email
}
var userName: String? {
get {
return UserDefaults.standard.string(forKey: Key.userName.rawValue)
}
set {
UserDefaults.standard.set(newValue, forKey: Key.userName.rawValue)
}
}
}
通知:
enum AppNotification: String {
case didReceivedData
case didCompleteTask
}
extension Notification.Name {
init(_ appNotification:AppNotification) {
self.init(appNotification.rawValue)
}
}
//Use:
func notify() {
NotificationCenter.default.post(.init(name: .init(.didReceivedData)))
}