Post

Replies

Boosts

Views

Activity

Reply to [MyClass initWithCoder:]: unrecognized selector sent to instance - On iOS16
The correct pattern for calling initWithCoder as an instance method and not a class method, which doesn't exist provided you didn't create a class version of the method, is [[MyClass alloc] initWithCoder: aCoder]. And what the error above is telling you is the variable or property result points to nothing. Always perform a NULL check on the result before passing it to unarchiveObjectWithData:.
Topic: App & System Services SubTopic: General Tags:
Nov ’22
Reply to Swift Protocol Constraint Required - Advanced
Here is my version minus the manual abstraction allowing the any keyword to do what it was meant to do. We define a protocol for the data model, City, allowing it to participate in the compiler's existential abstraction. Runs without issue. import Cocoa public protocol ManagedFilter: AnyObject {     typealias Condition = ((any DataModel) -> Bool)     var name: String { get }     var condition: Condition? { get }     func extractValues(from array: [any DataModel]) } public protocol FilterSpecification: ManagedFilter {     associatedtype ValueType: Comparable     var values: [ValueType] { get set } } public protocol DataModel {     var code: String { get }     var country: String { get }     var region: String { get } } public class FiltersManager {     private var originalArray: [any DataModel]     private var filteredArray: [any DataModel]     private(set) var filters: [any FilterSpecification]     public init(array: [any DataModel], filters: [any FilterSpecification]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters         self.filters.forEach {             $0.extractValues(from: array)         }     } } struct City1: DataModel {     let code: String     let country: String     let region: String } struct City2: DataModel {     let code: String     let country: String     let region: String } class CountryFilter1: FilterSpecification {     var name = "Country"     var values = [String]()     var condition: ((any DataModel) -> Bool)? {         guard !values.isEmpty else { return nil }         return { city in             self.values.contains(city.country)         }     }     func extractValues(from array: [any DataModel]) {         // remove duplicates     } } class RegionFilter1: FilterSpecification {     var name = "Region"     var values = [String]()     var condition: ((any DataModel) -> Bool)? {         guard !values.isEmpty else { return nil }         return { city in             self.values.contains(city.region)         }     }     func extractValues(from array: [any DataModel]) {         // remove duplicates     } } let cities = [City1(code: "001", country: "US", region: "EST"), City2(code: "002", country: "CA", region: "MST")] let manager = FiltersManager(array: cities, filters: [CountryFilter1(), RegionFilter1()])
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’22
Reply to Swift Protocol Constraint Required - Advanced
A much simpler approach: protocol ProtocolThing {     associatedtype Item     var values: Item { get set }     func things() -> Item } struct Thing1: ProtocolThing {     typealias Item = String     var values: Item     func things() -> Item {         values     } } struct Thing2: ProtocolThing {     typealias Item = String     var values: Item     func things() -> Item {         values     } } struct Doctor {     var things: [any ProtocolThing]     func thingAMeJig() -> String {         let thing = things[Int.random(in: 0..<things.count)]         return "\(thing.values) - \(thing.things())"     } } let exam = Doctor(things: [Thing1(values: "Hat"), Thing2(values: "Cat")]) print(exam.thingAMeJig())
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’22
Reply to Swift Protocol Constraint Required - Advanced
This is what I had to do in the end. Each filter had to be cast to its concrete type to call or access any property or method. protocol SimpleFilter {     associatedtype ArrayType: Collection     associatedtype ValueType: Comparable     var values: [ValueType] { get set }     func extractValues(from array: ArrayType) } extension SimpleFilter {     func extractValues(from array: [City]) {         print(array.count, values.count)     } } struct City {     let code: String     let country: String     let region: String } struct CountryFilter: SimpleFilter {     var values: [String]          typealias ArrayType = [City]     typealias ValueType = String } struct RegionFilter: SimpleFilter {     var values: [String]          typealias ArrayType = [City]     typealias ValueType = String } typealias anySimpleFilter = any SimpleFilter class FiltersManager<T: Collection> {          private var originalArray: T     private var filteredArray: T     private(set) var filters: [anySimpleFilter]          public init(array: T, filters: [anySimpleFilter]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters                  for filter in filters {             if let filter = filter as? CountryFilter {                 filter.extractValues(from: array as! [City])             } else if let filter = filter as? RegionFilter {                 filter.extractValues(from: array as! [City])             }         }              } } let filters = [RegionFilter(values: []), CountryFilter(values: [])] as [any SimpleFilter] let cities = [City(code: "0", country: "CA", region: "NA")] let manager = FiltersManager(array: cities, filters: filters)
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’22
Reply to Swift Protocol Constraint Required - Advanced
Or if you really need to use any then just apply it to the filter property. class FiltersManager<T: Collection, U: SimpleFilter> where T == U.ArrayType {     private var originalArray: T     private var filteredArray: T     private(set) var filters: [any SimpleFilter]     public init(array: T, filters: [U]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters         for filter in filters {             filter.extractValues(from: array)         }     } }
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’22
Reply to Swift Protocol Constraint Required - Advanced
Don't use any as you have no use case for it. class FiltersManager<T: Collection, U: SimpleFilter> where T == U.ArrayType {     private var originalArray: T     private var filteredArray: T     private(set) var filters: [U]     public init(array: T, filters: [U]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters         for filter in filters {             filter.extractValues(from: array)         }     } }
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’22
Reply to SecItemCopyMatching (crash)
if (keyData != NULL) { ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData]; }
Topic: Privacy & Security SubTopic: General Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to Spotlight Search Function Ventura 13.0.1
Welcome to the Apple Developer ForumsPost your questions, exchange knowledge, and connect with fellow developers and Apple engineers on a variety of software development topics. For questions about using Apple hardware and services, visit Apple Support Communities
Topic: App & System Services SubTopic: Hardware Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to Running under Parallels crashes XCode apps
Well, debug your app first.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to Autoplay working in Chrome but not in Safari. WHY!?!?!
Autoplay is a user-configurable setting via the Safari address bar for privacy reasons. You don't have programmatic access to this setting.
Topic: Safari & Web SubTopic: General Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to 2019 Sample Code
Looks like they're only keeping three years' worth since this https://developer.apple.com/sample-code/wwdc/2019/ doesn't work.
Topic: Machine Learning & AI SubTopic: General Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to macOS 10.12.4 NSButton After adding CAGradientLayer, button title is not displayed
Maybe because you're hiding the label by placing the new layer at index 0. Find the Label layer index and insert the gradient beneath it.
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to [MyClass initWithCoder:]: unrecognized selector sent to instance - On iOS16
The correct pattern for calling initWithCoder as an instance method and not a class method, which doesn't exist provided you didn't create a class version of the method, is [[MyClass alloc] initWithCoder: aCoder]. And what the error above is telling you is the variable or property result points to nothing. Always perform a NULL check on the result before passing it to unarchiveObjectWithData:.
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to Xcode 14.0.1, Build time Slow as Hell!
Or it is just your project and its dependencies.
Replies
Boosts
Views
Activity
Nov ’22
Reply to Swift Protocol Constraint Required - Advanced
Here is my version minus the manual abstraction allowing the any keyword to do what it was meant to do. We define a protocol for the data model, City, allowing it to participate in the compiler's existential abstraction. Runs without issue. import Cocoa public protocol ManagedFilter: AnyObject {     typealias Condition = ((any DataModel) -> Bool)     var name: String { get }     var condition: Condition? { get }     func extractValues(from array: [any DataModel]) } public protocol FilterSpecification: ManagedFilter {     associatedtype ValueType: Comparable     var values: [ValueType] { get set } } public protocol DataModel {     var code: String { get }     var country: String { get }     var region: String { get } } public class FiltersManager {     private var originalArray: [any DataModel]     private var filteredArray: [any DataModel]     private(set) var filters: [any FilterSpecification]     public init(array: [any DataModel], filters: [any FilterSpecification]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters         self.filters.forEach {             $0.extractValues(from: array)         }     } } struct City1: DataModel {     let code: String     let country: String     let region: String } struct City2: DataModel {     let code: String     let country: String     let region: String } class CountryFilter1: FilterSpecification {     var name = "Country"     var values = [String]()     var condition: ((any DataModel) -> Bool)? {         guard !values.isEmpty else { return nil }         return { city in             self.values.contains(city.country)         }     }     func extractValues(from array: [any DataModel]) {         // remove duplicates     } } class RegionFilter1: FilterSpecification {     var name = "Region"     var values = [String]()     var condition: ((any DataModel) -> Bool)? {         guard !values.isEmpty else { return nil }         return { city in             self.values.contains(city.region)         }     }     func extractValues(from array: [any DataModel]) {         // remove duplicates     } } let cities = [City1(code: "001", country: "US", region: "EST"), City2(code: "002", country: "CA", region: "MST")] let manager = FiltersManager(array: cities, filters: [CountryFilter1(), RegionFilter1()])
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to Unboxing protocol to concrete type in SwiftUI View
struct RowView: View {     var item: any ItemProtocol     init(item: any ItemProtocol) {         self.item = item     }     var body: some View {         Text("Row View")     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to New App Store Review Guideline - 3.1.1 In-App Purchase
My suggestion would be to consult an attorney and see what your options are if affected.
Topic: App & System Services SubTopic: StoreKit Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to Swift Protocol Constraint Required - Advanced
A much simpler approach: protocol ProtocolThing {     associatedtype Item     var values: Item { get set }     func things() -> Item } struct Thing1: ProtocolThing {     typealias Item = String     var values: Item     func things() -> Item {         values     } } struct Thing2: ProtocolThing {     typealias Item = String     var values: Item     func things() -> Item {         values     } } struct Doctor {     var things: [any ProtocolThing]     func thingAMeJig() -> String {         let thing = things[Int.random(in: 0..<things.count)]         return "\(thing.values) - \(thing.things())"     } } let exam = Doctor(things: [Thing1(values: "Hat"), Thing2(values: "Cat")]) print(exam.thingAMeJig())
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’22
Reply to Swift Protocol Constraint Required - Advanced
This is what I had to do in the end. Each filter had to be cast to its concrete type to call or access any property or method. protocol SimpleFilter {     associatedtype ArrayType: Collection     associatedtype ValueType: Comparable     var values: [ValueType] { get set }     func extractValues(from array: ArrayType) } extension SimpleFilter {     func extractValues(from array: [City]) {         print(array.count, values.count)     } } struct City {     let code: String     let country: String     let region: String } struct CountryFilter: SimpleFilter {     var values: [String]          typealias ArrayType = [City]     typealias ValueType = String } struct RegionFilter: SimpleFilter {     var values: [String]          typealias ArrayType = [City]     typealias ValueType = String } typealias anySimpleFilter = any SimpleFilter class FiltersManager<T: Collection> {          private var originalArray: T     private var filteredArray: T     private(set) var filters: [anySimpleFilter]          public init(array: T, filters: [anySimpleFilter]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters                  for filter in filters {             if let filter = filter as? CountryFilter {                 filter.extractValues(from: array as! [City])             } else if let filter = filter as? RegionFilter {                 filter.extractValues(from: array as! [City])             }         }              } } let filters = [RegionFilter(values: []), CountryFilter(values: [])] as [any SimpleFilter] let cities = [City(code: "0", country: "CA", region: "NA")] let manager = FiltersManager(array: cities, filters: filters)
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’22
Reply to Swift Protocol Constraint Required - Advanced
Or if you really need to use any then just apply it to the filter property. class FiltersManager<T: Collection, U: SimpleFilter> where T == U.ArrayType {     private var originalArray: T     private var filteredArray: T     private(set) var filters: [any SimpleFilter]     public init(array: T, filters: [U]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters         for filter in filters {             filter.extractValues(from: array)         }     } }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’22
Reply to Swift Protocol Constraint Required - Advanced
Don't use any as you have no use case for it. class FiltersManager<T: Collection, U: SimpleFilter> where T == U.ArrayType {     private var originalArray: T     private var filteredArray: T     private(set) var filters: [U]     public init(array: T, filters: [U]) {         self.originalArray = array         self.filteredArray = array         self.filters = filters         for filter in filters {             filter.extractValues(from: array)         }     } }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’22