Giter VIP home page Giter VIP logo

handlenull's Introduction

HandleNull

Check object or any value is null or not with its availability. This demo checks the nullability of any object or value which may come from API response and handle it properly accordingly. This demo is specially used in Swift programming language.

What is Null?

It's called nil in Swift which means the absence of a valid object.

Basic Config:

  1. Xcode 10.0 or above
  2. Swift 4.0 or above

How it Works?

You just need to download this demo and run it inot SIMULATOR or REAL device whatever you want. OR Just copy and paste code from Utility.swift file into your projects and access it!

Utility code for check nil value or nil object is:

static func isObjectOrValueAvailable(someObject: Any?) -> Any?
{
    if someObject is String {
            
        if let someObject = someObject as? String {
                
            return someObject
        } else {
                
            return ""
        }
    } else if someObject is Array<Any> {
            
        if let someObject = someObject as? Array<Any> {
                
            return someObject
        } else {
              
            return []
        }
    } else if someObject is Dictionary<AnyHashable, Any> {
            
        if let someObject = someObject as? Dictionary<String, Any> {
            
            return someObject
       } else {
              
            return [:]
        }
    } else if someObject is Data {
          
        if let someObject = someObject as? Data {
            
            return someObject
        } else {
              
            return Data()
        }
    } else if someObject is NSNumber {
          
        if let someObject = someObject as? NSNumber{
            
            return someObject
        } else {
              
            return NSNumber.init(booleanLiteral: false)
        }
    } else if someObject is UIImage {
          
        if let someObject = someObject as? UIImage {
            
            return someObject
        } else {
             
            return UIImage()
        }
    } else {
            
        return "NoObjectORValueAvailable"
    }
}

Access of above function

Using below if-else condition esily find object or value nil or not & if it's not proper it will handle with default initialized value:

let intValue: Int? = nil // some integer value but null
     
if "\(Utility.isObjectOrValueAvailable(someObject: intValue as AnyObject)!)" != "NoObjectORValueAvailable" {

    print("Value found")
    // do your stuff with value
} else {

    print("Value not found!")
    // handle stuff here if no integer found
}

License

This line of codes are avaialble for public use by 9Brainz. If you have any doubts or query regarding this code or any particular topic reagrding iOS Application development, Feel free to Contact Us.

Helpful!!

Is this demo helpful for your project? OR Reducing tiny bit of work in your project? Let us know about it ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

handlenull's People

Contributors

ihardiktrivedi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.