"값을 할당 할 수 없습니다 '자체가'불변"신속한 UIColor 확장 : 내가 직접 알파를 변경하는 <code>UIColor</code>의 확장을 썼다
public extension UIColor {
public var rgbaComponents: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var components: [CGFloat] {
let c = cgColor.components!
if c.count == 4 {
return c
}
return [c[0], c[0], c[0], c[1]]
}
let r = components[0]
let g = components[1]
let b = components[2]
let a = components[3]
return (red: r, green: g, blue: b, alpha: a)
}
public var alpha: CGFloat {
get {
return cgColor.alpha
}
set {
var rgba = rgbaComponents
self = UIColor(red: rgba.red // error here: "cannot assign to value: 'self' is immutable"
, green: rgba.green, blue: rgba.blue, alpha: newValue)
}
}
}
을하지만 오류가 :
cannot assign to value: 'self' is immutable
은 그것은 그러나 자기에 할당 할 날짜의 확장
public extension Date {
public var day: Int {
get {
return Calendar.current.component(.day, from: self)
}
set {
let allowedRange = Calendar.current.range(of: .day, in: .month, for: self)!
guard allowedRange.contains(newValue) else { return }
let currentDay = Calendar.current.component(.day, from: self)
let daysToAdd = newValue - currentDay
if let date = Calendar.current.date(byAdding: .day, value: daysToAdd, to: self) {
self = date // This is OK
}
}
}
}
OK 때문에의 UIColor
이 이리저리이다 m NSObject
날짜가 Swift 구조체입니까? 근본 원인은 무엇입니까?
코드로 솔루션을 게시 할 수 있습니까? @Upholder –
불행히도 확장 기능을 사용하는 솔루션이 없다고 생각합니다. 새로운 것을 만들지 않고는'UIColor'의 알파를 변경할 수 없으며, 변경할 수 없으므로 자기를 바꿀 수 없습니다. –
대단히 감사합니다! – Phoenix19