Assigning Null to NSMutableData or checking if its empty

43 views Asked by At

I need to return null or an empty value to identify an error condition. Since NSMutableData is not assignable to NULL in the following method, I'm just initialising it using the init() method.

Now how I check if the data is empty or is it possible to return null?

func generatedata(input:URL)->NSMutableData?
{
    if(errorcondition)
    {
        return NSMutableData.init()
    } 
} 
1

There are 1 answers

0
vadian On

The swifty way is to throw an error and return non-optional Data on success

func generatedata(input: URL) throws -> Data
{
   if errorcondition { throw anError }  

   return ...
} 

anError represents a custom error conforming to Error

Or return empty Data() which can be checked with isEmpty

But never use reference type NS(Mutable)Data in Swift