Skip to content

Commit 61ad14f

Browse files
committed
refactor remaking packets
1 parent 4c7b317 commit 61ad14f

File tree

1 file changed

+30
-53
lines changed

1 file changed

+30
-53
lines changed

SwiftIO/SocketPacket.swift

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -188,72 +188,49 @@ class SocketPacket {
188188
return message + "]"
189189
}
190190

191-
private func fillInArray(arr:NSArray) -> NSArray {
192-
var newArr = [AnyObject](count: arr.count, repeatedValue: 0)
193-
// println(arr)
191+
func fillInPlaceholders() {
192+
var newArr = NSMutableArray(array: self.data!)
194193

195-
for i in 0..<arr.count {
196-
if let nest = arr[i] as? NSArray {
197-
newArr[i] = self.fillInArray(nest)
198-
} else if let dict = arr[i] as? NSDictionary {
199-
newArr[i] = self.fillInDict(dict)
200-
} else if let str = arr[i] as? String {
194+
for i in 0..<self.data!.count {
195+
if let str = self.data?[i] as? String {
201196
if let num = str["~~(\\d)"].groups() {
202197
newArr[i] = self.binary[num[1].toInt()!]
203198
} else {
204-
newArr[i] = arr[i]
199+
newArr[i] = str
205200
}
206-
} else {
207-
newArr[i] = arr[i]
201+
} else if self.data?[i] is NSDictionary || self.data?[i] is NSArray {
202+
newArr[i] = self._fillInPlaceholders(self.data![i])
208203
}
209204
}
210205

211-
return newArr
206+
self.data = newArr
212207
}
213208

214-
private func fillInDict(dict:NSDictionary) -> NSDictionary {
215-
var newDict = [String: AnyObject]()
216-
217-
for (key, value) in dict {
218-
newDict[key as String] = value
209+
private func _fillInPlaceholders(data:AnyObject) -> AnyObject {
210+
if let str = data as? String {
211+
if let num = str["~~(\\d)"].groups() {
212+
return self.binary[num[1].toInt()!]
213+
} else {
214+
return str
215+
}
216+
} else if let dict = data as? NSDictionary {
217+
var newDict = NSMutableDictionary(dictionary: dict)
219218

220-
// If the value is a string we need to check
221-
// if it is a placeholder for data
222-
if let str = value as? String {
223-
if let num = str["~~(\\d)"].groups() {
224-
newDict[key as String] = self.binary[num[1].toInt()!]
225-
} else {
226-
newDict[key as String] = str
227-
}
228-
} else if let nestDict = value as? NSDictionary {
229-
newDict[key as String] = self.fillInDict(nestDict)
230-
} else if let arr = value as? NSArray {
231-
newDict[key as String] = self.fillInArray(arr)
219+
for (key, value) in dict {
220+
newDict[key as NSCopying] = _fillInPlaceholders(value)
232221
}
233-
}
234-
235-
return newDict
236-
}
237-
238-
func fillInPlaceholders() {
239-
var newArr = [AnyObject](count: self.data!.count, repeatedValue: 0)
240-
241-
for i in 0..<self.data!.count {
242-
if let str = self.data?[i] as? String {
243-
if let num = str["~~(\\d)"].groups() {
244-
newArr[i] = self.binary[num[1].toInt()!]
245-
} else {
246-
newArr[i] = str
247-
}
248-
} else if let arr = self.data?[i] as? NSArray {
249-
newArr[i] = self.fillInArray(arr)
250-
} else if let dict = self.data?[i] as? NSDictionary {
251-
newArr[i] = self.fillInDict(dict)
252-
} else {
253-
newArr[i] = self.data![i]
222+
223+
return newDict
224+
} else if let arr = data as? NSArray {
225+
var newArr = NSMutableArray(array: arr)
226+
227+
for i in 0..<arr.count {
228+
newArr[i] = _fillInPlaceholders(arr[i])
254229
}
230+
231+
return newArr
232+
} else {
233+
return data
255234
}
256-
257-
self.data = newArr
258235
}
259236
}

0 commit comments

Comments
 (0)