1

I need to design a String type extension that represents a day of the week and returns the corresponding number starting with Sunday = 1 and ending with Saturday = 7

for example sunday = 1 , monday = 2 , tuesday = 3 , wednesday = 4 ,thurday = 5 , friday = 6, saturday = 7

Here is one idea that i have, but i don't know how to put the Int for the value of the day of the week:

extension String{
   var sunday: String {return self}
   var monday: String {return self}
   var tuesday:String {return self}
   var wednesday: String {return self}
   var thursday: String {return self}
   var friday: String {return self}
   var saturday: String {return self}
}

Another idea is this but i don't know how to return the number or if its value to add the days of the week below to daysW :

extension String{
   var daysW:String{
       return self
   }
   func number() -> Int{
   return self
   }
}
.daysW
.number()

Please can somebody help me , i'm new with Swift and it's confusing

AskBee
  • 17
  • 5
  • 1
    I may be missing why you want an extension. Normally I would think you'd want a dictionary. e.g. https://www.tutorialspoint.com/swift/swift_dictionaries.htm. Allows you to create key value pairs. Also, the built in Calendar/date handling allows you to get days of week by number. https://stackoverflow.com/questions/25533147/get-day-of-week-using-nsdate – Nicholas Rees Feb 25 '21 at 23:51

1 Answers1

4

You better create a enumeration where its rawValue is a string and add a computed property to return a value:

extension Calendar {
    enum Weekday: String, CaseIterable, CustomStringConvertible {
        case sunday, monday, tuesday, wednesday, thursday, friday, saturday
        var description: String { rawValue.capitalized }
        var value: Int { Self.allCases.firstIndex(where: {$0.rawValue == rawValue})! + 1 }
    }
}

let monday = Calendar.Weekday.monday
print(monday)        // Monday
print(monday.value)  // 2

If you need to convert the string to a number you can initialize a new case and return its value:

extension String {
    var weekday: Int? { Calendar.Weekday(rawValue: lowercased())?.value }
}

"monday".weekday  // 2
"Monday".weekday  // 2
"MONDAY".weekday  // 2
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • In the section of {$0.rawValue == rawValue} it gives me an error that says: instance member "rawValue" cannot be used on type "Calendar.Weekday. I do not know if it is due to the fact of not having a physical mac, I use a license to access a remote mac – AskBee Feb 26 '21 at 20:01
  • 1
    @AskBee are you sure you have the exact code as above? – Leo Dabus Feb 26 '21 at 20:04
  • 1
    Note that you can only access the rawValue property on your enumeration cases `Calendar.Weekday.monday.rawValue` // "Monday" or value `Calendar.Weekday.monday.value` // 2 – Leo Dabus Feb 26 '21 at 20:05
  • I see my mistake, sorry it's just that swift suggested me to make changes and I think that affected. An apology – AskBee Feb 26 '21 at 20:08
  • The caseiterable helps us determine how many cases we have and in a way lists them when they do not have a value? Could you explain to me what the CustumStringConvertible and the capitalized does? please – AskBee Feb 26 '21 at 20:27
  • 1
    [CaseIterable protocol](https://developer.apple.com/documentation/swift/caseiterable) means you can convert the values to a collection (array) accessing `allValues` properties `CustumStringConvertible` allows you to print the cases as well do string interpolation with them. Capitalized allows you to display them starting with an Uppercase letter. – Leo Dabus Feb 26 '21 at 20:31
  • And when we define the 'enum', the value is assigned to each case and the 'rawValue' saves the implicit values ​​for each case? – AskBee Feb 26 '21 at 21:38
  • 1
    `rawValue` is the actual value of the enumeration cases. If you define its type as `String` it will (by default) have the same value as its name. You can also define a different value to each case `case sunday, monday = "Monday", tuesday ...` which I didn't (I only changed the way it prints). – Leo Dabus Feb 26 '21 at 21:49
  • 1
    and first of all thank you very much, I really learned a lot with your help. Totally grateful to you – AskBee Feb 26 '21 at 21:49