Use modern Swift 2.0

This commit is contained in:
Kyle Fuller
2015-09-25 10:40:58 -07:00
parent a0bde992c2
commit 878c5cfde8
3 changed files with 14 additions and 17 deletions

View File

@@ -1,13 +1,13 @@
import Foundation
/// A container for template variables. /// A container for template variables.
public class Context : Equatable { public class Context : Equatable {
var dictionaries:[Dictionary<String, AnyObject>] var dictionaries:[[String:AnyObject]]
public init(dictionary:Dictionary<String, AnyObject>) { /// Initialise a Context with a dictionary
public init(dictionary:[String:AnyObject]) {
dictionaries = [dictionary] dictionaries = [dictionary]
} }
/// Initialise an empty Context
public init() { public init() {
dictionaries = [] dictionaries = []
} }
@@ -16,7 +16,7 @@ public class Context : Equatable {
/// Retrieves a variable's value, starting at the current context and going upwards /// Retrieves a variable's value, starting at the current context and going upwards
get { get {
for dictionary in Array(dictionaries.reverse()) { for dictionary in Array(dictionaries.reverse()) {
if let value:AnyObject = dictionary[key] { if let value = dictionary[key] {
return value return value
} }
} }
@@ -26,24 +26,21 @@ public class Context : Equatable {
/// Set a variable in the current context, deleting the variable if it's nil /// Set a variable in the current context, deleting the variable if it's nil
set(value) { set(value) {
if dictionaries.count > 0 { if var dictionary = dictionaries.popLast() {
var dictionary = dictionaries.removeLast()
dictionary[key] = value dictionary[key] = value
dictionaries.append(dictionary) dictionaries.append(dictionary)
} }
} }
} }
public func push() { /// Push a new level into the Context
push(Dictionary<String, AnyObject>()) public func push(dictionary:[String:AnyObject]? = nil) {
dictionaries.append(dictionary ?? [:])
} }
public func push(dictionary:Dictionary<String, AnyObject>) { /// Pop the last level off of the Context
dictionaries.append(dictionary) public func pop() -> [String:AnyObject]? {
} return dictionaries.popLast()
public func pop() {
dictionaries.removeLast()
} }
} }

View File

@@ -28,7 +28,7 @@ public class TokenParser {
} }
private var tokens:[Token] private var tokens:[Token]
private var tags = Dictionary<String, TagParser>() private var tags = [String:TagParser]()
public init(tokens:[Token]) { public init(tokens:[Token]) {
self.tokens = tokens self.tokens = tokens

View File

@@ -25,7 +25,7 @@ public struct Variable : Equatable {
for bit in lookup() { for bit in lookup() {
if let context = current as? Context { if let context = current as? Context {
current = context[bit] current = context[bit]
} else if let dictionary = current as? Dictionary<String, AnyObject> { } else if let dictionary = current as? [String:AnyObject] {
current = dictionary[bit] current = dictionary[bit]
} else if let array = current as? [AnyObject] { } else if let array = current as? [AnyObject] {
if let index = Int(bit) { if let index = Int(bit) {