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; }
How can I write this method in Swift ?
To create an initial value. To assign default property value within the property definition. To initialize an instance for a particular data type 'init()' is used. No arguments are passed inside the init() function.
When you mark public , the thing gets available outside of the framework in which your code has been implemented whereas init() {} is a swift initializer that is responsible for ensuring the object is fully initialized. Basically initializers are called to create a new instance of a particular type.
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 } }
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<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()]) }
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.
class myClass { var text: String var response: String? init(text: String) { self.text = text } }
See Swift: Initialization
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With