From ea8be37499f951cc122ad81f89c991d73b95270a Mon Sep 17 00:00:00 2001 From: Hong Xin Date: Sun, 10 Apr 2016 12:23:53 +0800 Subject: [PATCH] optimize initializer --- README.md | 10 +++++----- RRuleSwift/RRule.swift | 2 +- RRuleSwift/RecurrenceRule.swift | 16 ++++++++++------ RRuleSwift/Supporting Files/Info.plist | 2 +- RRuleSwiftExample/Info.plist | 4 ++-- .../RRuleExample.playground/Contents.swift | 4 ++-- .../RRuleExample.playground/timeline.xctimeline | 8 ++++---- .../RRuleExampleViewController.swift | 8 ++++---- 8 files changed, 29 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 5d4c801..162b1f2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ #RRuleSwift -Swift library for working with recurrence rules of calendar dates. +Swift rrule library for working with recurrence rules of calendar dates. ![Example](Gif/RRuleSwiftExample.gif "RRuleSwiftExample") @@ -7,7 +7,7 @@ RRuleSwift is based on [rrule.js](https://github.com/jkbrzt/rrule). ##How To Get Started ###Carthage -Specify "RRuleSwift" in your Cartfile: +Specify "RRuleSwift" in your ```Cartfile```: ```ogdl github "teambition/RRuleSwift" ``` @@ -15,7 +15,7 @@ github "teambition/RRuleSwift" ###Usage ##### Initialization ```swift -var recurrenceRule = RecurrenceRule(recurrenceWithFrequency: .Daily) +var recurrenceRule = RecurrenceRule(frequency: .Daily) recurrenceRule.calendar = ... recurrenceRule.frequency = ... recurrenceRule.interval = ... @@ -36,7 +36,7 @@ recurrenceRule.bysecond = ... ##### Rule form string ```swift let ruleString = "RRULE:FREQ=MONTHLY;DTSTART=20160404T021000Z;COUNT=5;INTERVAL=2;WKST=MO;BYDAY=MO,TU" -let rule = RecurrenceRule.ruleWithString(ruleString) +let rule = RecurrenceRule(recurrenceWithRRuleString: ruleString) ``` ##### String form rule @@ -49,7 +49,7 @@ print(ruleString) ##### Occurrence generator ```swift let ruleString = "RRULE:FREQ=YEARLY;COUNT=5;WKST=MO" -if let rule = RecurrenceRule.ruleWithString(ruleString) { +if let rule = RecurrenceRule(recurrenceWithRRuleString: ruleString) { let allDates = rule.allOccurrences() print(allDates) /* diff --git a/RRuleSwift/RRule.swift b/RRuleSwift/RRule.swift index 90069a3..d7a4447 100644 --- a/RRuleSwift/RRule.swift +++ b/RRuleSwift/RRule.swift @@ -38,7 +38,7 @@ public struct RRule { return rule } - var recurrenceRule = RecurrenceRule(recurrenceWithFrequency: .Daily) + var recurrenceRule = RecurrenceRule(frequency: .Daily) var ruleFrequency: RecurrenceFrequency? for rule in rules { let ruleComponents = rule.componentsSeparatedByString("=") diff --git a/RRuleSwift/RecurrenceRule.swift b/RRuleSwift/RecurrenceRule.swift index e0681b6..11b9e8e 100644 --- a/RRuleSwift/RecurrenceRule.swift +++ b/RRuleSwift/RecurrenceRule.swift @@ -71,15 +71,19 @@ public struct RecurrenceRule { /// The seconds of the minute associated with the recurrence rule, as an array of integers. public var bysecond: [Int]? - public init(recurrenceWithFrequency type: RecurrenceFrequency) { - frequency = type + public init(frequency: RecurrenceFrequency) { + self.frequency = frequency } - public func toRRuleString() -> String { - return RRule.stringFromRule(self) + public init?(recurrenceWithRRuleString rruleString: String) { + if let recurrenceRule = RRule.ruleFromString(rruleString) { + self = recurrenceRule + } else { + return nil + } } - public static func ruleWithString(rruleString: String) -> RecurrenceRule? { - return RRule.ruleFromString(rruleString) + public func toRRuleString() -> String { + return RRule.stringFromRule(self) } } diff --git a/RRuleSwift/Supporting Files/Info.plist b/RRuleSwift/Supporting Files/Info.plist index 5f59404..50325bf 100644 --- a/RRuleSwift/Supporting Files/Info.plist +++ b/RRuleSwift/Supporting Files/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.0.1 + 0.0.2 CFBundleSignature ???? CFBundleVersion diff --git a/RRuleSwiftExample/Info.plist b/RRuleSwiftExample/Info.plist index dbc3551..afd47e9 100644 --- a/RRuleSwiftExample/Info.plist +++ b/RRuleSwiftExample/Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.0.1 + 0.0.2 CFBundleSignature ???? CFBundleVersion - 1 + 2 LSRequiresIPhoneOS UILaunchStoryboardName diff --git a/RRuleSwiftExample/RRuleExample.playground/Contents.swift b/RRuleSwiftExample/RRuleExample.playground/Contents.swift index ee1e09e..e6e4b80 100644 --- a/RRuleSwiftExample/RRuleExample.playground/Contents.swift +++ b/RRuleSwiftExample/RRuleExample.playground/Contents.swift @@ -13,14 +13,14 @@ let dateFormatter: NSDateFormatter = { }() let ruleString1 = "RRULE:FREQ=WEEKLY;DTSTART=20160328T070000Z;BYDAY=MO,TU,WE,TH,FR;INTERVAL=1" -if let rule1 = RecurrenceRule.ruleWithString(ruleString1) { +if let rule1 = RecurrenceRule(recurrenceWithRRuleString: ruleString1) { let weekdays = rule1.byweekday!.map({ (weekday) -> Int in return weekday.rawValue }) } let ruleString2 = "RRULE:FREQ=YEARLY;COUNT=5;WKST=MO" -if let rule2 = RecurrenceRule.ruleWithString(ruleString2) { +if let rule2 = RecurrenceRule(recurrenceWithRRuleString: ruleString2) { let allDates = rule2.allOccurrences().map({ (date) -> String in return dateFormatter.stringFromDate(date) }) diff --git a/RRuleSwiftExample/RRuleExample.playground/timeline.xctimeline b/RRuleSwiftExample/RRuleExample.playground/timeline.xctimeline index 04caad0..6835d9c 100644 --- a/RRuleSwiftExample/RRuleExample.playground/timeline.xctimeline +++ b/RRuleSwiftExample/RRuleExample.playground/timeline.xctimeline @@ -3,22 +3,22 @@ version = "3.0"> diff --git a/RRuleSwiftExample/RRuleExampleViewController.swift b/RRuleSwiftExample/RRuleExampleViewController.swift index fc8e9a6..13b6143 100644 --- a/RRuleSwiftExample/RRuleExampleViewController.swift +++ b/RRuleSwiftExample/RRuleExampleViewController.swift @@ -50,7 +50,7 @@ class RRuleExampleViewController: UIViewController { @IBOutlet weak var textView: UITextView! @IBOutlet weak var tableView: UITableView! - private var rule = RecurrenceRule(recurrenceWithFrequency: .Daily) { + private var rule = RecurrenceRule(frequency: .Daily) { didSet { textView.text = rule.toRRuleString() tableView.reloadData() @@ -67,13 +67,13 @@ class RRuleExampleViewController: UIViewController { navigationItem.title = "RRuleSwift Example" tableView.tableFooterView = UIView() tableView.separatorStyle = .None - rule = RecurrenceRule(recurrenceWithFrequency: .Daily) + rule = RecurrenceRule(frequency: .Daily) textView.delegate = self navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Reset", style: .Plain, target: self, action: #selector(resetButtonTapped(_:))) } func resetButtonTapped(sender: UIBarButtonItem) { - rule = RecurrenceRule(recurrenceWithFrequency: .Daily) + rule = RecurrenceRule(frequency: .Daily) } } @@ -370,7 +370,7 @@ extension RRuleExampleViewController: UITextViewDelegate { if text == "\n" { textView.resignFirstResponder() - rule = RecurrenceRule.ruleWithString(textView.text) ?? RecurrenceRule(recurrenceWithFrequency: .Daily) + rule = RecurrenceRule(recurrenceWithRRuleString: textView.text) ?? RecurrenceRule(frequency: .Daily) return false } return true