Post

Replies

Boosts

Views

Created

UICollectionViewCell subclass cannot use backgroundView property?
I made a subclass of UICollectionViewCell and Swift is complaining that I'm not allowed to use superclass properties like bounds, backgroundView or selectedBackgroundView. This very code was given by Appleʼs documentation - https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/changing_the_appearance_of_selected_and_highlighted_cells so I don't understand why it's disallowed. I declared class MyCollectionViewCell: UICollectionViewCell method awakeFromNib. class MyCollectionViewCell: UICollectionViewCell { ... override class func awakeFromNib() { ... let redView = UIView(frame: bounds) redView.backgroundColor =  colorLiteral(red: 1, green: 0, blue: 0, alpha: 1) self.backgroundView = redView let blueView = UIView(frame: bounds) blueView.backgroundColor =  colorLiteral(red: 0, green: 0, blue: 1, alpha: 1) self.selectedBackgroundView = blueView Xcode 12.2 gives an error about bounds, backgroundView, and selectedBackgroundView saying stuff like "Instance member 'bounds' cannot be used on type 'MyCollectionViewCell'". If I declare a variable as UICollectionViewCell I can set these properties in it, but a function in a subclass of UICollectionViewCell cannot access its own properties?
2
0
880
Dec ’20
Define Array of protocol which conforms to Identifiable
I've got Yet Another Protocol Question. Sorry, folks.The Swift 5.1 manual describes creating a `Collection` where the `Element` is a simple protocol, but doesn't explain how to make a Collection where the Element is a protocol with an associatedtype. In my case it's a protocol `Playable` that requires conformance to `Identifiable`.I am fine with requiring the `Identifiable.ID` type to be `Int` for all types which adopt `Playable`, but I don't know how to express that qualification/requirement. The bits hit the fan when I try to declare something to be `[Playable]`.My playground code might make my question clear.protocol Playable: Identifiable // DOESN'T WORK where ID: Int { // DOESN'T HELP associatedtype ID = Int // DOESN'T HELP var id: Int { get } func play() } struct Music: Playable { var id: Int func play() { print("playing music #\(id)") } } (Music(id: 1)).play() // displays: "playing music #1\n" class Game: Playable { var id: Int init(id: Int) { self.id = id } func play() { print("playing game #\(id)") } } (Game(id: 2)).play() // displays: "playing game #2\n" enum Lottery: Int, Playable { case state = 100 case church case charity var id: Int { return self.rawValue } func play() { print("playing lottery \(self) #\(self.rawValue)") } } Lottery.church.play() // displays: "playing lottery church #101\n" var stuff: [Playable] = [] // error: Protocol 'Playable' can only be used as a generic constraint because it has Self or associated type requirements stuff.append(Music(id: 10)) stuff.append(Game(id: 24)) stuff.append(Lottery.charity) stuff.map { $0.id }My goal is to have different structs and classes conforming to Playable (and hence to Identifiable) able to live inside a Collection.
7
1
7.6k
Mar ’20
How can I view the Development-server private data for my iCloud test account in the CloudKit Dashboard?
When developing a CloudKit application, the standard recommendation is to create an iCloud account for testing. That makes a lot of sense as I’d like to keep developing code far from my personal iCloud account. But the CloudKit Dashboard won’t let me view the private database for that account. What do other people do to check their work?* clarification: I mean on the development server, not the production server.
3
0
1.7k
Jan ’18