如何在Swift中编写Init方法

Vin*_* TP 29 objective-c init swift

我想写init方法swift,这里我给了NSObject模型类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;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能写这个方法swift

hol*_*lex 65

我猜这可能是你班级的良好基础:

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
    }

}
Run Code Online (Sandbox Code Playgroud)

高级模式

我想避免将密钥复制粘贴到项目中,因此我将可能的密钥放入例如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 ""
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并且您可以convenience init(...)像这样改进您的方法,并且将来您可以避免代码中任何可能的密钥错误:

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()])

}
Run Code Online (Sandbox Code Playgroud)

注意:这只是你如何做到这一点的原始想法,根本没有必要使用通用的初始化器,但它看起来很明显,我对你的最终课程一无所知 - 你只分享了一种方法.


Sha*_*hai 10

class myClass {
    var text: String
    var response: String?

    init(text: String) {
        self.text = text
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅Swift:初始化(下次最好自己谷歌...)


abd*_*har 6

不需要从其他类调用此方法,它将自动调用

override init()
    {
        super.init()
         //synthesize.delegate = self
       // println("my array elements are \(readingData)")

    }
Run Code Online (Sandbox Code Playgroud)