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