我的XML布局:
protocol JSONDecodable {
// This is used so you can have a unified way to instantiate an instance without relying on sub-classing NSObject
init()
// This can be implemented so you can manually decode a single object of the type from a dictionary
static func decodeFromJSON(json: AnyObject?) -> AnyObject?
// This is used so that you can manually set the values to the object. This is required as Swift reflection doesn't support dynamic property setting
func setValueForKey(value: AnyObject?, forKey: String)
}
// This class is responsible for decoding a JSON dictionary into an object
class JSONDecoder<T:JSONDecodable>: NSObject {
//MARK: Initialization
required override init() {
// do any initialization here
}
//MARK: Public Methods
/**
Creates a single object from the JSON. This method should only be used if the JSON will only ever contain a single object
:json: A dictionary of data
:returns: An object of the given type
*/
func toSingle(json: AnyObject?) -> T? {
// process single object, and return an array with the one object
if let dict = json as? [NSObject: AnyObject] {
return self.makeObject(dict)
}
return nil
}
/**
Creates a list of objects from the JSON. This method should only be used if the JSON will contain multiple objects
:json: A dictionary of data
:returns: An list of objects of the given type
*/
func toArray(json: AnyObject?) -> [T]? {
// process array
if let arr = json as? [AnyObject] {
return self.makeObjects(arr)
} else if let dict = json as? [NSObject: AnyObject] {
// process single object, and return an array with the one object
var arr = [T]()
arr.append(self.makeObject(dict))
return arr
}
return nil
}
//MARK: The Magic
private func makeObjects(jsonArray: [AnyObject]?) -> [T]? {
var returnArray: [T] = [T]()
if let jArray = jsonArray {
for jObject in jArray {
if let dict = jObject as? [NSObject: AnyObject] {
returnArray.append(self.makeObject(dict))
}
}
}
if returnArray.count > 0 {
return returnArray
} else {
return nil
}
}
private func makeObject(jsonDict: [NSObject: AnyObject]) -> T {
var returnObject = T.self() // this is where the init() function in the protocol comes in handy. It allows us to use generics to create a dynamic instance of our object
for (key, value) in jsonDict {
if let k = key as? String {
returnObject.setValueForKey(value, forKey: k) // this is where the setValueForKey(value: AnyObject?, forKey: String) function in the protocol comes in handy. It allows us to let the object it's self set it's own values.
}
}
return returnObject
}
}
// This is an example class that implements the protocol which means it can be sent through the decoding process
class Employee: NSManagedObject, JSONDecodable {
//MARK: - Properties
var employeID: Int!
var name: Int!
var hireDate: NSDate?
var department: Department?
//MARK: - Initialization
override required init() {
// Necessary to satisfy the JSONDecodable protocol
}
static func decodeFromJSON(json: AnyObject?) -> AnyObject? {
var decoder = JSONDecoder<Employee>()
return decoder.toSingle(json)
}
func setValueForKey(value: AnyObject?, forKey: String) {
switch (forKey) {
case "employeID":
self.employeID = value as! Int
case "name":
self.name = value as! String
case "hireDate":
if let v = value as? String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
self.hireDate = dateFormatter.dateFromString(v)
}
case "department":
if let v = value as? [NSObject: AnyObject] {
if let dept = Department.decodeFromJSON(dict) as? Department {
self.department = dept
}
}
default:
NSLog("[setValueForKey] Unable to find property \(forKey)")
}
}
}
我有 default.css :
<LinearLayout android:orientation="vertical"...>
<View android:id="@+id/bar" .../>
<TextView android:id="@+id/text" .../>
</LinearLayout>
custom.css :
#bar{ background-color: red; }
#text{ color: red; }
如果我这样做&#34; PixateFreestyle.init(this); &#34;,我可以通过应用default.css(View - red,TextView - red)看到它。
如果我这样做&#34; PixateFreestyle.init(这,&#34; custom.css&#34;); &#34;,我可以通过应用custom.css看到(视图 - 不是红色,默认视图值,TextView - 绿色)。
如何合并default.css和custom.css?我想获得View - red,TextView - green。
换句话说,我想用两个从两个文件应用的样式初始化库。
答案 0 :(得分:0)
仅使用绿色更改default.css
,因为如果你合并两个css文件,它只需要稍后一个,或者你可以在几秒钟之后使用动态更改css文件但是一次合并我认为它不可能。
答案 1 :(得分:0)
为了解决我的问题,我正在使用default.css,然后我正在下载custom.css并将其应用于默认值。