mikeash.com pyblog/friday-qa-2011-08-05-method-signature-mismatches.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsmikeash.com Recent CommentsFri, 29 Mar 2024 09:22:53 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssCedric Luthi - 2011-08-17 09:27:03http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsFor a great example of passing variable arguments when the method expects fixed arguments that does not work, have a look at <a href="https://gist.github.com/937908">https://gist.github.com/937908</a> <br /> <br />This undefined behavior happens with the gcc version that comes with Xcode 3.2.6fd3fa9b918b455d7b45b6b3a61e8f889Wed, 17 Aug 2011 09:27:03 GMTmikeash - 2011-08-07 14:29:16http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsIf you cast the receiver of the message to the correct class, then that will indeed solve the problem. It's ugly, but if you need to do it then it gets the job done. <br /> <br />This is actually a pretty common pattern when multiple init methods exist with the same name but different signatures: <br /><code> <br />&nbsp;&nbsp;&nbsp;&nbsp;[[SomeClass alloc] initWithThingy: 7]; // warns because +alloc returns id <br />&nbsp;&nbsp;&nbsp;&nbsp;[(SomeClass *)[SomeClass alloc] initWithThingy: 7]; // works <br /></code> <br />Fortunately, Apple's newer compilers are becoming smart enough to know that +alloc returns an instance of its receiver, so this particular example is going away.7803f950db9e8bfa9b216dcbe45715beSun, 07 Aug 2011 14:29:16 GMTgroue - 2011-08-07 04:50:14http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsI did not dig deep in that subject, but once in a while, I remember having explicitly casted to the class owning the intended selector, in order to avoid compiler warnings (and I never had runtime problems). ... So I cargo-cult-induced that this cast was enough to let the compiler generate the correct code. What do you think?0d66d061e0b01f1854134756ebb4a2b0Sun, 07 Aug 2011 04:50:14 GMTmikeash - 2011-08-05 20:58:55http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsRight, Java is in the same category as Objective-C, as a mostly OO language with non-OO parts.a21b2d0434bed94a990fb160b644c1acFri, 05 Aug 2011 20:58:55 GMTKelvin Kao - 2011-08-05 19:50:39http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsSo I guess Java can't be considered a true object oriented language then? It also uses C-like primitives...9dcbd4d8ad3b1bb782e7bb992a5f72a5Fri, 05 Aug 2011 19:50:39 GMTScott - 2011-08-05 19:00:29http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsa further note: signatures and integer sizes are more fun when you use classdump. <br /> <br />If one happens to be using private APIs (which of course, none of us would do) and using ClassDump to get the headers, you have to be extra careful because generating a classdump on different architectures may return different signatures -- especially with NSInteger and its kin because class dump will return the size used for the architecture and not NSInteger. <br /> <br /> <br />if classdump i386 show a int, and classdump x86_64 shows a long long, then the header should be an NSInteger. <br /> <br />If i386 shows a int and x86_64 shows an int, then the header should be an int <br /> <br />If i386 shows a long long and x86_64 shows an long long, then the header should be an long long <br /> <br />fb08234b4bc28bc4aa1fae42551ce008Fri, 05 Aug 2011 19:00:29 GMTmikeash - 2011-08-05 17:20:44http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsReally, the entire existence of <code>NSNumber</code> is due to the C heritage. In true object oriented languages, like Smalltalk and many others, there's no need for classes like <code>NSNumber</code> because there are no primitives, and the language's built-in numbers are already objects.062e9d615b9261c7144159d340af0f64Fri, 05 Aug 2011 17:20:44 GMTJesper - 2011-08-05 17:11:51http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-05-method-signature-mismatches.html#commentsIt's a very interesting point that maybe some methods have selectors like numberWithUnsignedLongLong: simply because they have to. The name Objective-C belies how much of the "objective" part is shaped by the "C".0178dda22c6110dbee1291ee063e6190Fri, 05 Aug 2011 17:11:51 GMT