Open ericcgu opened 9 years ago
@ericcgu It would be great if you can help with the swift doc and examples migration. I have been working on swift examples and almost got all the examples migrated to swift.
Since you want to help I just pushed it here: https://github.com/xmartlabs/XLForm/tree/examples/swift . Could you review these swift code and make changes if needed ? This is my first swift code. :) I'm open to any contribution such as improve documentation, feature implementation, bug fixes, answer questions, add more test cases, etc.
@mtnbarreto It looks good. I don't know this repo well enough to add a lot of code change but here are some of the things I noticed:
Int
instead of static lets
. I believe you can write custom functions on enums. They are much more powerful:class XLFormWeekDaysCell : XLFormBaseCell {
static let kSunday = "sunday"
static let kMonday = "monday"
static let kTuesday = "tuesday"
static let kWednesday = "wednesday"
static let kThursday = "thursday"
static let kFriday = "friday"
static let kSaturday = "saturday"
enum kWeekDay: Int {
case
Sunday = 1,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
func description() -> String {
switch self {
case .Sunday:
return "Sunday"
case .Monday:
return "Monday"
case .Tuesday:
return "Tuesday"
case .Wednesday:
return "Wednesday"
case .Thursday:
return "Thursday"
case .Friday:
return "Friday"
case .Saturday:
return "Saturday"
}
}
//Add Custom Function to Enum
//Allows you loop and iterate as needed
static let allValues = [Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,]
}
@IBOutlet weak var sundayButton: UIButton!
@IBOutlet weak var mondayButton: UIButton!
@IBOutlet weak var tuesdayButton: UIButton!
@IBOutlet weak var wednesdayButton: UIButton!
@IBOutlet weak var thursdayButton: UIButton!
@IBOutlet weak var fridayButton: UIButton!
@IBOutlet weak var saturdayButton: UIButton!
//Consider changing tags and using Array of UIButton
@IBOutlet var weekdayButton = [UIButton]()
func updateButtons() {
let value = self.rowDescriptor.value as! Dictionary<String, Bool>
sundayButton.selected = value[XLFormWeekDaysCell.kSunday]!
self.mondayButton.selected = value[XLFormWeekDaysCell.kMonday]!
self.tuesdayButton.selected = value[XLFormWeekDaysCell.kTuesday]!
self.wednesdayButton.selected = value[XLFormWeekDaysCell.kWednesday]!
self.thursdayButton.selected = value[XLFormWeekDaysCell.kThursday]!
self.fridayButton.selected = value[XLFormWeekDaysCell.kFriday]!
self.saturdayButton.selected = value[XLFormWeekDaysCell.kSaturday]!
}
func updateButtons() {
let value = rowDescriptor.value as! Dictionary<String, Bool>
sundayButton.selected = value[XLFormWeekDaysCell.kSunday]!
mondayButton.selected = value[XLFormWeekDaysCell.kMonday]!
tuesdayButton.selected = value[XLFormWeekDaysCell.kTuesday]!
wednesdayButton.selected = value[XLFormWeekDaysCell.kWednesday]!
thursdayButton.selected = value[XLFormWeekDaysCell.kThursday]!
fridayButton.selected = value[XLFormWeekDaysCell.kFriday]!
saturdayButton.selected = value[XLFormWeekDaysCell.kSaturday]!
}
struct OrderedDictionary<Tk: Hashable, Tv> {
var keys: Array<Tk>
var values: Dictionary<Tk,Tv>
// 1
mutating func insert(value: Tv, forKey key: Tk, atIndex index: Int) -> Tv?
{
var adjustedIndex = index
// 2
let existingValue = self.values[key]
if existingValue != nil {
// 3
let existingIndex = find(self.keys, key)!
// 4
if existingIndex < index {
adjustedIndex--
}
self.keys.removeAtIndex(existingIndex)
}
// 5
self.keys.insert(key, atIndex:adjustedIndex)
self.values[key] = value
// 6
return existingValue
}
}
@ericcgu, thanks for your contributions!
I agree that functions inside an enum is a better approach (swift has this capability, let's use it). Can you update the code? Regarding the collection IBOutlet I think it can reduce the amount of code but at the same time is hard to understand. I don't have a strong position. I let you make the decision.
Notice that the swift and obj-c examples are the same, I tried to have the same examples (on both languages) so anyone can compare and i would like to continue with this correspondence.
My pleasure.
I also changed this Switch Looking If Statement to a Switch Statement. Hope that is OK.
func getDayFormButton(button: UIButton) -> String
{
if button == self.sundayButton { return XLFormWeekDaysCell.kSunday }
if button == self.mondayButton { return XLFormWeekDaysCell.kMonday }
if button == self.tuesdayButton { return XLFormWeekDaysCell.kTuesday }
if button == self.wednesdayButton { return XLFormWeekDaysCell.kWednesday }
if button == self.thursdayButton { return XLFormWeekDaysCell.kThursday }
if button == self.fridayButton { return XLFormWeekDaysCell.kFriday }
return XLFormWeekDaysCell.kSaturday
}
func getDayFormButton(button: UIButton) -> String {
switch button {
case sundayButton:
return XLFormWeekDaysCell.kWeekDay.Sunday.description()
case mondayButton:
return XLFormWeekDaysCell.kWeekDay.Monday.description()
case tuesdayButton:
return XLFormWeekDaysCell.kWeekDay.Tuesday.description()
case wednesdayButton:
return XLFormWeekDaysCell.kWeekDay.Wednesday.description()
case thursdayButton:
return XLFormWeekDaysCell.kWeekDay.Thursday.description()
case fridayButton:
return XLFormWeekDaysCell.kWeekDay.Friday.description()
default:
return XLFormWeekDaysCell.kWeekDay.Saturday.description()
}
}
https://github.com/xmartlabs/XLForm/pull/362
I will try to help out as I study XLForms more.
@mtnbarreto
Do you want me to change this for UICustomization?
Let me know.
@ericcgu Go ahead! Please update objective-c example as well.
@mtnbarreto
If you think this is OK, I will work on converting Obj C right away.
Let me know if want help with this. @mtnbarreto