Skip to content

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.

Notifications You must be signed in to change notification settings

9Brainz-iOS/HandleNull

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

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 🔥🔥🔥

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages