Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import UIKit
  2.  
  3. var map = "10,5\n++++++++++\n+-P------+\n+--------+\n+-----o--+\n++++++++++\n"
  4.  
  5. func parseData(mapString: String) -> Data {
  6. var converted = mapString.components(separatedBy: "\n").dropLast()
  7. var dict = [String:String]()
  8. let widthHeight = converted[0].components(separatedBy: ",")
  9. dict["width"] = widthHeight[0]
  10. dict["height"] = widthHeight[1]
  11. converted.remove(at: 0)
  12. dict["data"] = converted.joined()
  13. let json = try! JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
  14. return json
  15. }
  16.  
  17. func drawMap(json: Data) {
  18. let decoded = try! JSONSerialization.jsonObject(with: json, options: [])
  19. let dictFromJson = decoded as! [String:String]
  20. let tempArray = Array(dictFromJson["data"]!.characters)
  21. print(Array(dictFromJson["data"]!.characters))
  22. for index in 0...tempArray.count - 1 {
  23. if tempArray[index] == "-" {
  24. print(" ", terminator: "")
  25. } else {
  26. print(tempArray[index], terminator: "")
  27. }
  28. if (index + 1) % Int(dictFromJson["width"]!)! == 0 {
  29. print()
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement