Cannot Get Rid of Optional() String

Cannot get rid of Optional() string

If you unwrap the latitude and longitude values like this...

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if let lat = manager.location?.coordinate.latitude,
let long = manager.location?.coordinate.longitude {

updateloc(String(lat), long: String(long))
}
}

...then you can avoid optionals altogether in the updateloc function:

func updateloc(lat : String, long : String) {

// code...

let data = "lat=\(lat)&long=\(long)"
}

originalUser2 is right...you should get in the habit of safely unwrapping optionals unless you have a really good reason for force-unwrapping them. And just trying to get rid of the optional so the code will compile is not a good reason.

How to get rid of Optional(...) in my output when transferring data to another ViewController

More than a solution you need to get the basic idea about the optional in Swift.

Swift Optional

You can go for

  1. Forced Unwrapping: Do this if you are very sure about the content. Otherwise App will crash

    firstNameLabel.text = userFirstName!
  2. Optional Binding: This is more secure way

    if let firstName = userFirstName {
    firstNameLabel.text = firstName
    }

Unable to remove Optional from String

Your line

(String(location?.latitude), String(location?.longitude))

is the culprit.

When you call String() it makes a String of the content, but here your content is an Optional, so your String is "Optional(...)" (because the Optional type conforms to StringLiteralConvertible, Optional(value) becomes "Optional(value)").

You can't remove it later, because it's now text representing an Optional, not an Optional String.

The solution is to fully unwrap location?.latitude and location?.longitude first.

Can't remove optional(String). states its non-optional

The problem is already where you add data to the array.
Assuming that transDescrInput.text is an optional string,

String(describing: transDescrInput.text)

returns a non-optional string "Optional(text...)" and there is
no sensible way to revert that. You should use optional binding
or other unwrapping mechanisms instead, for example

if let text = transDescrInput.text {
transactions.append(Transaction(discr: text, amount: Int(tempAmount)))
}

or with nil-coalescing:

transactions.append(Transaction(discr: transDescrInput.text ?? "", amount: Int(tempAmount)))

As a rule of thumb, String(describing:) almost never the correct
solution (even if the compiler suggest it as a Fix-it), it only hides
the actual problem.

can't remove optional() from string after updating to latest xcode?

Your value is probably an optional containing an optional, so you'll have to unwrap it twice:

if let temp = KeychainWrapper.stringForKey("username"), let username = temp {
retrievedUsername = username
}

If this doesn't work, this is because it's not a double optional, and it means that your original string already contains the text "Optional(HK)" due to a prior error.

Swift: Can't get rid of Optional when setting cell text label

For anyone else who had the same issue as me, I managed to work out the answer, helped greatly by this thread: Cannot invoke 'append' with an argument list of type '(String?!)'

In AppDelegate I changed my code:

self.levelArrayName += [["\(name)"]]

To

if let nameArray = name as String! {self.levelArrayName.append(name!)}

And then no need to unwrap in tableviewcontroller, so this code worked:

cell.textLabel?.text = levelArrayName[indexPath.row]

This may seem a easy answer, but I hope this helps others

How to remove Optional from String Value Swift

Just like @GioR said, the value is Optional(52.523553) because the type of latstring is implicitly: String?. This is due to the fact that
let lat = data.value(forKey: "lat")
will return a String? which implicitly sets the type for lat.
see https://developer.apple.com/documentation/objectivec/nsobject/1412591-value
for the documentation on value(forKey:)

Swift has a number of ways of handling nil. The three that may help you are,
The nil coalescing operator:

??

This operator gives a default value if the optional turns out to be nil:

let lat: String = data.value(forKey: "lat") ?? "the lat in the dictionary was nil!"

the guard statement

guard let lat: String = data.value(forKey: "lat") as? String else {
//Oops, didn't get a string, leave the function!
}

the guard statement lets you turn an optional into it's non-optional equivalent, or you can exit the function if it happens to be nil

the if let

if let lat: String = data.value(forKey: "lat") as? String {
//Do something with the non-optional lat
}
//Carry on with the rest of the function

Hope this helps ^^



Related Topics



Leave a reply



Submit