Post

Replies

Boosts

Views

Activity

How can I get a Subscriber to subscribe to get only 4 elements from an array?
Hello, I am trying to implement a subscriber which specifies its own demand for how many elements it wants to receive from a publisher. My code is below: import Combine var array = [1, 2, 3, 4, 5, 6, 7] struct ArraySubscriber<T>: Subscriber { typealias Input = T typealias Failure = Never let combineIdentifier = CombineIdentifier() func receive(subscription: any Subscription) { subscription.request(.max(4)) } func receive(_ input: T) -> Subscribers.Demand { print("input,", input) return .max(4) } func receive(completion: Subscribers.Completion<Never>) { switch completion { case .finished: print("publisher finished normally") case .failure(let failure): print("publisher failed due to, ", failure) } } } let subscriber = ArraySubscriber<Int>() array.publisher.subscribe(subscriber) According to Apple's documentation, I specify the demand inside the receive(subscription: any Subscription) method, see link. But when I run this code I get the following output: input, 1 input, 2 input, 3 input, 4 input, 5 input, 6 input, 7 publisher finished normally Instead, I expect the subscriber to only "receive" elements 1, 2, 3, 4 from the array. How can I accomplish this?
0
0
87
2w
Is super.init(frame: CGRect) the designated initialiser for a UICollectionViewCell class?
I was writing some code for a UICollectionViewCell class and I normally include this initialiser when I create a new subclass for UICollectionViewCell, and saw that frequently this is paired with a required init?(coder: NSCoder) initialiser, so I was wondering if super.init(frame: CGRect) is the designated initialiser for the UICollectionViewCell class?
Topic: UI Frameworks SubTopic: UIKit
1
0
243
Jul ’25
Who vends the item identifier and the index path arguments to the cell provider closure of a diffable data source?
I was reading over the documentation of the CellProvider struct for the diffable data source initialiser, and the CellProvider is defined as a closure which takes three arguments, a table view, an index path, and an item identifier. My question is, who vends the index path and the item identifier to this closure? My thinking is that it is the data source who vends these items because it adopts the UITableViewDataSource protocol which acts as the source of data for this view.
Topic: UI Frameworks SubTopic: UIKit Tags:
0
0
239
Jul ’25
Documentation for UIListContentConfiguration is incomplete in UIKit
The documentation for UIListContentConfiguration states that I can get the default content configuration of a UICollectionViewCell by getting the cells default content configuration. In practice, however, this presents an error in the compiler: let choiceRegistration = UICollectionView.CellRegistration<UICollectionViewCell, SurveyItem> { cell, indexPath, item in cell.configurationUpdateHandler = { cell, state in switch item.type { case .choice(let letter, let text): cell.defaultContentConfiguration() ... } The error shown when calling cell.defaultContentConfiguration() is Value of type 'UICollectionViewCell' has no member 'defaultContentConfiguration'. Instead, I need to initialise a content configuration using UIListContentConfiguration.cell() to get the default content configuration. This needs to be reflected in the documentation in UIKit.
Topic: UI Frameworks SubTopic: UIKit Tags:
4
0
97
Jul ’25
Trying to Apply Content Configuration on a UICollectionReusableView
I was trying to register a UICollectionReusableView in my UICollectionView.SupplementaryRegistration handler in order to apply a content configuration to apply the text and secondaryText properties to the reusable view. However, when I try to apply the content configuration on the header view object that I return in my cell registration handler, I get an error that says: "Value of type 'UICollectionReusableView' has no member 'contentConfiguration'". How can apply content configuration to a UICollectionReusableView? This is my code: let headerRegistration = UICollectionView.SupplementaryRegistration<UICollectionReusableView>(elementKind: UICollectionView.elementKindSectionHeader) { header, elementKind, indexPath in // Header configuration is handled in the header's init var configuration = UIListContentConfiguration.plainHeader() configuration.text = "Current Emotional State" configuration.secondaryText = "What best describes how you're feeling right now?" header.contentConfiguration = configuration }
Topic: UI Frameworks SubTopic: UIKit
1
0
118
Jul ’25
Modifying an associated value of an existing enum instance
Hi, I would like to modify an associated value of an existing enum instance of the the following enum: enum FilterItem: Equatable, Hashable { case work(isSelected: Bool) ... var filterName: String { switch self { case .work: return "Work" ... } } var isSelected: Bool { switch self { case .work(let isSelected): return isSelected ... } } I want to be able to switch on the FilterItem type and then to be able to modify the isSelected property of the instance like: let itemToChange: FilterItem switch item { case .work(let isSelected): itemToChange.isSelected = !isSelected I know the above code doesn't compile, but I was wondering if there was a way I could modify my enum instance without creating a totally new enum instance.
5
0
356
Jul ’25
Position is Ambiguous for UILabel Warning for label I added programmatically
Hi, I have added a UILabel to my collection view cell programmatically and have set up constraints using auto layout. The centerX and centerY constraints of the label are equal to the centerX and centerY anchors of the content view. However, the label is appearing in the top left corner of the cell rather than in the center where I expect it to be. Here is my code: protocol TapLabelCollectionViewCellDelegate: AnyObject { func incrementNumberOfTaps(index: Int) } class TapLabelCollectionViewCell: UICollectionViewCell { var taplabel: UILabel! var delegate: TapLabelCollectionViewCellDelegate? var index: Int! static let identifier = "tapLabelCellIdentifier" override init(frame: CGRect) { super.init(frame: frame) taplabel = UILabel() taplabel.translatesAutoresizingMaskIntoConstraints = false addSubview(taplabel) NSLayoutConstraint.activate([ taplabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), taplabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor) ]) setUpTapGestureRecognizer() } required init?(coder: NSCoder) { super.init(coder: coder) } func setUpTapGestureRecognizer() { let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(incrementNumberOfTaps)) print("tap Label,", taplabel) taplabel.addGestureRecognizer(tapGestureRecognizer) } @objc func incrementNumberOfTaps() { delegate?.incrementNumberOfTaps(index: index) } } I am also getting the following warning in Xcode
Topic: UI Frameworks SubTopic: UIKit
2
0
144
Jul ’25
Label I Added in Storyboard Appears nil when I try to Dequeue a Cell Using a Data Source Cell Provider
I have added a UICollectionViewCell to my storyboard, and I added a UILabel to my UICollectionViewCell in storyboard. I have created a cell registration using UICollectionView.CellRegistration and have implemented the cellProvider closure for the datasource which dequeue a collection view cell of type TapLabelCollectionViewCell(I have subclassed the cell in my storyboard to this class). In my TapLabelCollectionViewCell, I am trying to set the tap gesture recogniser on the label, but the label appears nil, which I've connected using an @IBOutlet. Why is this and how can I fix it? My code : // UI View Controller: class TapGridViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. tapGridCollectionView.collectionViewLayout = createLayout() configureDataSource() applySnapshot() } func configureDataSource() { let cellRegistration = UICollectionView.CellRegistration<TapLabelCollectionViewCell, CellItem>(handler: { (cell: TapLabelCollectionViewCell, indexPath: IndexPath, item: CellItem) in cell.taplabel.text = String(item.labelCount) }) dataSource = UICollectionViewDiffableDataSource(collectionView: tapGridCollectionView, cellProvider: { (collectionView: UICollectionView, indexPath: IndexPath, item: CellItem) in let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item) cell.delegate = self cell.index = indexPath.row return cell }) } } // UI Collection View Cell: protocol TapLabelCollectionViewCellDelegate: AnyObject { func incrementNumberOfTaps(index: Int) } class TapLabelCollectionViewCell: UICollectionViewCell { @IBOutlet var taplabel: UILabel! var delegate: TapLabelCollectionViewCellDelegate? var index: Int! static let identifier = "tapLabelCellIdentifier" override init(frame: CGRect) { super.init(frame: frame) setUpTapGestureRecognizer() } required init?(coder: NSCoder) { super.init(coder: coder) } func setUpTapGestureRecognizer() { let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(incrementNumberOfTaps)) print("tap Label,", taplabel) taplabel.addGestureRecognizer(tapGestureRecognizer) } @objc func incrementNumberOfTaps() { delegate?.incrementNumberOfTaps(index: index) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
9
0
89
Jul ’25
Cell Delegate Method Not Being Called
I have set up a collection view cell programmatically which I am registering in the cell registration of a diffable data source. I have set up a delegate for the cell, and the view controller which contains the collection view as the delegate for the cell. However, calling the cell delegate method from the cell is not calling the method in the view controller. This is my code: `// in my view controller: cell.delegate = self // in my collection view cell: delegate?.repromptLLMForIconName(iconName, index: index, emotion: emotion, red: red, green: green, blue: blue) ` But although the delegate method in the collection view gets called, my method implementation in the view controller does not. What could possibly be going wrong?
Topic: UI Frameworks SubTopic: UIKit
11
0
117
Jul ’25
Collection Reusable View Added Via Storyboard Not Registering with the Collection View
When I set up a UICollectionReusableView cell in storyboard and try to dequeue a cell of this type, my app crashes with this error message: Steps I've gone through to add my collection reusable view in storyboard: I added a collection reusable view by dragging the item from the panel of objects to my collection view. I created a class called EmotionFiltersHeaderView which subclasses UICollectionReusableView that contains a reference to an IBOutlet var for the label I added to the reusable view (also in storyboard). I changed the class name of the reusable view in the storyboard to my custom class name, EmotionFiltersHeaderView. I added an identifier to the reusable view and called it "emotionFiltersHeaderReusableView". I've included the header view in the boundary supplementary items property of the section where I'm adding the header view. This is my code: `let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(37.0)) let header = NSCollectionLayoutBoundarySupplementaryItem( layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .topLeading) section.boundarySupplementaryItems = [header] emotionFiltersDataSource.supplementaryViewProvider = { collectionView, kind, indexPath in guard let headerView = collectionView.dequeueReusableSupplementaryView( ofKind: ElementKind.sectionHeader.rawValue, withReuseIdentifier: "emotionFiltersHeaderReusableView", for: indexPath ) as? EmotionFiltersHeaderView else { fatalError("Could not dequeue header view as EmotionFiltersHeaderView") } let sectionTitle = Section.allCases[indexPath.section].rawValue headerView.sectionTitleLabel.text = sectionTitle return headerView } enum ElementKind: String { case sectionHeader = "section-header-element-kind" }`
Topic: UI Frameworks SubTopic: UIKit
4
0
77
Jul ’25