美文网首页
Swift 中 String(describing: self)

Swift 中 String(describing: self)

作者: SoaringHeart | 来源:发表于2020-12-29 17:30 被阅读0次

    总结:

    #function String(describing: self) type(of: self)
    方法 testFunc() SwiftExpand.SettingsService SettingsService
    类方法 testFunc() SettingsService SettingsService.Type
    属性 isNotificationsEnabled SwiftExpand.SettingsService SettingsService
    类属性 isNotificationsEnabled SettingsService SettingsService.Type

    备注:SwiftExpand为命名空间,SettingsService 为类,

    public extension UserDefaults{
        
        subscript<T>(key: String) -> T? {
            get {
                return value(forKey: key) as? T
            }
            set {
                set(newValue, forKey: key)
            }
        }
    }
    
    public class SettingsService {
        
        public required init() {
            
        }
        
        public var isNotificationsEnabled: Bool {
            get {
                let isEnabled = UserDefaults.standard.value(forKey: #function) as? Bool
                return isEnabled ?? true
            }
            set {
                DDLog(#function, String(describing: self), type(of: self))
                UserDefaults.standard.setValue(newValue, forKey: #function)
            }
        }
       
        public static var isNotificationsEnabled: Bool {
            get {
                let isEnabled = UserDefaults.standard.value(forKey: #function) as? Bool
                return isEnabled ?? true
            }
            set {
                DDLog(#function, String(describing: self), type(of: self))
                UserDefaults.standard.setValue(newValue, forKey: #function)
            }
        }
    
        public func testFunc() {
            DDLog(#function, String(describing: self), type(of: self))
        }
        
        public static func testFunc() {
            DDLog(#function, String(describing: self), type(of: self))
        }
    }
    
    🌰🌰:
            let service = SettingsService()
            service.testFunc()
            service.isNotificationsEnabled = false
    
            SettingsService.isNotificationsEnabled = false
            SettingsService.testFunc()
    
    2020-12-30 01:28:23.586 UserDefaults+Helper.swift.testFunc()[line 158]: testFunc(), SwiftExpand.SettingsService, SettingsService
    2020-12-30 01:28:23.586 UserDefaults+Helper.swift.testFunc()[line 162]: testFunc(), SettingsService, SettingsService.Type
    2020-12-30 01:28:23.586 UserDefaults+Helper.swift.isNotificationsEnabled[line 122]: isNotificationsEnabled, SwiftExpand.SettingsService, SettingsService
    2020-12-30 01:28:23.586 UserDefaults+Helper.swift.isNotificationsEnabled[line 143]: isNotificationsEnabled, SettingsService, SettingsService.Type
    
    

    相关文章

      网友评论

          本文标题:Swift 中 String(describing: self)

          本文链接:https://www.haomeiwen.com/subject/cgqqoktx.html