Unable to return an optional as an optional - swift

Why can't I get Swift to return a value as an optional.
I have a funtion that checks if an optional contains a value and return it as an optional if it isn't:
var someOptional: String?
func checkIfOptional<T>(value: T?) -> (String, T) {
if let _value = value {
return (("Your optional contains a value. It is: \(_value)"), (_value))
} else {
return (("Your optional did not contain a value"), (value?)) //ERROR: Value of optional type 'T?' not unwrapped; did you mean to use '!' or '?'?
}
}
When the optional is nil. Ist should return the same optional the was given to the function.
If there is a value. It should return the unwrapped value.

If you want to return an optional you have to declare the return type as optional
func checkIfOptional<T>(value: T?) -> (String, T?) {
if let _value = value {
return ("Your optional contains a value. It is: \(_value)", value)
} else {
return ("Your optional did not contain a value", value)
// or even return ("Your optional did not contain a value", nil)
}
I removed all unnecessary parentheses.

You may want to declare an enum like this:
enum Value<T> {
case full(String, T)
case empty(String, T?)
}
func checkIfOptional<T>(_ value: T?) -> Value<T> {
if let _value = value {
return .full("Your optional contains a value. It is: \(_value)", _value)
} else {
return .empty("Your optional did not contain a value.", value)
}
}
var toto: String?
print(checkIfOptional(toto)) // empty("Your optional did not contain a value", nil)
print(checkIfOptional("Blah")) // full("Your optional contains a value. It is: Blah", "Blah")
To treat a Value you should use switch this way:
var toto: String?
let empty = checkIfOptional(toto)
let full = checkIfOptional("Blah")
func treatValue<T>(_ value: Value<T>) {
switch(value) {
case .full(let msg, let val):
print(msg)
print(val)
case .empty(let msg, _):
print(msg)
}
}
treatValue(empty) // Your optional did not contain a value.
treatValue(full) // Your optional contains a value. It is: Blah\nBlah
But all of this seems to me to only add needless complexity to the straightforward type that is Optional. So you might want to expand on what you are trying to achieve here.

Related

Accessing Typed Value of Generic Argument in Swift

I am trying to make a dispatch function which can take Payload as an argument. The Payload can be Int, String or any other type. I tried the following approaches but inside the dispatch function the payload.value is always T and not Int or String. Casting is an option but I thought that was the whole point of generics.
struct Payload<T> {
let value: T
}
func dispatch<T>(payload: Payload<T>) {
print(payload.value) // get the value as Int, String or some other type
}
let payload = Payload<Int>(value: 100)
let payload2 = Payload<String>(value: "FOO")
dispatch(payload: payload)
dispatch(payload: payload2)
As you already know T is unconstrained so it can be any type. Your only option is to cast the value to the types you are expecting. You can simply switch the value:
switch payload.value {
case let value as Int:
print("Int:", value)
case let value as String:
print("String:", value)
default:
print("some other type", payload.value)
}
Depends on what you want to get inside your dispatch, you can create a protocol and requires T to be conformed to that protocol.
protocol Printable {
func printValue() -> String
}
struct Payload<T> {
let value: T
}
func dispatch<T: Printable>(payload: Payload<T>) {
print(payload.value.printValue()) // get the value as Int, String or some other type
}
extension String: Printable {
func printValue() -> String {
return self
}
}
extension Int: Printable {
func printValue() -> String {
return "\(self)"
}
}

Swift - unwrapping a Double value with a Guard let statement

to unwrap a Double from my default values, I seem to have to do it with two guard let statements to unwrap the value safely such as in what follows:
guard let distanceAwayPreference = self.defaults.string(forKey: "distancePreference") else {
return
}
guard let doubleDAP = Double(distanceAwayPreference) else {
return
}
– because if I do it like this:
guard let DistanceAwayPreference = self.defaults.double(forKey: "distancePreference") else {
return
}
– I get the error:
Initializer for conditional binding must have Optional type, not 'Double'
Is there a better way so i can do it once, or have less code throughout my app?
The method double(forKey:) is not returning an optional at all. According to the documentation it returns:
The double value associated with the specified key. If the key doesn‘t exist, this method returns 0.
So you can't optional bind it to a variable. This is why you've got that error:
Initializer for conditional binding must have Optional type, not 'Double'
For less and cleaner code (when using it), you can use a simple property wrapper like:
#propertyWrapper
struct UserDefault<T> {
let key: String
let defaultValue: T
init(_ key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get { UserDefaults.standard.object(forKey: key) as? T ?? defaultValue }
set { UserDefaults.standard.set(newValue, forKey: key) }
}
}
Then you can have a custom helper struct like this:
struct UserDefaultsConfig {
#UserDefault("distancePreference", defaultValue: 0)
static var distancePreference: Double
}
And use it late like:
UserDefaultsConfig.distancePreference = 12
I think it's better to return Any from defaults instead of String. So, this way you can use optional casting.
guard let doubleDAP = self.defaults.object(forKey: "distancePreference") as? Double else {
return
}

Optional extension for any types

I want to write Optional extension for any types.
My code for integer:
extension Optional where Wrapped == Int {
func ifNil<T>(default: T) -> T {
if self != nil {
return self as! T
}
return default
}
}
var tempInt: Int?
tempInt.ifNil(default: 2) // returns 2
tempInt = 5
tempInt.ifNil(default: 2) // returns 5
It works but it is Optional(Int) extension (Optional where Wrapped == Int), I want to use this extension for any types like Date, String, Double etc.
What are your suggestions?
The answer to your basic question is to just remove the where clause:
extension Optional {
// ... the rest is the same
func isNil<T>(value: T) -> T {
if self != nil {
return self as! T
}
return value
}
}
Now it applies to all Optionals.
But this code is quite broken. It crashes if T is not the same as Wrapped. So you would really mean a non-generic function that works on Wrapped:
extension Optional {
func isNil(value: Wrapped) -> Wrapped {
if self != nil {
return self! // `as!` is unnecessary
}
return value
}
}
But this is just an elaborate way of saying ?? (as matt points out)
extension Optional {
func isNil(value: Wrapped) -> Wrapped { self ?? value }
}
Except that ?? is much more powerful. It includes an autoclosure that avoids evaluating the default value unless it's actually used, and which can throw. It's also much more idiomatic Swift in most cases. You can find the source on github.
public func ?? <T>(optional: T?, defaultValue: #autoclosure () throws -> T)
rethrows -> T {
switch optional {
case .some(let value):
return value
case .none:
return try defaultValue()
}
}
But I can imagine cases where you might a method-based solution (they're weird, but maybe there are such cases). You can get that by just rewriting it as a method:
extension Optional {
public func value(or defaultValue: #autoclosure () throws -> Wrapped) rethrows -> Wrapped {
switch self {
case .some(let value):
return value
case .none:
return try defaultValue()
}
}
}
tempInt.value(or: 2)
Optional is already a generic. It already takes any type as its parameterized type. Its parameterized already has a name: Wrapped. Just say Wrapped instead of T. Your T is Wrapped.
extension Optional {
func isNil<Wrapped>(value: Wrapped) -> Wrapped {
if self != nil {
return self as! Wrapped
}
return value
}
}
If you really prefer T for some reason, use a type alias. It's only a name:
extension Optional {
typealias T = Wrapped
func isNil<T>(value: T) -> T {
if self != nil {
return self as! T
}
return value
}
}
But in any case your extension is completely unnecessary because this is what the nil-coalescing operator ?? already does.
var tempInt: Int?
tempInt ?? 2 /// returns 2
tempInt = 5
tempInt ?? 2 /// returns 5

overloading methods where only difference is optional vs. non-optional type

I was under the impression that swift can have overloaded methods that differ only in the type of object that the methods return. I would think that I could have two funcs with the same signature yet they differ in return type.
import Foundation
// ambiguous use of 'IsTextEmpty(text:)'
func IsTextEmpty(text : String?) -> Bool? {
return text?.isEmpty
}
func IsTextEmpty(text : String?) -> Bool {
guard let text = text else {
return true
}
return text.isEmpty
}
let text: String? = nil
if let empty = IsTextEmpty(text:"text") {
print("Not Empty")
}
if IsTextEmpty(text: text) {
print("Empty")
}
Here, both functions have the same input parameters but one func returns an optional Bool? and the other returns a Bool. In this case I get an error:
ambiguous use of 'IsTextEmpty(text:)'
If I change the name of one of the input parameters I no longer get the ambiguous error:
// Works
func IsTextEmpty(foo : String?) -> Bool? {
return foo?.isEmpty
}
func IsTextEmpty(text : String?) -> Bool {
guard let text = text else {
return true
}
return text.isEmpty
}
let text: String? = nil
if let empty = IsTextEmpty(foo:"text") {
print("Not Empty")
}
if IsTextEmpty(text: text) {
print("Empty")
}
Shouldn't the compiler detect that they are two distinct methods even though their return types are different, since an optional Bool? is a different type from a non-optional Bool?
The compiler needs some context to decide which method to call, e.g.
let e1 = IsTextEmpty(text: text) as Bool
let e2: Bool = IsTextEmpty(text: text)
to call the non-optional variant, or
let e3 = IsTextEmpty(text: text) as Bool?
let e4: Bool? = IsTextEmpty(text: text)
to call the optional variant. Now
if IsTextEmpty(text: text) {
print("Empty")
}
compiles without problems: the if-statement requires a boolean
expression, so there is no ambiguity here.
But apparently the compiler is not smart enough to figure out that
in the context of an optional binding the expression on the right-hand side must be some optional
and to infer the unwrapped type automatically.
You need to annotate the type of empty explicitly:
if let empty: Bool = IsTextEmpty(text: text) { ... }
or to make the return type explicit:
if let empty = (IsTextEmpty(text: text) as Bool?) { ... }

Simplest way to convert an optional String to an optional Int in Swift

It seems to me there ought to be a simple way to do an optional conversion from a String to an Int in Swift but I can't figure it out.
value is a String? and I need to return an Int?.
Basically I want to do this, but without the boilerplate:
return value != nil ? Int(value) : nil
I tried this, which seems to fit with Swift's conventions and would be nice and concise, but it doesn't recognize the syntax:
return Int?(value)
You can use the nil coalescing operator ?? to unwrap the String? and use a default value "" that you know will produce nil:
return Int(value ?? "")
Another approach: Int initializer that takes String?
From the comments:
It's very odd to me that the initializer would not accept an optional and would just return nil if any nil were passed in.
You can create your own initializer for Int that does just that:
extension Int {
init?(_ value: String?) {
guard let value = value else { return nil }
self.init(value)
}
}
and now you can just do:
var value: String?
return Int(value)
You can use the flatMap() method of Optional:
func foo(_ value: String?) -> Int? {
return value.flatMap { Int($0) }
}
If value == nil then flatMap returns nil. Otherwise it
evaluates Int($0) where $0 is the unwrapped value,
and returns the result (which can be nil if the conversion fails):
print(foo(nil) as Any) // nil
print(foo("123") as Any) // Optional(123)
print(foo("xyz") as Any) // nil
With String extension you shouldn't worry about string == nil
extension String {
func toInt() -> Int? {
return Int(self)
}
}
Usage:
var nilString: String?
print("".toInt()) // nil
print(nilString?.toInt()) // nil
print("123".toInt()) // Optional(123)