Debugging.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-2015 SnapKit Team - https://github.com/SnapKit
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS) || os(tvOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. /**
  29. Used to allow adding a snp_label to a View for debugging purposes
  30. */
  31. public extension View {
  32. public var snp_label: String? {
  33. get {
  34. return objc_getAssociatedObject(self, &labelKey) as? String
  35. }
  36. set {
  37. objc_setAssociatedObject(self, &labelKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
  38. }
  39. }
  40. }
  41. /**
  42. Used to allow adding a snp_label to a LayoutConstraint for debugging purposes
  43. */
  44. public extension LayoutConstraint {
  45. public var snp_label: String? {
  46. get {
  47. return objc_getAssociatedObject(self, &labelKey) as? String
  48. }
  49. set {
  50. objc_setAssociatedObject(self, &labelKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
  51. }
  52. }
  53. override public var description: String {
  54. var description = "<"
  55. description += descriptionForObject(self)
  56. if let firstItem: AnyObject = self.firstItem {
  57. description += " \(descriptionForObject(firstItem))"
  58. }
  59. if self.firstAttribute != .NotAnAttribute {
  60. description += ".\(self.firstAttribute.snp_description)"
  61. }
  62. description += " \(self.relation.snp_description)"
  63. if let secondItem: AnyObject = self.secondItem {
  64. description += " \(descriptionForObject(secondItem))"
  65. }
  66. if self.secondAttribute != .NotAnAttribute {
  67. description += ".\(self.secondAttribute.snp_description)"
  68. }
  69. if self.multiplier != 1.0 {
  70. description += " * \(self.multiplier)"
  71. }
  72. if self.secondAttribute == .NotAnAttribute {
  73. description += " \(self.constant)"
  74. } else {
  75. if self.constant > 0.0 {
  76. description += " + \(self.constant)"
  77. } else if self.constant < 0.0 {
  78. description += " - \(CGFloat.abs(self.constant))"
  79. }
  80. }
  81. if self.priority != 1000.0 {
  82. description += " ^\(self.priority)"
  83. }
  84. description += ">"
  85. return description
  86. }
  87. internal var snp_makerFile: String? {
  88. return self.snp_constraint?.makerFile
  89. }
  90. internal var snp_makerLine: UInt? {
  91. return self.snp_constraint?.makerLine
  92. }
  93. }
  94. private var labelKey = ""
  95. private func descriptionForObject(object: AnyObject) -> String {
  96. let pointerDescription = NSString(format: "%p", ObjectIdentifier(object).uintValue)
  97. var desc = ""
  98. desc += object.dynamicType.description()
  99. if let object = object as? View {
  100. desc += ":\(object.snp_label ?? pointerDescription)"
  101. } else if let object = object as? LayoutConstraint {
  102. desc += ":\(object.snp_label ?? pointerDescription)"
  103. } else {
  104. desc += ":\(pointerDescription)"
  105. }
  106. if let object = object as? LayoutConstraint, let file = object.snp_makerFile, let line = object.snp_makerLine {
  107. desc += "@\(file)#\(line)"
  108. }
  109. desc += ""
  110. return desc
  111. }
  112. private extension NSLayoutRelation {
  113. private var snp_description: String {
  114. switch self {
  115. case .Equal: return "=="
  116. case .GreaterThanOrEqual: return ">="
  117. case .LessThanOrEqual: return "<="
  118. }
  119. }
  120. }
  121. private extension NSLayoutAttribute {
  122. private var snp_description: String {
  123. #if os(iOS) || os(tvOS)
  124. switch self {
  125. case .NotAnAttribute: return "notAnAttribute"
  126. case .Top: return "top"
  127. case .Left: return "left"
  128. case .Bottom: return "bottom"
  129. case .Right: return "right"
  130. case .Leading: return "leading"
  131. case .Trailing: return "trailing"
  132. case .Width: return "width"
  133. case .Height: return "height"
  134. case .CenterX: return "centerX"
  135. case .CenterY: return "centerY"
  136. case .LastBaseline: return "baseline"
  137. case .FirstBaseline: return "firstBaseline"
  138. case .TopMargin: return "topMargin"
  139. case .LeftMargin: return "leftMargin"
  140. case .BottomMargin: return "bottomMargin"
  141. case .RightMargin: return "rightMargin"
  142. case .LeadingMargin: return "leadingMargin"
  143. case .TrailingMargin: return "trailingMargin"
  144. case .CenterXWithinMargins: return "centerXWithinMargins"
  145. case .CenterYWithinMargins: return "centerYWithinMargins"
  146. }
  147. #else
  148. switch self {
  149. case .NotAnAttribute: return "notAnAttribute"
  150. case .Top: return "top"
  151. case .Left: return "left"
  152. case .Bottom: return "bottom"
  153. case .Right: return "right"
  154. case .Leading: return "leading"
  155. case .Trailing: return "trailing"
  156. case .Width: return "width"
  157. case .Height: return "height"
  158. case .CenterX: return "centerX"
  159. case .CenterY: return "centerY"
  160. case .LastBaseline: return "baseline"
  161. default: return "default"
  162. }
  163. #endif
  164. }
  165. }