How to initialize stored property which is of type associatedtype protocol - swift

protocol Identifiable {
associatedtype ID
func identifier() -> ID
}
protocol PersonProtocol: Identifiable {
var name: String { get }
var age: Int { get }
}
class Person: PersonProtocol {
let name: String
let age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func identifier() -> String {
return "\(name)_\(age)"
}
}
I tried to a declare & initialise stored property as let owner: PersonProtocol in class Car but it gave an error:
`PersonProtocol' can only be used as a generic constraint because it has Self or associated type requirements
Hence I tried following piece of code to do the same, but I am not sure if this is the correct way to do this.
Need suggestions.
class Car<T: PersonProtocol>{
let owner: T
init<U: PersonProtocol>(owner: U) where U.ID == String {
self.owner = owner as! T // I am force casting `U` as `T`. is this forcecasting justified ?
}
func getID() -> String {
owner.identifier() as! String // is this forcecasting justified ?
}
}

class Car<U,T: PersonProtocol> where T.ID == U{
let owner: T
init(owner: T) {
self.owner = owner
}
func getID() -> U {
owner.identifier()
}
}
let person = Person(name: "John Snow", age: 34)
let car = Car<String, Person>(owner: person)

Related

[Core Data]: data: <fault>

The questions parameter in the function is full, but I cannot transfer assignments to questionsCD.questions?.questionList.
The id and title variables are not nil. Those variables are working in a healthy way.
In Console output, questions return nil.
I cannot assign the questionList variable in the QuestionsNSSecureCoding class, so I think it returns nil. Why ?
You can examine the Core Data Entity image to see the questionsCD.questions?.questionList in more detail.
Core Data Save Function:
func saveSelectedQuestion(questionTitle: String, id: String, questions: [QuestionList]) {
let questionsCD = QuestionCD(context: persistentContainer.viewContext)
questionsCD.title = questionTitle
questionsCD.id = id
questionsCD.questions?.questionList = questions
print("nil test: \(questionsCD.questions?.questionList ?? [])")
do {
try persistentContainer.viewContext.save()
} catch let error {
print("Failed to save selected category: \(error.localizedDescription)")
}
}
Core Data Get Function:
func getSelectedQuestion(questionID: String) -> [QuestionCD] {
let fetchRequest: NSFetchRequest<QuestionCD> = QuestionCD.fetchRequest()
let search = NSPredicate(format: "id == %#", questionID)
print("search: \(search)")
fetchRequest.predicate = search
fetchRequest.returnsObjectsAsFaults = false
print("request predicate: \(String(describing: fetchRequest.predicate))")
do {
return try persistentContainer.viewContext.fetch(fetchRequest)
} catch let error {
print("get hata: \(error.localizedDescription)")
return []
}
}
Console Output:
selectedQuestionCD [<QuestionCD: 0x2800e8e60> (entity: QuestionCD; id: 0x9d00d28ec7359eb0 <x-coredata://089E80AC-0E4F-4303-BF8F-47C31EC70ED4/QuestionCD/p2>; data: {
id = "agustos_test_1";
questions = nil;
title = "A\U011fustos Test 1";
})]
Core Data Entity:
Questions NSSecure Coding:
public class QuestionsNSSecureCoding: NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool = true
var questionList: [QuestionList]
required init(questions: [QuestionList]) {
self.questionList = questions
}
public func encode(with coder: NSCoder) {
coder.encode(questionList, forKey: "questionList")
}
required public init?(coder: NSCoder) {
questionList = coder.decodeObject(of: NSArray.self, forKey: "questionList") as? Array<QuestionList> ?? []
}
}
Questions Value Transformer:
#objc(QuestionsValueTransformer)
final class QuestionsValueTransformer: NSSecureUnarchiveFromDataTransformer {
static let name = NSValueTransformerName(rawValue: String(describing: QuestionsValueTransformer.self))
override static var allowedTopLevelClasses: [AnyClass] {
return [QuestionsNSSecureCoding.self]
}
public static func register() {
let transformer = QuestionsValueTransformer()
ValueTransformer.setValueTransformer(transformer, forName: name)
}
}
My Custom Model:
class QuestionContainer: Codable {
var questionCategories: [Question]
init(questionCategories: [Question]) {
self.questionCategories = questionCategories
}
}
class Question: Codable, Identifiable {
var title: String
var id: String
var questions: [QuestionList]
init(title: String, id: String, questions: [QuestionList]) {
self.title = title
self.id = id
self.questions = questions
}
}
class QuestionList: Codable, Identifiable {
var id: String
init(id: String) {
self.id = id
}
}
I think the problem is that questionCD.questions is nil, so the line
questionsCD.questions?.questionList = questions
does nothing. You need to create an instance of QuestionsNSSecureCoding with the correct questions array, and assign that to the questions property:
questionsCD.questions = QuestionsNSSecureCoding(questions: questions)

swift - UserDefaults wrapper

I want to make userDefaults easier to use. I write this code.
But I don't want to create a allKeys enum(cases voipToken, userId, userName, displayName,...) as the key path for my property.
If I want add a var accessToken to struct LoginInfo, I must add a case accessToken to enum AllKeys. I use CoreStore.key(.accessToken) get a keyPath for CoreStore.LoginInfo.accessToken.
import Foundation
struct CoreStore {
// static let sharedInstance = CoreStore()
// private init(){}
private static let keyPrefix = "_shared_store_"
enum allKeys: String {
case accessToken
}
static func key(_ key: allKeys) -> String {
return keyPrefix + key.rawValue
}
struct LoginInfo: CoreStoreSettable,CoreStoreGettable { }
}
extension CoreStore.LoginInfo {
static var accessToken: String? {
set {
set(value: newValue, forKey: CoreStore.key(.accessToken))
}
get {
return getString(forKey: CoreStore.key(.accessToken))
}
}
// ...
}
protocol CoreStoreSettable {}
extension CoreStoreSettable {
static func set(value: String?, forKey key: String) {
UserDefaults.standard.set(value, forKey: key)
}
}
protocol CoreStoreGettable {}
extension CoreStoreGettable {
static func getString(forKey key: String) -> String? {
return UserDefaults.standard.string(forKey: key)
}
}
Questions:
Is there a way to remove "enum allKeys"?
Should I use sharedInstance? Why?
I tried :
static var accessToken: String? {
set {
let keyPath = \CoreStore.Loginfo.accessToken
let keyPathString = keyPath.string
set(value: newValue, forKey: keyPathString)
}
get {
return getString(forKey: CoreStore.key(.accessToken))
}
}
I get error "Type of expression is ambiguous without more context"

multiple initializers in Swift

Error: play.playground:34:5: error: invalid redeclaration of 'init'
How do I have multiple initializers for a class in Swift? I thought that if I gave different parameters for each init, then each init would have a different method signature, and I could create multiple of them. Why doesn't this work, or have I made a mistake somewhere else? (Below is pulled from playground.)
//make a class
class Human{
var name: String
var age: Int
init(_ name: String){
self.name = name
self.age = -1
}
init(name: String, age: Int){
self.name = name
self.age = age
}
}
var newHuman = Human("bob")
print(newHuman.name)
var newHuman2 = Human(name: "Marmelade", age: 19)
print(newHuman2)
You can pass more parameters with default value like gender: String = ""
init(name: String, age: Int = 0, gender: String = ""){
self.name = name
self.age = age
}
var newHuman = Human("bob")
print(newHuman.name)
var newHuman2 = Human(name: "lol", age: 0)
print(newHuman2)
var newHuman3 = Human(name: "lol", age: 3, gender: "Male")
print(newHuman2)
Have you tried convenience keyword?
//make a class
class Human{
var name: String
var age: Int
convenience init(_ name: String){
self.init(name: name, age: -1)
}
init(name: String, age: Int){
self.name = name
self.age = age
}
}
var newHuman = Human("bob")
print(newHuman.name)
var newHuman2 = Human(name: "Marmelade", age: 19)
print(newHuman2)
class Your_ViewController : UIViewController {
var yourFirstProperty : String?
var yourSecondProperty : String?
init(yourFirstProperty: String) {
self.yourFirstProperty = yourFirstProperty
self.yourSecondProperty = nil
super.init(nibName: nil, bundle: nil)
}
init(yourSecondProperty: String) {
self.yourSecondProperty = yourSecondProperty
self.yourFirstProperty = nil
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) is not supported")
}
}
You can easily have multiple initializers for your view controller here I have written a view controller that can be initialized in two ways and
this is for programmers who make UI with pure code

Swift optional chaining with non-nil value

I have the following program which works fine -
class Person {
var pet: Pet?
}
class Pet {
var name: String
var favoriteToy: Toy?
init(name: String) {
self.name = name
}
}
class Toy {
var name: String
init(name: String) {
self.name = name
}
}
let q = Person()
// Pet(name:"goofy")
// Toy(name:"teddy")
if let someToy = q.pet?.favoriteToy?.name {
print("This person's pet likes \(someToy).")
} else {
print("This person's pet does not have a favorite toy")
}
Prints:
This person's pet does not have a favorite toy
How to use the above code modified to print "This person's pet likes teddy."?
I know I will have not be able to use if let as there will be nothing to unwrap. So I have to write something like this instead of if let:
let someToy = q.pet.favoriteToy.name
print("This person's pet likes \(someToy).")
Should print "This person's pet likes the teddy."
I also know I have to put non-nil value something like this:
class Person {
var pet = Pet ()
}
Still I am having initialization problems. How to go about it?
This should solve your immediate needs...
let man = Person()
let goofy = Pet(name: "goofy")
let squeaky = Toy(name: "squeaky toy")
goofy.favoriteToy = squeaky
man.pet = goofy
But if a person is usually going to be initialized with a pet and a toy, and both of those classes are initialized with a string, then you might want to define a convenience initializer:
class Pet {
var name: String
var favoriteToy: Toy?
init(name: String) {
self.name = name
}
}
class Toy {
var name: String
init(name: String) {
self.name = name
}
}
class Person {
var pet: Pet?
convenience init(petName: String, toyName: String) {
self.init()
let toy = Toy(name: toyName)
let pet = Pet(name: petName)
pet.favoriteToy = toy
self.pet = pet
}
}
func test() {
let bill = Person(petName: "goofy", toyName: "squeaky ball")
guard let toyName = bill.pet?.favoriteToy?.name else { return }
print(toyName)
}
test()
One more thing: optional chaining can be combined with a guard statement to great effect in Swift.
Use ? = nil in your initializer to pass in some optional parameters.
class Person {
var pet: Pet?
init(pet: Pet? = nil) {
self.pet = pet
}
}
class Pet {
var name: String
var favoriteToy: Toy?
init(name: String, toy: Toy? = nil) {
self.name = name
self.favoriteToy = toy
}
}
class Toy {
var name: String
init(name: String) {
self.name = name
}
}
let q = Person(pet: Pet(name: "goofy", toy: Toy(name: "teddy")))
// Pet(name:"goofy")
// Toy(name:"teddy")
if let someToy = q.pet?.favoriteToy?.name {
print("This person's pet likes \(someToy).")
} else {
print("This person's pet does not have a favorite toy")
}

How to use protocols for stucts to emulate classes inheritance

I'm implementing a model:
It has structs ClientSummary and ClientDetails
ClientDetails struct has all properties of ClientSummary struct + some extra properties
Both structs have main initializer init(jsonDictionary: [String: Any])
inits of ClientSummary and ClientDetails share big part of the code
There is an extension which will work with shared functionality of those structs.
The most straightforward solution which came to my mind is just classic inheritance, but it doesn't work for value types.
I'm trying to solve that with protocols, but I can't implement those "shared inits". I was trying to move shared part of the init to the protocol extension but can't really make it. There are various errors.
Here is the test code.
protocol Client {
var name: String { get }
var age: Int { get }
var dateOfBirth: Date { get }
init?(jsonDictionary: [String: Any])
}
struct ClientSummary: Client {
let name: String
let age: Int
let dateOfBirth: Date
init?(jsonDictionary: [String: Any]) {
guard let name = jsonDictionary["name"] as? String else {
return nil
}
self.name = name
age = 1
dateOfBirth = Date()
}
}
struct ClientDetails: Client {
let name: String
let age: Int
let dateOfBirth: Date
let visitHistory: [Date: String]?
init?(jsonDictionary: [String: Any]) {
guard let name = jsonDictionary["name"] as? String else {
return nil
}
self.name = name
age = 1
dateOfBirth = Date()
visitHistory = [Date(): "Test"]
}
}
extension Client {
// A lot of helper methods here
var stringDOB: String {
return formatter.string(from: dateOfBirth)
}
}
Inheritance is the wrong tool here. It doesn't make sense to say "details IS-A summary." Details are not a kind of summary. Step away from the structural question of whether they share a lot of methods, and focus on the essential question of whether one is a kind of the other. (Sometimes renaming things can make that true, but as long as they're "summary" and "detail" it doesn't make sense to inherit.)
What can make sense is to say that details HAS-A summary. Composition, not inheritance. So you wind up with something like:
struct ClientDetails {
let summary: ClientSummary
let visitHistory: [Date: String]?
init?(jsonDictionary: [String: Any]) {
guard let summary = ClientSummary(jsonDictionary: jsonDictionary) else {
return nil
}
self.summary = summary
visitHistory = [Date(): "Test"]
}
// You can add these if you need them, or to conform to Client if that's still useful.
var name: String { return summary.name }
var age: Int { return summary.age }
var dateOfBirth: Date { return summary.dateOfBirth }
}
I often wish that Swift had a built-in way to separate out parts of init methods. However, it can be done, admittedly somewhat awkwardly, with tuples, as below:
struct S {
let foo: String
let bar: Int
let baz: Bool
init() {
(self.foo, self.bar, self.baz) = S.sharedSetup()
}
static func sharedSetup() -> (String, Int, Bool) {
...
}
}
In your case, the sharedSetup() method can be moved to the protocol extension, or wherever it's convenient to have it.
For structs you can use composition instead of relying on inheritance. Let's suppose you already have ClientSummary struct defined with the Client protocol:
protocol Client {
var name: String { get }
var age: Int { get }
var dateOfBirth: Date { get }
init?(jsonDictionary: [String: Any])
}
struct ClientSummary: Client {
let name: String
let age: Int
let dateOfBirth: Date
init?(jsonDictionary: [String: Any]) {
guard let name = jsonDictionary["name"] as? String else {
return nil
}
self.name = name
age = 1
dateOfBirth = Date()
}
}
Now to create ClientDetails sharing ClientSummary logic you can just create a ClientSummary property in ClientDetails. This way have the same initializer as ClientSummary with your additional type specific logic and with use of dynamicMemberLookup you can access ClientSummary properties on ClientDetails type:
#dynamicMemberLookup
struct ClientDetails {
var summary: ClientSummary
let visitHistory: [Date: String]?
init?(jsonDictionary: [String: Any]) {
guard let summary = ClientSummary(jsonDictionary: jsonDictionary) else {
return nil
}
self.summary = summary
visitHistory = [Date(): "Test"]
}
subscript<T>(dynamicMember path: KeyPath<ClientSummary, T>) -> T {
return summary[keyPath: path]
}
subscript<T>(dynamicMember path: WritableKeyPath<ClientSummary, T>) -> T {
get {
return summary[keyPath: path]
}
set {
summary[keyPath: path] = newValue
}
}
subscript<T>(dynamicMember path: ReferenceWritableKeyPath<ClientSummary, T>) -> T {
get {
return summary[keyPath: path]
}
set {
summary[keyPath: path] = newValue
}
}
}
There is an extension which will work with shared functionality of those structs.
Now sharing code between ClientSummary and ClientDetails is tricky. By using dynamicMemberLookup you will be able to access all the properties in ClientSummary from ClientDetails but methods from ClientSummary can't be invoked this way. There is proposal to fulfill protocol requirements with dynamicMemberLookup which should allow you to share methods between ClientSummary and ClientDetails for now you have to invoke ClientSummary methods on ClientDetails using the summary property.