     var dynamicPropertySets:[StylePropertySet.Type] { get { return [UIViewPropertySet.self, UILabelPropertySet.self, UIButtonPropertySet.self, UIImageViewPropertySet.self] } }
     
     required init() {
-        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
-        let documentsDirectory = paths[0]
-        let filename = documentsDirectory.stringByAppendingString("stylesheet.json")
-        let bundle = NSBundle(forClass: JSONStylesheet.self)
+        let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
+        let savedDirectory = paths[0]
+        let filename = savedDirectory.appending("/stylesheet.json")
+        let bundle = Bundle(for: JSONStylesheet.self)
+        let fileManager = FileManager.default
         
-        // First try loading stylesheet.json from documents directory.  If that fails, try to load stylesheet.json from bundle, and move a copy of that to documents directory. In order to dynamically update styling via the web at runtime, create code somewhere in your app that downloads the current stylesheet and writes it to the documents directory as "stylesheet.json"  If running in Interface Builder, only the bundle version is loaded, never the cached version.
+        // Compare the file modification date of the downloaded / copied version of stylesheet.json in the Documents directory, and the original version of stylesheet.json included in the app bundle. If the Documents version is more recent, load and parse that version.  Otherwise, use the bundle version and then copy it to the Documents directory.
         
-        #if TARGET_INTERFACE_BUILDER
-            if let path = bundle.pathForResource("stylesheet", ofType: "json"), let data = NSData(contentsOfFile:path), let json = (try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))) as? [[String : AnyObject]] {
-                parse(json)
-            }
-            return
-        #endif
+        if let savedAttributes = try? fileManager.attributesOfItem(atPath: filename), let savedDate = savedAttributes[FileAttributeKey.modificationDate] as? NSDate, let path = bundle.path(forResource: "stylesheet", ofType: "json"), let bundledAttributes = try? fileManager.attributesOfItem(atPath: path), let bundledDate = bundledAttributes[FileAttributeKey.modificationDate] as? NSDate  {
+            
+            if let data = NSData(contentsOfFile:filename), let json = (try? JSONSerialization.jsonObject(with: data as Data, options:[])) as? [[String : AnyObject]], savedDate.timeIntervalSinceReferenceDate >= bundledDate.timeIntervalSinceReferenceDate {
+                parse(json: json)
+            } else if let path = bundle.path(forResource: "stylesheet", ofType: "json"), let data = NSData(contentsOfFile:path), let json = (try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions(rawValue: 0))) as? [[String : AnyObject]] {
+                if let stringJSON = String(data:data as Data, encoding: String.Encoding.utf8) {
+                    do {
+                        try stringJSON.write(toFile: filename, atomically: true, encoding: String.Encoding.utf8)
+                    }
+                    catch {}
+                }
+                
+                parse(json: json)
+            }
+        }
+        else if let path = bundle.path(forResource: "stylesheet", ofType: "json"), let data = NSData(contentsOfFile:path), let json = (try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions(rawValue: 0))) as? [[String : AnyObject]] {
+            if let stringJSON = String(data:data as Data, encoding: String.Encoding.utf8) {
+                do {
+                    try stringJSON.write(toFile: filename, atomically: true, encoding: String.Encoding.utf8)
+                }
+                catch {}
+            }
+            parse(json: json)
+        }
         
-        if let data = NSData(contentsOfFile:filename), let json = (try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))) as? [[String : AnyObject]] {
-            parse(json)
-        } else if let path = bundle.pathForResource("stylesheet", ofType: "json"), let data = NSData(contentsOfFile:path), let json = (try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))) as? [[String : AnyObject]] {
-            if let stringJSON = String(data:data, encoding: NSUTF8StringEncoding) {
-                do {
-                    try stringJSON.writeToFile(filename, atomically: true, encoding: NSUTF8StringEncoding)
-                }
-                catch {}
-            }
-            parse(json)
-        }
+        return
     }
     
     private func parse(json:[[String : AnyObject]]) {
         for dictionary in json {
             if let styleClass = dictionary["styleClass"] as? String, let array = dictionary["properties"] as? [[String : AnyObject]] {
-                styleClasses.append((identifier: styleClass, styleClass: DynamicStyleClass(jsonArray: array, dynamicPropertySets: dynamicPropertySets)))
-            }
-        }
-        JSONStylesheet.cacheTimestamp = NSDate.timeIntervalSinceReferenceDate()
+                styleClasses.append((identifier: styleClass, styleClass: DynamicStyleClass(jsonArray: array, styleClass:styleClass, dynamicPropertySets: dynamicPropertySets)))
+            } else {
+                assert(false, "Error in JSON stylesheet, possibly missing a 'styleClass' String value, or a 'properties' array for one of the included style classes")
+            }
+        }
+        JSONStylesheet.cacheTimestamp = NSDate.timeIntervalSinceReferenceDate
         JSONStylesheet.cachedStylesheet = self
     }
 }
 
+

    var alpha: CGFloat?
    var layoutMargins:UIEdgeInsets?
    var tintColor:UIColor?
    var customUIViewStyleBlock:(UIView->())?
    mutating func setStyleProperty<T>(named name: String, toValue value: T) {
        switch name {
        case _ where name.isVariantOf("Background Color"):
            backgroundColor = value as? UIColor
        case _ where name.isVariantOf("Content Mode"):
            contentMode = value as? UIViewContentMode
        case _ where name.isVariantOf("User Interaction Enabled"):
            userInteractionEnabled = value as? Bool
        case _ where name.isVariantOf("Hidden"):
            hidden = value as? Bool
        case _ where name.isVariantOf("Border Color"):
            borderColor = (value as! CGColor)
        case _ where name.isVariantOf("Border Width"):
            borderWidth = value as? CGFloat
        case _ where name.isVariantOf("Corner Radius"):
            cornerRadius = value as? CGFloat
        case _ where name.isVariantOf("Corner Radius Percentage"):
            cornerRadiusPercentage = value as? CGFloat
        case _ where name.isVariantOf("Alpha"):
            alpha = value as? CGFloat
        case _ where name.isVariantOf("Layout Margins"):
            layoutMargins = value as? UIEdgeInsets
        case _ where name.isVariantOf("Tint Color"):
            tintColor = value as? UIColor
        case _ where name.isVariantOf("Masks To Bounds"):
            masksToBounds = value as? Bool
        case _ where name.isVariantOf("Clips To Bounds"):
            clipsToBounds = value as? Bool
        default :
            return

                }
            }
        }
        self.init()
        return nil
    }
}
