Skip to content

Instantly share code, notes, and snippets.

@mmuszynski
Last active May 3, 2022 18:33
Show Gist options
  • Save mmuszynski/ba6f7c443455553955bf0e4f12cc0a66 to your computer and use it in GitHub Desktop.
Save mmuszynski/ba6f7c443455553955bf0e4f12cc0a66 to your computer and use it in GitHub Desktop.
struct AnyMusicStaffViewElement: MusicStaffViewElement {
private var wrapped: MusicStaffViewElement
init(_ wrapped: MusicStaffViewElement) {
self.wrapped = wrapped
}
var unboxed: MusicStaffViewElement {
return self.wrapped
}
var asAnyMusicStaffViewElement: AnyMusicStaffViewElement {
AnyMusicStaffViewElement(self)
}
//MusicStaffViewElement protocol adoption
func path(in frame: CGRect) -> CGPath {
return wrapped.path(in: frame)
}
var aspectRatio: CGFloat {
return wrapped.aspectRatio
}
var heightInStaffSpace: CGFloat {
return wrapped.heightInStaffSpace
}
var anchorPoint: CGPoint {
return wrapped.anchorPoint
}
func offset(in clef: MusicClef) -> Int {
return wrapped.offset(in: clef)
}
func direction(in clef: MusicClef) -> MusicStaffViewElementDirection {
return wrapped.direction(in: clef)
}
}
//Tests
XCTAssertNotNil(MusicAccidental.natural.asAnyMusicStaffViewElement) //pass
XCTAssertNotNil(MusicAccidental.natural.asAnyMusicStaffViewElement.unboxed as? MusicAccidental) //pass
XCTAssertNotNil(AnyMusicStaffViewElement(MusicClef.bass) as? MusicClef) //fails
//Cast from 'AnyMusicStaffViewElement' to unrelated type 'MusicClef' always fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment