objective c - Logically ANDing NSUInteger and String Type? -


i've searched stackoverflow , other sites, can't seem find answer.

in apple text editor source, have @ least 1 routine apparently strange logical anding between 2 non-boolean variables. casting them bools can done, doesn't make sense. i'm learning swift , less familiar objective-c, life of me, can't figure out how trying achieve goal stated "build list of encodings, sorted, , including human readable names."

here code:

/* return sorted list of available string encodings. */ + (nsarray *)allavailablestringencodings {     static nsmutablearray *allencodings = nil;     if (!allencodings) {    // build list of encodings, sorted, , including human readable names         const cfstringencoding *cfencodings = cfstringgetlistofavailableencodings();         cfstringencoding *tmp;         nsinteger cnt, num = 0;         while (cfencodings[num] != kcfstringencodinginvalidid) num++;   // count         tmp = malloc(sizeof(cfstringencoding) * num);         memcpy(tmp, cfencodings, sizeof(cfstringencoding) * num);   // copy list         qsort(tmp, num, sizeof(cfstringencoding), encodingcompare); // sort         allencodings = [[nsmutablearray alloc] init];           // put in nsarray         (cnt = 0; cnt < num; cnt++) {             nsstringencoding nsencoding = cfstringconvertencodingtonsstringencoding(tmp[cnt]);             if (nsencoding && [nsstring localizednameofstringencoding:nsencoding]) [allencodings addobject:[nsnumber numberwithunsignedinteger:nsencoding]];         }         free(tmp);     }     return allencodings; } 

the line in question contains "&&." guidance appreciated.

objective-c strict superset of c, same rules logical operators apply. in contrast swift, more strict types, logical operators in c take arbitrary scalar operands. (the boolean type bool did not exist in versions of c, added c99 standard.)

the c standard specifies (see e.g. http://port70.net/~nsz/c/c11/n1570.pdf, draft of c11 standard):

6.5.13 logical , operator

constraints

2 each of operands shall have scalar type.

semantics

3 && operator shall yield 1 if both of operands compare unequal 0; otherwise, yields 0. result has type int.

in case, in

if (nsencoding && [nsstring localizednameofstringencoding:nsencoding]) 

the left operand has type nsuinteger (which can unsigned long or unsigned int, depending on platform), , right operand has type nsstring *, pointer type. therefore above expression equivalent to

if (nsencoding != 0 && [nsstring localizednameofstringencoding:nsencoding] != 0) 

where 0 in right operand null pointer constant written null, or nil objective-c pointers:

if (nsencoding != 0 && [nsstring localizednameofstringencoding:nsencoding] != nil) 

some more information how relates swift

cocoa/cocoa touch objective-c methods return object pointer return nil indicate error (compare handling error objects returned methods in "error handling programming guide").

[nsstring localizednameofstringencoding:nsencoding] != nil 

would mean "no localized name encoding determined". swift equivalent method returning optional string, , check success with

nsstring.localizednameofstringencoding(nsencoding) != nil 

however, not compile, , here reason why: if option-click on objective-c localizednameofstringencoding method in xcode show declaration you'll see

+ (nsstring * _nonnull)localizednameofstringencoding:(nsstringencoding)encoding 

here _nonnull indicates method not expected return nil. kind of nullability annotations introduced improve mapping of objective-c methods swift, see example "nullability , objective-c" in swift blog.

because of _nonnull annotation, method imported swift as

public class func localizednameofstringencoding(encoding: uint) -> string 

so testing return value in objective-c can done makes no sense because method returns non-nil value. in swift compiler assumes return value never nil , returns non-optional string.

the translation of if-statement swift therefore be

if nsencoding != 0 {     // ... } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -