How to write init method in Swift?

前端 未结 4 1040
死守一世寂寞
死守一世寂寞 2020-12-05 02:37

I want to write an init method in Swift. Here I initialize an NSObject class in Objective-C:

-(id)initWithNewsDictionary:(NSDiction         


        
4条回答
  •  无人及你
    2020-12-05 02:51

    that could be good bases for your class, I guess:

    class MyClass {
    
        // you may need to set the proper types in accordance with your dictionarty's content
        var title: String?
        var shortDescription: String?
        var newsDescription: String?
        var link: NSURL?
        var pubDate: NSDate?
    
        //
    
        init () {
            // uncomment this line if your class has been inherited from any other class
            //super.init()
        }
    
        //
    
        convenience init(_ dictionary: Dictionary) {
            self.init()
    
            title = dictionary["title"] as? NSString
            shortDescription = dictionary["shortDescription"] as? NSString
            newsDescription = dictionary["newsDescription"] as? NSString
            link = dictionary["link"] as? NSURL
            pubDate = self.getDate(dictionary["pubDate"])
    
        }
    
        //
    
        func getDate(object: AnyObject?) -> NSDate? {
            // parse the object as a date here and replace the next line for your wish...
            return object as? NSDate
        }
    
    }
    

    advanced-mode

    I would like to avoid to copy-pand-paste the keys in a project, so I'd put the possible keys into e.g. an enum like this:

    enum MyKeys : Int {
        case KeyTitle, KeyShortDescription, KeyNewsDescription, KeyLink, KeyPubDate
        func toKey() -> String! {
            switch self {
            case .KeyLink:
                return "title"
            case .KeyNewsDescription:
                return "newsDescription"
            case .KeyPubDate:
                return "pubDate"
            case .KeyShortDescription:
                return "shortDescription"
            case .KeyTitle:
                return "title"
            default:
                return ""
            }
        }
    }
    

    and you can improve your convenience init(...) method like e.g. this, and in the future you can avoid any possible mistyping of the keys in your code:

    convenience init(_ dictionary: Dictionary) {
        self.init()
    
        title = dictionary[MyKeys.KeyTitle.toKey()] as? NSString
        shortDescription = dictionary[MyKeys.KeyShortDescription.toKey()] as? NSString
        newsDescription = dictionary[MyKeys.KeyNewsDescription.toKey()] as? NSString
        link = dictionary[MyKeys.KeyLink.toKey()] as? NSURL
        pubDate = self.getDate(dictionary[MyKeys.KeyPubDate.toKey()])
    
    }
    

    NOTE: that is just a raw idea of how you could do it, it is not necessary to use conveniece initializer at all, but it looked obvious choice regarding I don't know anything about your final class – you have shared one method only.

提交回复
热议问题