skip to Main Content

When using the split() the current time for example, "10:47 AM" based on space character, I am getting the result as "10:47 AM" itself. The code is as follows:

let formatter = DateFormatter()
formatter.timeStyle = .short

let scheduleTime = formatter.string(from: scheduleTimePickerDate) // here it is "10:47 AM"

let timeSplitArray_space = scheduleTime.split(separator: " ") // here i am getting "10:47 AM" which means split() didn't work
let timeSplitArray_colon = timeSplitArray_space[0].split(separator: ":")
let lastDigit = timeSplitArray_colon[1].last // here i am getting "M"

The issue happens only in iOS 17. I checked in iOS 16 and it gave correct result when using split().

2

Answers


  1. Chosen as BEST ANSWER

    It worked when using CharacterSet.whitespaces instead of " " for splitting a string based on space character.

    The code i used is:

    let timeSplitArray_space = time.components(separatedBy: CharacterSet.whitespaces)
    

    instead of:

    let timeSplitArray_space = scheduleTime.split(separator: " ")
    

  2. As stated by @Paulw11 and @HangarRash, it is better to use Date Components to get the minute and hours from a date. Otherwise you will be having format issues. It is an issue with Date formatting which exists in iOS 17.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search