Basic Objective-C Pointer Question

#import <Foundation/Foundation.h>

BOOL areIntsDifferent( int thing1, int thing2 ) {
    if (thing1 == thing2) {
        return (NO);
    } else {
        return (YES);
    }
}

NSString *boolString (BOOL yesNo) {
    if (yesNo == NO) {
        return( @"NO" );
    } else {
        return( @"YES" );
    }
}

int main (int argc, const char * argv[]) {
    BOOL areTheyDifferent;

    areTheyDifferent = areIntsDifferent (5,5);

    NSLog(@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent));

    areTheyDifferent = areIntsDifferent (23,42);

    NSLog(@"are %d and %d different? %@", 23, 42, boolString(areTheyDifferent));

    return (0);
}

This is the whole program. This is not terribly complicated, but it illuminates the general problem that I am facing is understanding pointers. In this situation, is the return value of the boolString function a pointer because there is no memory allocated to hold the string? And if so, why is notIntsDifferent returns a pointer value to a BOOL value? Is it possible to rewrite this program so that the return value of boolString is a string and not a pointer to a string? If yes, how? I tried to fix *, but then I got a compiler error.

+3
source share
2 answers

NSString - . Obj-C , .

A BOOL - . , .

, , (, UIImage), . , . . NSArray UIImage NSString NSNumber. Apple, . , , , , .

+4

Objective-C , .

...

boolString , , ?

boolString @"YES" @"NO" , NSString. , , NSString*, , @"YES" @"NO" , , .

isIntDifferent BOOL?

BOOL - / .

, boolString , ?

, , Objective-C.

+2

Source: https://habr.com/ru/post/1769924/


All Articles