[英]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.