HomeiOS Developmentios - Easy methods to convert array to String in Swift?

ios – Easy methods to convert [Character] array to String in Swift?


I am making an attempt to transform an array of Characters ([Character]) to a String in Swift. My operate must reverse a String; it takes a String, breaks it down right into a [Character], reverses it, then turns it right into a String once more. I’ve discovered and tried 3 methods of doing this, however none appear to work for me. Please see code beneath:

reverseString(String: "Reverse this String")


func reverseString(String: String) -> String {

    var reversedArray: [Character] = []
    var finalArray: [Character] = []

    for character in String {

        reversedArray.append(character)
    }

    for character in reversedArray.reversed() {

        finalArray.append(character)

    }
    
// 1.    var str = finalArray.compactMap { $0 as? String }.joined()

// 2. var nextLevelString = String(finalArray)

// 3.  var finalFinalArray = finalArray.joined()

    return //the ultimate reversed string
}

Sadly, every possibility simply provides me the error: “Can not name worth of non-function sort ‘String'”.
I really feel like I am lacking one thing, as all these strains ought to work in idea. TIA for recommendations.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments