全局常量文件在Swift中
在我的Objective-C项目中,我经常使用全局常量文件来存储NSUserDefaults
通知名称和密钥等NSUserDefaults
。 它看起来像这样:
@interface GlobalConstants : NSObject
extern NSString *someNotification;
@end
@implementation GlobalConstants
NSString *someNotification = @"aaaaNotification";
@end
我如何在Swift中完成同样的事情?
IMO处理这类常量的最好方法是创建一个Struct。
struct Constants {
static let someNotification = "TEST"
}
然后,例如,在代码中这样调用它:
print(Constants.someNotification)
编辑:如果你想要一个更好的组织,我建议你使用分段子结构
struct K {
struct NotificationKey {
static let Welcome = "kWelcomeNotif"
}
struct Path {
static let Documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
static let Tmp = NSTemporaryDirectory()
}
}
然后你可以使用例如K.Path.Tmp
我对派对的技巧稍微迟钝了。
不管这里是如何管理常量文件,以便在开发人员编写Swift代码时更有意义。
FOR URL:
//URLConstants.swift
struct APPURL {
private struct Domains {
static let Dev = "http://test-dev.cloudapp.net"
static let UAT = "http://test-UAT.com"
static let Local = "192.145.1.1"
static let QA = "testAddress.qa.com"
}
private struct Routes {
static let Api = "/api/mobile"
}
private static let Domain = Domains.Dev
private static let Route = Routes.Api
private static let BaseURL = Domain + Route
static var FacebookLogin: String {
return BaseURL + "/auth/facebook"
}
}
对于CUSTOMFONTS:
//FontsConstants.swift
struct FontNames {
static let LatoName = "Lato"
struct Lato {
static let LatoBold = "Lato-Bold"
static let LatoMedium = "Lato-Medium"
static let LatoRegular = "Lato-Regular"
static let LatoExtraBold = "Lato-ExtraBold"
}
}
适用于APP中使用的所有关键字
//KeyConstants.swift
struct Key {
static let DeviceType = "iOS"
struct Beacon{
static let ONEXUUID = "xxxx-xxxx-xxxx-xxxx"
}
struct UserDefaults {
static let k_App_Running_FirstTime = "userRunningAppFirstTime"
}
struct Headers {
static let Authorization = "Authorization"
static let ContentType = "Content-Type"
}
struct Google{
static let placesKey = "some key here"//for photos
static let serverKey = "some key here"
}
struct ErrorMessage{
static let listNotFound = "ERROR_LIST_NOT_FOUND"
static let validationError = "ERROR_VALIDATION"
}
}
对于颜色常量:
//ColorConstants.swift
struct AppColor {
private struct Alphas {
static let Opaque = CGFloat(1)
static let SemiOpaque = CGFloat(0.8)
static let SemiTransparent = CGFloat(0.5)
static let Transparent = CGFloat(0.3)
}
static let appPrimaryColor = UIColor.white.withAlphaComponent(Alphas.SemiOpaque)
static let appSecondaryColor = UIColor.blue.withAlphaComponent(Alphas.Opaque)
struct TextColors {
static let Error = AppColor.appSecondaryColor
static let Success = UIColor(red: 0.1303, green: 0.9915, blue: 0.0233, alpha: Alphas.Opaque)
}
struct TabBarColors{
static let Selected = UIColor.white
static let NotSelected = UIColor.black
}
struct OverlayColor {
static let SemiTransparentBlack = UIColor.black.withAlphaComponent(Alphas.Transparent)
static let SemiOpaque = UIColor.black.withAlphaComponent(Alphas.SemiOpaque)
static let demoOverlay = UIColor.black.withAlphaComponent(0.6)
}
}
您可以将这些所有文件包装在Xcode项目中名为常量的常用组中。
并观看更多视频
虽然我更喜欢@ Francescu的方式(使用具有静态属性的结构),但您也可以定义全局常量和变量:
let someNotification = "TEST"
但是请注意,与局部变量/常量和类/结构属性不同,全局变量隐含地是惰性的,这意味着它们在被第一次访问时被初始化。
建议阅读:全局变量和局部变量,以及Swift中的全局变量不是变量
链接地址: http://www.djcxy.com/p/57439.html