所以我确实找到了一种通过编写某种适配器来使用它的方法。
我很确定它可以做得更好,如果有人有办法这样做,请不要犹豫提供您的解决方案,但这就是我现在所做的
public struct Corners: OptionSet {
private enum Corner: Int, CustomStringConvertible {
case TopLeft=1
case TopRight=2
case BottomLeft=4
case BottomRight=8
case All=16
public var description: String {
var shift = 0
while (rawValue.hashValue >> shift != 1) { shift += 1 }
return ["topleft", "topright", "bottomleft", "bottomright", "all"][shift]
}
}
public let rawValue: Int
public init(rawValue: Int) { self.rawValue = rawValue }
private init(_ shape: Corner) { self.rawValue = shape.rawValue }
static let TopLeft = Corners(Corner.TopLeft)
static let TopRight = Corners(Corner.TopRight)
static let BottomLeft = Corners(Corner.BottomLeft)
static let BottomRight = Corners(Corner.BottomRight)
static let All = [TopLeft, TopRight, BottomLeft, BottomRight]
}
// Needed to split the string that's provided in the @IBInspectable. and remove any possible spaces the user introduced
extension String {
func getStrings() -> [String] {
var stringArray: [String] = []
let strings = self.characters.split{$0 == ","}.map(String.init)
for s in strings {
let string = s.removeSpaces()
stringArray.append(string)
}
return stringArray
}
func removeSpaces() -> String {
if self.characters.first == " " {
var copy = self
copy.characters.removeFirst()
return copy.removeSpaces()
} else {
return self
}
}
}
然后我的@IBInspectable
样子是这样的
var corners = [Corners.TopLeft]
@IBInspectable public var onCorners: String = "" {
willSet {
corners = []
for s in newValue.lowercased().getStrings() {
switch s {
case "topleft":
corners.append(Corners.TopLeft)
case "topright":
corners.append(Corners.TopRight)
case "bottomleft":
corners.append(Corners.BottomLeft)
case "bottomright":
corners.append(Corners.BottomRight)
case "all":
corners = Corners.All
default:
return
}
}
}
didSet {
// Do your logic here
}
}