<div dir="ltr">It's easy to test this in a playground on OS X:<div><br></div><div>
<p class=""><span class=""> NSMutableData</span><span class="">(length: </span><span class="">1000000000000000</span><span class="">) // returns nil</span></p><p class=""><span class="">It makes sense that the swift-corelibs-foundation version of NSMutableData should work the same way.</span></p></div><div class="gmail_extra"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Thu, Apr 14, 2016 at 11:56 AM, Jens Alfke via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Apr 14, 2016, at 10:45 AM, soyer via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br><div><span style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">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-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"></div></blockquote><div><br></div></span><div>Because the Objective-C declaration in <Foundation/NSData.h> declares the return type as ‘nullable’:</div><div><br></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Consolas"><span>- (</span><span style="color:#005493">nullable</span><span> </span><span style="color:#005493">instancetype</span><span>)initWithLength:(</span><span style="color:#703daa">NSUInteger</span><span>)length;</span></div></div><span class=""><br><blockquote type="cite"><div><span style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">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-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"></div></blockquote></span></div><br><div>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><br></div><div>(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><span class=""><div><br></div><div><blockquote type="cite"><span style="font-family:Alegreya-Regular">The code on github calls a non failable initializer.</span><br style="font-family:Alegreya-Regular"></blockquote><br></div></span><div>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><br></div><div>—Jens</div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>