3

How to remove Optional("") text on optional value when displaying without forcing to !.

Update

// I have somthing like this declared outside class
// I put question mark wrapper since I don't know when this session might have a value
var url = "\(self.session?.apiURL)/api/products.json"

// private session
private var _session:Session?

class MyClass
{
   .
   .
   .


   // the value of apiURL depends on session, session has optional value and declared as
   // custom lazy loaded var session
   var session:Session?
   {
      get
      {  
         if _session == nil 
         {
           _session = // fetch from coredata store if there is an active session. Might return nil

           // if no active session
           if _session == nil
           {
              // I just print "No active session"
           }
         }

         // return _session may or may not contain any value
         return _session
      }
   }
}

When the session has a value the url has a value:

Optional("my_api_url_here")/api/products.json
Allan Macatingrao
  • 2,071
  • 1
  • 20
  • 28

3 Answers3

4

You can use this pod http://cocoapods.org/pods/NoOptionalInterpolation.

Alternatively, add this code to your project to remove the Optional(...) and nil text in string interpolation:

public protocol Unwrappable {
    func unwrap() -> Any?
}

extension Optional: Unwrappable {
    public func unwrap() -> Any? {
        switch self {
        case .None:
            return nil
        case .Some(let unwrappable as Unwrappable):
            return unwrappable.unwrap()
        case .Some (let some):
            return some
        }
    }
}

public extension String {
    init(stringInterpolationSegment expr: Unwrappable) {
        self = String(expr.unwrap() ?? "")
    }
}

Please note that simply overriding the description function of Optional won't work for string interpolation, although it works for print.

Thanh Pham
  • 2,021
  • 21
  • 30
  • Awesome solution here, and it helped me improve my own custom operator to handle nested optionals perfectly. For those who don't wish to override the default functionality, consider [my own answer here](http://stackoverflow.com/a/37843344/994104). – Jeremy Jun 15 '16 at 18:51
3

you can use ?? (null coalescing operator) to unwrap it and provide a default value if it is nil

let sessionApiURL = self.session?.apiURL ?? ""
var url = "\(sessionApiURL)/api/products.json"
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • Note that this only works well when working with optionals of type `String?`. You won't be able to do something like: `let i: Int? = 88; let s = i ?? "nil"` – Jeremy Jun 15 '16 at 18:16
0

If you want to without optional value you have to unwrap the Optional. You can use "optional binding" to unwrap an Optional:

if let url = self.session?.apiURL{
     //you can use url without optional
     print(url)
}

You can check my example in online swift playground for better understand.

slymnozdmrc
  • 390
  • 2
  • 8
  • 20