<div dir="ltr">This is even worse if I want to create a singleton, and ensure that the only way to access it, is through a static let on the class:<div><br></div><div>class Singleton: NSObject {</div><div> static let instance = Singleton()</div><div> private override init() {</div><div> super.init()</div><div> }</div><div>}</div><div><br></div><div>In Swift, I can't instantiate this class, since it has no internal/public initializers. However, since it inherits from NSObject (which is necessary to access it from objc), I can initialize it from objc, even though it's private. Annotating it with @nonobjc doesn't help either.</div><div><br></div><div>Is there a reason why Swift doesn't automatically declare accessors from the super class, that are either private or @nonobjc in the Swift subclass implementation, as __unavailable?</div><div><br></div><div>Is there anyway I can enforce it using some (combination of) attributes?</div><div><br></div><div><br></div><div>sv.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-06-08 15:12 GMT+02:00 Svein Halvor Halvorsen <span dir="ltr"><<a href="mailto:svein.h@lvor.halvorsen.cc" target="_blank">svein.h@lvor.halvorsen.cc</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi, <div><br></div><div>In Objective-C, I've often attributed initializer from super classes as unavailable. Like this;</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">@class</span><span style="color:rgb(0,0,0)"> </span><span>Dependency</span><span style="color:rgb(0,0,0)">;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">@interface</span><span> SomeClass : </span><span style="color:rgb(112,61,170)">NSObject</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">- (</span><span>nullable</span><span style="color:rgb(0,0,0)"> </span><span>instancetype</span><span style="color:rgb(0,0,0)">)init </span><span style="color:rgb(120,73,42)">__unavailable</span><span style="color:rgb(0,0,0)">;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>- (</span><span style="color:rgb(187,44,162)">nonnull</span><span> </span><span style="color:rgb(187,44,162)">instancetype</span><span>)initWithDependency:(</span><span style="color:rgb(187,44,162)">nonnull</span><span> </span><span style="color:rgb(112,61,170)">Dependency</span><span> *)dependency;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>@end</span></p></div><div><span><br></span></div><div>This makes the compiler complain if I try to instantiate <span style="font-family:Menlo;font-size:11px">[[</span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">SomeClass</span><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px;color:rgb(61,29,129)">alloc</span><span style="font-family:Menlo;font-size:11px">] </span><span style="font-family:Menlo;font-size:11px;color:rgb(61,29,129)">init</span><span style="font-family:Menlo;font-size:11px">];</span></div><div><br></div><div>However, If I declare a Swift class like this:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">class</span><span> SomeClass: </span><span style="color:rgb(112,61,170)">NSObject</span><span> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">init</span><span>(dependency: </span><span style="color:rgb(79,129,135)">Dependency</span><span>) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">super</span><span>.</span><span style="color:rgb(187,44,162)">init</span><span>()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></p></div><div><span><br></span></div><div>The generated header file will look like this:</div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">@interface</span><span> SomeClass : </span><span style="color:rgb(112,61,170)">NSObject</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>- (</span><span style="color:rgb(187,44,162)">nonnull</span><span> </span><span style="color:rgb(187,44,162)">instancetype</span><span>)initWithDependency:(</span><span style="color:rgb(112,61,170)">Dependency</span><span> * </span><span style="color:rgb(187,44,162)">_Nonnull</span><span>)dependency </span><span style="color:rgb(120,73,42)">OBJC_DESIGNATED_INITIALIZER</span><span>;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>@end</span></p></div><div><span><br></span></div><div>If I try to use init, it will be a run time error, and not a compile time error.</div><div>It there any way I can make Swift create the desired objc header file?</div><span class="HOEnZb"><font color="#888888"><div><br></div><div><br></div><div>Svein Halvor</div><div><br></div></font></span></div>
</blockquote></div><br></div>