• Swift-扩展


    import UIKit   导入此框架

     

    扩展的格式

      

    extension + 需要扩展的类名{

     

    }

    以下是对数据持久化UserDefaults的扩展代码

    extension UserDefaults{

        enum TestData: String,UserDefaultSettable{

            case name

            case age

       }

     

    public protocol UserDefaultSettable{

        var uniqueKey:String { get}

    }

     

    public extension UserDefaultSettable where Self: RawRepresentable, Self.RawValue == String {

        

        public func store(value: Any?){

            UserDefaults.standard.set(value, forKey: uniqueKey)

        }

        

        public var storedValue: Any? {

            return UserDefaults.standard.value(forKey: uniqueKey)

        }

     

        public var uniqueKey: String {

            return "(Self.self).(rawValue)"

        }

        

        public func store(value: Bool) {

            

        }

    //    public var storedBool: Bool {

    //        // ......

    //    }

    //    

    }

     

  • 相关阅读:
    微信小程序开发之搞懂flex布局2——flex container
    微信小程序开发之搞懂flex布局3——Flex Item
    微信小程序开发之搞懂flex布局4——Main Axis
    微信小程序开发之搞懂flex布局5——cross axis
    【微信小程序开发之坑】javascript创建date对象
    【微信小程序开发】快速开发一个动态横向导航模板并使用
    Centos下配置tomcat7的https证书
    让你的程序更可读
    Egg 中 Controller 最佳实践
    React与Preact差异之 -- setState
  • 原文地址:https://www.cnblogs.com/lcl15/p/6479720.html
Copyright © 2020-2023  润新知