1

I am writing below function to return value based on data objet that I passed to this method.

If we get data null I am trying to return "-" otherwise If I get any valid response I am returning value..

private func checkDataForValue(data: Optional<AnyObject>) -> String {
    if let value = data {
        let value1: AnyObject = //have to check <Null>
        if value === value1 {
            return "-"
        }
        return "\(value)"
    } else {
        return "-"
    }
}

My question How to compare null value here ..

tp2376
  • 690
  • 1
  • 7
  • 23
  • 1
    The `if let` is already check to see if `data` is `nil` or not. `value` can't be `nil`. – rmaddy Aug 27 '18 at 17:02
  • `AnyObject` can't be null. Only optionals can be null. Except for perhaps `NSNull`, I don't know much about how that bridges into swift from ObjC – Alexander Aug 27 '18 at 17:02
  • Thanks rmaddy ..but here value coming as and it's coming into the loop not going out..! – tp2376 Aug 27 '18 at 17:09
  • Possibly helpful: https://stackoverflow.com/questions/29954806/how-to-check-if-nsmutablearray-element-is-nsnull-or-anyobject – Martin R Aug 27 '18 at 18:58

2 Answers2

1

Most likely you are looking to compare value with NSNull.

You can update your code as follows:

private func checkDataForValue(data: Any?) -> String {
    if let value = data {
        if value is NSNull != nil {
            return "-"
        } else if (value as? String)?.lowercased() == "<null>" {
            return "-"
        } else {
            return "\(value)"
        }
    } else {
        return "-"
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    Why don't you use `value is NSNull` instead of `value as? NSNull != nil` ? – OOPer Aug 27 '18 at 18:52
  • @OOPer Because sometimes you forget the obvious. :) That would be simpler. Thanks. – rmaddy Aug 27 '18 at 20:02
  • Thanks rmaddy, Here I am trying to get value and "value as? NSNull == nil ? "\(value)" : "-" " Is giving me value with optional character(optional(8))..Any ideas how to remove that optional char .. – tp2376 Aug 27 '18 at 20:09
  • I want value if it's not nill and if it's nill "-" but above logic is not giving "-" when it's nill. – tp2376 Aug 27 '18 at 20:34
  • If `data` is `nil` then you get `-` from the `else`. If `data` is not `nil` then `value is compared against `NSNull`. If `value` is an instance of `NSNull`, you get `-`, otherwise you get a string representation of `value`. If you are still getting `` then `data` isn't `nil` nor is it an instance of `NSNull`. It must be a `String` with the value ``. After the `if let`, before the `return`, add `print(type(of: value))`. Update your question with the result of that line. – rmaddy Aug 27 '18 at 20:40
  • print(type(of: value)) it will have Int and String objets.. If data is nill it's not going into else loop it's still going into if loop..! – tp2376 Aug 27 '18 at 21:14
  • You need to actually add that line and run your code. Tell is the output of that `print` when you are getting `` as a result. – rmaddy Aug 27 '18 at 21:17
  • I added that line and the value is nil but it's going into if loop, when I print that I am getting Optional or Optional. – tp2376 Aug 28 '18 at 13:37
  • Sounds like your data contains the literal string "" Again, telling us how you're getting the original data *might* help us figure out what's going on. But we still don't know exactly what @rmaddy's fragment prints WHEN you get the "" result. – David Berry Aug 28 '18 at 15:29
  • @tarun Try my update. This deals with the literal string `""`. And there's no loop here. Instead of saying the "if loop", say the "if block" vs "else block". – rmaddy Aug 28 '18 at 15:57
  • @rmaddy Yes It did .. Thank u so much – tp2376 Aug 31 '18 at 13:53
0

I'd take advantage of the power of switch cases and go with:

private func checkDataForValue(data: Any?) -> String {
    switch data {
    case .none, .some(is NSNull):       return "-"
    case .some(let value as String):    return value == "<null>" ? "-" : value
    case .some(let value):              return "\(value)"
    }
}
David Berry
  • 40,941
  • 12
  • 84
  • 95
  • As @rmaddy suggests, try adding `print(type(of: data))` to see what it is that you're really getting then. It may just be a string containing "" and not an `NSNull` issue at all. It also might help to have some context around your question and the usage of `checkDataForValue` – David Berry Aug 27 '18 at 21:14
  • If I add print(type(of: data)) in above code I am getting and It's not a NSNull it is String though.. – tp2376 Aug 28 '18 at 14:01
  • Updated the chunk above to cover the string literal "" case. – David Berry Aug 28 '18 at 15:57