简体   繁体   English

将 Null 分配给 NSMutableData 或检查其是否为空

[英]Assigning Null to NSMutableData or checking if its empty

I need to return null or an empty value to identify an error condition.我需要返回 null 或空值来识别错误情况。 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?由于NSMutableData在以下方法中不可分配给 NULL,我只是使用init()方法对其进行初始化。现在我如何检查数据是否为空或者是否可以返回 null?

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

The swifty way is to throw an error and return non-optional Data on success快速的方法是抛出错误并在成功时返回非可选Data

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

   return ...
} 

anError represents a custom error conforming to Error anError表示符合Error的自定义错误

Or return empty Data() which can be checked with isEmpty或者返回可以使用isEmpty检查的空Data()

But never use reference type NS(Mutable)Data in Swift但切勿在 Swift 中使用引用类型NS(Mutable)Data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM