Debugging.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. description += " \(descriptionForObject(self.firstItem))"
  57. if self.firstAttribute != .NotAnAttribute {
  58. description += ".\(self.firstAttribute.snp_description)"
  59. }
  60. description += " \(self.relation.snp_description)"
  61. if let secondItem: AnyObject = self.secondItem {
  62. description += " \(descriptionForObject(secondItem))"
  63. }
  64. if self.secondAttribute != .NotAnAttribute {
  65. description += ".\(self.secondAttribute.snp_description)"
  66. }
  67. if self.multiplier != 1.0 {
  68. description += " * \(self.multiplier)"
  69. }
  70. if self.secondAttribute == .NotAnAttribute {
  71. description += " \(self.constant)"
  72. } else {
  73. if self.constant > 0.0 {
  74. description += " + \(self.constant)"
  75. } else if self.constant < 0.0 {
  76. description += " - \(CGFloat.abs(self.constant))"
  77. }
  78. }
  79. if self.priority != 1000.0 {
  80. description += " ^\(self.priority)"
  81. }
  82. description += ">"
  83. return description
  84. }
  85. internal var snp_makerFile: String? {
  86. return self.snp_constraint?.makerFile
  87. }
  88. internal var snp_makerLine: UInt? {
  89. return self.snp_constraint?.makerLine
  90. }
  91. }
  92. private var labelKey = ""
  93. private func descriptionForObject(object: AnyObject) -> String {
  94. let pointerDescription = NSString(format: "%p", ObjectIdentifier(object).uintValue)
  95. var desc = ""
  96. desc += object.dynamicType.description()
  97. if let object = object as? View {
  98. desc += ":\(object.snp_label ?? pointerDescription)"
  99. } else if let object = object as? LayoutConstraint {
  100. desc += ":\(object.snp_label ?? pointerDescription)"
  101. } else {
  102. desc += ":\(pointerDescription)"
  103. }
  104. if let object = object as? LayoutConstraint, let file = object.snp_makerFile, let line = object.snp_makerLine {
  105. desc += "@\(file)#\(line)"
  106. }
  107. desc += ""
  108. return desc
  109. }
  110. private extension NSLayoutRelation {
  111. private var snp_description: String {
  112. switch self {
  113. case .Equal: return "=="
  114. case .GreaterThanOrEqual: return ">="
  115. case .LessThanOrEqual: return "<="
  116. }
  117. }
  118. }
  119. private extension NSLayoutAttribute {
  120. private var snp_description: String {
  121. #if os(iOS) || os(tvOS)
  122. switch self {
  123. case .NotAnAttribute: return "notAnAttribute"
  124. case .Top: return "top"
  125. case .Left: return "left"
  126. case .Bottom: return "bottom"
  127. case .Right: return "right"
  128. case .Leading: return "leading"
  129. case .Trailing: return "trailing"
  130. case .Width: return "width"
  131. case .Height: return "height"
  132. case .CenterX: return "centerX"
  133. case .CenterY: return "centerY"
  134. case .Baseline: return "baseline"
  135. case .FirstBaseline: return "firstBaseline"
  136. case .TopMargin: return "topMargin"
  137. case .LeftMargin: return "leftMargin"
  138. case .BottomMargin: return "bottomMargin"
  139. case .RightMargin: return "rightMargin"
  140. case .LeadingMargin: return "leadingMargin"
  141. case .TrailingMargin: return "trailingMargin"
  142. case .CenterXWithinMargins: return "centerXWithinMargins"
  143. case .CenterYWithinMargins: return "centerYWithinMargins"
  144. }
  145. #else
  146. switch self {
  147. case .NotAnAttribute: return "notAnAttribute"
  148. case .Top: return "top"
  149. case .Left: return "left"
  150. case .Bottom: return "bottom"
  151. case .Right: return "right"
  152. case .Leading: return "leading"
  153. case .Trailing: return "trailing"
  154. case .Width: return "width"
  155. case .Height: return "height"
  156. case .CenterX: return "centerX"
  157. case .CenterY: return "centerY"
  158. case .Baseline: return "baseline"
  159. default: return "default"
  160. }
  161. #endif
  162. }
  163. }