What you cannot do in C, what you can do in Objective-C?

What cannot you do in C (standard C99) that you can do in Objective-C? (with sample code if you want)

+5
source share
4 answers

I am going to be a little presumptuous and disagree with everyone else. Although it is technically true that everything possible in one language is possible in another (where "possibly" means "computable"), they differ in what can be expressed naturally and easily . A computer can do the same in response to code that you write in C, but you write radically different code to get it to do these things.

As others have said, Objective-C provides a complete runtime library written in C that will allow you to create Objective-C data structures and call C functions, but the code for this will be very verbose, pretty cool and completely imperative. In Objective-C, code is more declarative, more concise, and more readable.

, Objective-C C , -. , , Objective-C:

@interface NumberAdder : NSObject {
    int storedValue;
}

- (id)initWithStoredValue:(int)value;
- (int)resultOfAddingStoredValue:(int)numberToAdd;
@end

@implementation NumberAdder
- (int)resultOfAddingStoredValue:(int)numberToAdd {
    return numberToAdd + storedValue;
}

- (id)initWithStoredValue:(int)value {
    if (!(self = [super init])) return nil;
    storedValue = value;
    return self;
}
@end

int main() {
    id adder = [[NumberAdder alloc] initWithStoredValue:4];
    int result = [adder resultOfAddingStoredValue:3];
    printf("It is %d\n", result);
    return 0;
}

, C Objective-C ( , ):

int returnPlusStoredValueImp(id self, SEL _cmd, int arg) {
    int *storedValue = nil;
    object_getInstanceVariable(self, "storedValue", &storedValue)
    return arg + *storedValue;
}

id numberAdderInit(id self, SEL _cmd, int valueToStore) {
    objc_super superInfo = {self, objc_lookupClass("NSObject")};
    self = objc_msgSendSuper(super_info, sel_getName("init"));
    if (!self) return nil;
    object_setInstanceVariable(self, "storedValue", &valueToStore);
    return self;
}

void createNumberAdderClass() __attribute(constructor)__ {
    Class NumberAdder = objc_allocateClassPair(objc_lookupClass("NSObject"), "NumberAdder", 0);
    if (!NumberAdder) return;
    class_addIvar(NumberAdder, "storedValue", sizeof(int), 4, "i"); // I'm actually not sure if the fourth argument is correct, so it probably wrong, but just take that as a sign of how much this way of coding sucks
    objc_registerClassPair(NumberAdder);
    SEL nameOfPlusStoredValue = sel_registerName("resultOfAddingStoredValue:");
    SEL nameOfInit = sel_registerName("initWithStoredValue:");
    class_addMethod(NumberAdder, nameOfPlusStoredValue, returnPlusStoredValueImp, "i@:i");
    class_addMethod(NumberAdder, nameOfInit, numberAdderInit, "@@:i");
}

int main() {
    id adder = objc_msgSend(objc_lookupClass("NumberAdder"), sel_getName"alloc");
    adder = objc_msgSend(adder, sel_getName("initWithStoredValue:"), 4);
    int result = (int)objc_msgSend(adder, sel_getName("resultOfAddingStoredValue:"), 3);
    printf("It is %d\n", result);
    return 0;
}
+9

. Objective-C OO , C.

, Objective-C , C - , GCC/LLVM C, Objective-C, ObjC.

+8

, Objective-C, C. , C , , . .

( , , , , , OO- C - ...)

:

+3

. , .

+1

All Articles