Post

Replies

Boosts

Views

Activity

Reply to How to sort my Core Data entities using the new query properties from AppIntents
I found a solution thanks to this Gist. There's no way to directly convert from an EntityQuerySort to an NSSortDescriptor. Instead, we have to convert it manually: private func toSortDescriptor(_ sortedBy: [Sort<ArtistEntity>]) -> [NSSortDescriptor] { var sortDescriptors = [NSSortDescriptor]() if let sort = sortedBy.first { switch sort.by { case \.$name: sortDescriptors.append(NSSortDescriptor(keyPath: \EArtist.name, ascending: sort.order == .ascending)) default: break } } return sortDescriptors }
Aug ’22
Reply to How to get the page URL shared via the Share button on macOS?
Turns out that the NSSecureCoding content was the URL, but in hexadecimal. Here's how I'm converting it to String: let urlHex = dict.debugDescription .replacingOccurrences(of: "Optional(", with: "") .replacingOccurrences(of: ")", with: "") .replacingOccurrences(of: "<", with: "") .replacingOccurrences(of: ">", with: "") .replacingOccurrences(of: " ", with: "") guard let urlHexData = Data(fromHexEncodedString: urlHex) else { return } guard let url = String(data: urlHexData, encoding: .utf8) else { return } extension Data { // From http://stackoverflow.com/a/40278391 init?(fromHexEncodedString string: String) { func decodeNibble(u: UInt16) -> UInt8? { switch(u) { case 0x30 ... 0x39: return UInt8(u - 0x30) case 0x41 ... 0x46: return UInt8(u - 0x41 + 10) case 0x61 ... 0x66: return UInt8(u - 0x61 + 10) default: return nil } } self.init(capacity: string.utf16.count/2) var even = true var byte: UInt8 = 0 for c in string.utf16 { guard let val = decodeNibble(u: c) else { return nil } if even { byte = val << 4 } else { byte += val self.append(byte) } even = !even } guard even else { return nil } } }
Mar ’22
Reply to Swift, iOS15, UIKit, CollectionView header issue
I'm having this exact same problem. Here's my error message: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath: does not match the element kind it is being used for. When asked for a view of element kind 'UICollectionElementKindSectionHeader' the data source dequeued a view registered for the element kind 'FeedCollectionViewHeader'.'
Jun ’21
Reply to Layout bug with the new sidebar appearance of UICollectionView when switching between Regular and Compact modes
Just a few more details: I found that disabling autoresizing mask and setting individual constraints on the collection view fixes the problem: private func configureHierarchy() { &#9;&#9;collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout()) &#9;&#9;collectionView.delegate = self &#9;&#9;collectionView.translatesAutoresizingMaskIntoConstraints = false &#9;&#9;view.addSubview(collectionView) &#9;&#9;NSLayoutConstraint.activate([ &#9;&#9;&#9;&#9;collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), &#9;&#9;&#9;&#9;collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), &#9;&#9;&#9;&#9;collectionView.topAnchor.constraint(equalTo: view.topAnchor), &#9;&#9;&#9;&#9;collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) &#9;&#9;]) } Not sure thought if this is how it was supposed to work, or if it's a bug with the .flexibleWidth autoresize mask. The only thing that still bothers me is that it takes a while for the collection view to assume the .sidebar appearance. When opening the app for the first time, it seems that the collection view has the .sidebarPlain appearance, and then it changes to .sidebar. But I am initializing the layout list configuration with the .sidebar, so not sure why this happens.
Jun ’20