如何在Swift中编写init方法? [英] How to write init method in Swift?

查看:92
本文介绍了如何在Swift中编写init方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Swift中编写一个init方法.在这里,我在Objective-C中初始化一个NSObject类:

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

-(id)initWithNewsDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self) {
        self.title           = dictionary[@"title"];
        self.shortDescription = dictionary[@"description"];
        self.newsDescription = dictionary[@"content:encoded"];
        self.link            = dictionary[@"link"];
        self.pubDate         = [self getDate:dictionary[@"pubDate"]];

    }
    return self;
}

如何在Swift中编写此方法?

How can I write this method in Swift ?

推荐答案

我想这可能是您课堂上的好基础.

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<String, AnyObject>) {
        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
    }

}

高级模式

我想避免将键复制粘贴到项目中,因此我将可能的键放入例如像这样的enum:

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 ""
        }
    }
}

,您可以改进convenience init(...)方法,例如这样,将来您就可以避免对代码中的键进行任何可能的迷惑:

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<String, AnyObject>) {
    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()])

}

注意:那只是如何做的一个原始想法,根本不需要使用便捷初始化器,但是关于我对您的期末班一无所知,这看起来是显而易见的选择.只能共享一种方法.

这篇关于如何在Swift中编写init方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆