in Education by
I'm having trouble with understanding how to properly do unwrapping in Swift. Here is the situation: I have these two classes: class Alpha { var val: Int init(x: Int) { val = x } func doSomething() -> Int { // does stuff and returns an Int value return val } } and a second class which is a subclass of the first class Beta: Alpha { var str: [String] = [] override init(x: Int) { super.init(x) } func doSomething() -> String { // does stuff and returns a String return str } } So far so good, these two classes work as expected. But now I want to create a dictionary to store several of both these class objects. So I've created a class to manage the dictionary and add items to it. class Gamma { var myDict: [String:AnyObject] = [:] func addAlphaToDict(key: String, val: Int) { self.myDict[key] = Alpha(x: val).self } func addBetaToDict(key: String, val: Int) { self.myDict[key] = Beta(x: val).self } } This also is working, but I think this could somehow be improved. The issue I'm having is now when I try to access values in the dictionary: var foo = Gamma() foo.addAlphaToDict(key: "blah", val: 45) foo.addBetaToDict(key: "bluh", val: 23) var result1 = (foo.myDict["blah"] as! Alpha).doSomething() var result2 = (foo.myDict["bluh"] as! Beta).doSomething() I find that the syntax here is very clunky and I feel like I'm doing it wrong, even though it works. I'm an experienced developer, but have only just started using Swift so I'm not really sure about certain things. Could someone with more Swift experience show me how to improve this code, or point me in the direction I should be going? Thanks! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use Alpha instead of AnyObject as your dictionary value in this case. Just downcast it to Beta when needed. Using AnyObject or Any as the dictionary key should be avoided as much as possible. However, i have to say that this approach is bad. You need to have a clearer logic to decide when the key will be Beta other than just relaying on knowing which key you are passing to the dictionary.

Related questions

0 votes
    What is a dictionary in Swift ?...
asked Nov 30, 2020 in Technology by JackTerrance
0 votes
    I'm relatively new to Swift and Firebase, so I'm not very familiar with the intricacies of how ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    How do I see which version of Swift I'm using?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    What is a GUARD statement? What is the benefit of using the GUARD statement in swift?...
asked Nov 30, 2020 in Technology by JackTerrance
0 votes
    What are the advantages of using Swift?...
asked Nov 30, 2020 in Technology by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    Need to read my SMTP email settings defined under system.net section in my web.config file. Below is one example of SMTP email setting defined in web.config file: (Under Section)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    If you had enabled a Conditional Access Policy for External Users using multi-factor authentication, and then also ... users, would they trigger both when accessing content?...
asked Mar 10, 2021 in Technology by JackTerrance
0 votes
    The data are kept in a list whose elements are dictionaries, where each dictionary contains the data for a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    What type of authentication attackers can detect via manual means and exploit them, using automated tools with password lists and dictionary attacks?...
asked Mar 20, 2021 in Technology by JackTerrance
0 votes
    How will you create a dictionary using tuples in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    I've custom UICollectionViewCell that contains UILabel and UIImage. I want to set the cell in circular ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
...