我已将此struct
定义用于我的ErrorLogger
。但是,一些参数(例如Timestamp)我想立即使用列出的函数。我如何传递一个默认值,而不是零的参数为:header
,errorLocation
,userId
?为了表明如果没有其他值,将使用默认值。
想法是,如果我将一些默认值传递给这些参数,则将调用struct函数以获取这些值,否则这些值将被初始化期间用户传递的所有内容覆盖。
我也收到错误:
在初始化所有存储的属性之前自行使用
在这条线上:
self.errorLocation = getErrorLocation()
从viewController初始化结构:
let logError = LogError(header: nil, errorLocation: nil, userID: nil, description: "An error occurred while verifying if the user profile exists", errorMessage: "\(error?.localizedDescription ?? "")")
ErrorLogger中的结构:
struct LogError {
var header: String
var errorLocation: String
var userID: String
var description: String //describes the type of error
var errorMessage: String //actual error returned by given function
//TODO: add timestamp to lofError
//TODO: add logEvent (enum: Error, Info) For example: data validation errors can be of event Info
init(header: String?, errorLocation: String?, userID: String?, description: String, errorMessage: String) {
if header != nil {
self.header = header!
} else {
self.header = " ::Error::"
}
if errorLocation != nil {
self.errorLocation = errorLocation!
} else {
self.errorLocation = getErrorLocation()
}
if userID != nil {
self.userID = userID!
} else {
self.userID = getUserID()
}
self.description = " ::description::" + description
self.errorMessage = " ::errorMessage::" + errorMessage
}
func getUserID() -> String {
var userUID: String = ""
if Auth.auth().currentUser != nil {
userUID = (Auth.auth().currentUser!.uid.isEmpty ? "" : Auth.auth().currentUser!.uid)
} else {
userUID = ""
}
let userUIDStr: String = " ::UserID::" + userUID
return userUIDStr
}
func getErrorLocation() -> String {
// Get class name
let filePath: String = #file
let components = filePath.components(separatedBy: "/")
let className: String = components.isEmpty ? "" : components.last!
// Get line number
let line: Int = #line
// Get column number
let column: Int = #column
// Get function name
let funcName: String = #function
let errorLocationStr: String = " ::className::\(className)::lineNumber\(line)::columnNumber\(column)::funcName::\(funcName)"
return errorLocationStr
}
func toString() -> String {
let completeLogErrorMessageStr: String = header + errorLocation + userID + description + errorMessage
return completeLogErrorMessageStr
}
}
在调用方法之前,必须为所有属性赋予有效值。我会将您重构init
为以下形式:
init(header: String?, errorLocation: String?, userID: String?, description: String, errorMessage: String) {
self.header = header ?? " ::Error::"
self.errorLocation = errorLocation ?? ""
self.userID = userID ?? ""
self.description = " ::description::" + description
self.errorMessage = " ::errorMessage::" + errorMessage
if errorLocation == nil {
self.errorLocation = getErrorLocation()
}
if userID == nil {
self.userID = getUserID()
}
}
这将修复“初始化所有存储的属性之前自行使用”的错误。
要使用默认值,请更新签名:
init(header: String? = nil, errorLocation: String? = nil, userID: String? = nil, description: String, errorMessage: String) {
// nothing else changes
}
现在,您可以不带nil
参数地调用初始化程序。
例如:
let logError = LogError(header: nil, errorLocation: nil, userID: nil, description: "An error occurred while verifying if the user profile exists", errorMessage: "\(error?.localizedDescription ?? "")")
变成:
let logError = LogError(description: "An error occurred while verifying if the user profile exists", errorMessage: "\(error?.localizedDescription ?? "")")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句