<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 14, 2016, at 10:45 AM, soyer via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Do you know why is the init?(length length: Int) NSMutableData's initializer failable?</span><br style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>Because the Objective-C declaration in &lt;Foundation/NSData.h&gt; declares the return type as ‘nullable’:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">- (</span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">nullable</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">instancetype</span><span style="font-variant-ligatures: no-common-ligatures" class="">)initWithLength:(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSUInteger</span><span style="font-variant-ligatures: no-common-ligatures" class="">)length;</span></div></div><br class=""><blockquote type="cite" class=""><div class=""><span style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">The memory allocation can fail, but I think Swift doesn't handle that cases. (it is not a real issue in a modern OS)</span><br style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote></div><br class=""><div class="">Not on a modern 64-bit desktop OS, but it can be an issue on some other platforms. In a 32-bit process (still supported on iOS and Mac OS) malloc can fail if the process's address space is fragmented enough that there are no free blocks large enough for the allocation. On an OS without a VM pager, like many embedded systems, malloc can fail if physical RAM is exhausted.</div><div class=""><br class=""></div><div class="">(I’m actually not sure if this is the reason why that initializer is failable. It’s possible this is a mistake, or Apple’s frameworks team had some other architectural reason for allowing it to fail. Historically, the design pattern for initializers in Objective-C always allows them to return nil.)</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><span style="font-family: Alegreya-Regular;" class="">The code on github calls a non failable initializer.</span><br style="font-family: Alegreya-Regular;" class=""></blockquote><br class=""></div><div class="">That’s for the native-Swift Foundation framework coming in Swift 3. I’d guess they’ve adopted the same method signature as in Apple’s Foundation, for compatibility.</div><div class=""><br class=""></div><div class="">—Jens</div></body></html>