0

Dears

I have this case where chatId is a property of type Int

let StringMessage = String(self.listingChat?.messages.last?.chatId)

When I debug I find that StringMessage is returning Optional(15) Which means it is unwrapped. But at the same time XCode does not allow me to put any bangs (!) to unwrap it. So I am stuck with Unwrapped Variable. I know its noob question but it I really cant get it. Your help is appreciated.

Thank you

2
  • Add nil coalescing operator ?? 0 Or use if let or guard Commented Apr 12, 2016 at 1:28
  • 1
    Are you sure Xcode doesn't let you use bangs to unwrap? I find that odd. Not that you should use them (use guard-let-else instead), but it is strange that you don't have the option... Commented Apr 12, 2016 at 2:42

3 Answers 3

1

It depends on what you want the default value to be.

Assuming you want the default value to be an empty string (""), You could create a function or a method to handle it.

func stringFromChatId(chatId: Int?) -> String {
    if let chatId = chatId {
        return String(chatId)
    } else {
        return ""
    }
}

let stringMessage = stringFromChatId(self.listingChat?.messages.last?.chatId)

Or you could handle it with a closure.

let stringMessage = { $0 != nil ? String($0!) : "" }(self.listingChat?.messages.last?.chatId)

If you don't mind crashing if self.listingChat?.messages.last?.chatId is nil, then you should be able to directly unwrap it.

let StringMessage = String((self.listingChat?.messages.last?.chatId)!)

or with a closure

let stringMessage = { String($0!) }(self.listingChat?.messages.last?.chatId)

Update

Assuming chatId is an Int and not an Optional<Int> (AKA Int?) I missed the most obvious unwrap answer. Sorry, I was tired last night.

let StringMessage = String(self.listingChat!.messages.last!.chatId)

Force unwrap all the optionals along the way.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. It worked. But can you please explain to me why self.listingChat?.messages.last?.chatId returns unwrapped Value " But in Int" But when I call String((self.listingChat?.messages.last?.chatId)!) I must unwrap it Before Converting it to String. I can get it if the unwrapping is after conversion. But what really can't get is why is it before conversion
@MostafaMohamedRaafat self.listingChat?.messages.last?.chatId returns an Optional<Int>. It is not unwrapped.
@MostafaMohamedRaafat I added a new answer.
Thank you for your support :) People like you make Stackoverflow great. Thanks again
1

Optionals have a very nice method called map (unrelated to map for Arrays) which returns nil if the variable is nil, otherwise it calls a function on the (non-nil) value. Combined with a guard-let, you get very concise code. (I've changed the case of stringMessage because variables should begin with a lower-case letter.)

guard let stringMessage = self.listingChat?.messages.last?.chatId.map { String($0) } else {
    // Do failure
}

// Success. stringMessage is of type String, not String?

Comments

0

I think:

let StringMessage = String(self.listingChat?.messages.last?.chatId)!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.