-2

I have below func in my class.

static func getFirstCharInName(strName: String) -> String {

   let firstCharInName = String(strName.first)

    return firstCharInName.trim()
}

I encountered this err:

Value of optional type 'Character?' must be unwrapped to a value of type 'Character'

What seems to be the problem?

Thanks

MilkBottle
  • 4,242
  • 13
  • 64
  • 146
  • why not just `let first = String(name[name.startIndex...name.startIndex])`...? – and you might need to check whether the string has at least 1 character for doing some failsafe _(see my answer, I just created eventually)_. – holex Feb 18 '19 at 08:24
  • User can find solution from this link : https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language –  Feb 18 '19 at 09:41

5 Answers5

0
func getFirstCharInName(strName: String) -> String {
    let indexStartOfText = strName.index(strName.startIndex, offsetBy: 0)
    let indexEndOfText = strName.index(strName.startIndex, offsetBy: 0)
    let firstChar = String(strName[indexStartOfText...indexEndOfText])
    return firstChar
}
qtngo
  • 1,594
  • 1
  • 11
  • 13
0

This error means that the expression has optional value (the value can be nil) that is not yet unwrapped, strName.first returns an optional value of Character?, but your function demands a returning type of String which is not an optional type.

So, in order to fix this, you need to unwrap the optional value strName.first, it seems like you are not familiar with optionals, here's the code for your case (choose one from two options):

func getFirstCharInName(strName: String) -> String {
    // option 1: force unwrap - can cause fatal error
    return String(strName.first!)

    // option 2: optional binding
    if let firstCharInName = strName.first {
        return String(firstCharInName)
    } else {
        // if the optional value is nil, return an empty string
        return ""
    }
}

PS. I don't really understand the function trim() in your question, but if you mean to strip away the blank spaces like " ", you can do:

firstCharInName.trimmingCharacters(in: .whitespaces)
AgentBilly
  • 756
  • 5
  • 20
0

you can do something like that:

extension String {

    var firstLetter: String {

        guard !self.isEmpty else { return "" }
        return String(self[self.startIndex...self.startIndex])
    }
}

then

let name = "MilkBottle"
let first = name.firstLetter // "M"
holex
  • 23,961
  • 7
  • 62
  • 76
0

Avoid the optional simply with prefix, it's totally safe. if there is no first character you'll get an empty string.

static func getFirstChar(in name: String) -> String { // the function name getFirstChar(in name is swiftier
    return String(name.prefix(1))
}

I don't know what the trim function is supposed to do.

vadian
  • 274,689
  • 30
  • 353
  • 361
0

It means that value of optional type 'Character?' (as result of your part of code strName.first) must be unwrapped to a value of type 'Character' before you will be gonna cast it to String type. You may use this variant:

func getFirstCharInName(strName: String) -> String {
   return strName.count != 0 ? String(strName.first!) : ""
}

As you can see, the exclamation point is in the string strName.first! retrieves the optional variable as it was needed.

NSA-bot
  • 101
  • 3