[英]Swift: Why is the init?(length length: Int) NSMutableData's initializer failable?
The memory allocation can fail, but I think Swift doesn't handle that cases. 内存分配可能会失败,但我认为Swift不会处理这种情况。 The code on github calls a non failable initializer
github上的代码调用一个不可用的初始化程序
public convenience init?(length: Int) {
let memory = malloc(length)
self.init(bytes: memory, length: length, copy: false) { buffer, amount in
free(buffer)
}
}
EDIT: The code is from the native-Swift Foundation framework coming in Swift 3. 编辑:代码来自Swift 3中的native-Swift Foundation框架。
From the swift-users list: 来自swift-users列表:
"Swift's policy on memory allocation failure is that fixed-size object allocation is considered to be a runtime failure if it cannot be handled. OTOH, APIs that can take a variable and arbitrarily large amount to allocate should be failable. NSData falls into the later category." “Swift关于内存分配失败的策略是,如果无法处理,固定大小的对象分配被认为是运行时失败.OTOH,可以采用变量和任意大量分配的API应该是可用的.NSData属于后者类别。” -Chris
-克里斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.