iOS/WebView/Project - Constants 및 Utils 분리#10
IOS/Xcode 14X Swift5.7.2 WKWebView 에서 작업 진행
상수 추가
가독성 및 유지보수를 위해서 상수를 추가한다.
import Foundation
struct Constants {
static let SERVER_LOCAL: String = "LOCALHOST"
static let SERVER_DEV: String = "DEVELOPMENT"
static let SERVER_STG: String = "STAGING"
static let SERVER_PROD: String = "PRODUCTION"
static let SERVERS = [
SERVER_LOCAL: [
"NAME": "로컬",
"URL": "http://local"
],
SERVER_DEV: [
"NAME": "개발",
"URL": "http://dev"
],
SERVER_STG: [
"NAME": "스테이징",
"URL": "http://stg"
],
SERVER_PROD: [
"NAME": "운영",
"URL": "http://prod"
]
]
}
유틸 추가
공통 된 함수로 만들어서 편의성을 극대화 해보자!
import Foundation
class Utils {
public static func isDebug() -> Bool {
#if DEBUG
return true
#else
return false
#endif
}
public static func Log<T>(_ object: T?, filename: String = #file, line: Int = #line, funcName: String = #function) {
#if DEBUG
// Optional Binding
if let obj = object {
print("\(Date()) \(filename.components(separatedBy: "/").last ?? "")(\(line)) : \(funcName) : \(obj)")
} else {
print("\(Date()) \(filename.components(separatedBy: "/").last ?? "")(\(line)) : \(funcName) : nil")
}
#endif
}
}
사용된 부분
다음 장에서는 상수랑 유틸을 활용해서 IOS 진입시점에 웹뷰 서버를 선택할 수 있도록 할것이다!
참조
https://es1015.tistory.com/347
[Swift] log에 date, file name, function name, line 쉽게 출력하는 방법
Swift에서 콘솔창에 로그 쉽게 출력하는 방법이다. 보통 로그에 날짜, 파일명, 메소드명, 라인을 작성하려면 아래와 같이 작성해야한다. 1 print("\(Date()) \(#file.components(separatedBy: "/").last ?? "") \(#functi
es1015.tistory.com
https://stackoverflow.com/questions/38585344/swift-constants-struct-or-enum
Swift constants: Struct or Enum
I'm not sure which of both are better to define constants. A struct or a enum. A struct will be copied every time i use it or not? When i think about a struct with static let constants it makes no ...
stackoverflow.com