Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
Adds and removes items in a shopping cart with total cost.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *cart = [NSMutableArray array];
NSMutableArray *prices = [NSMutableArray array];
void (^addItem)(NSString*, NSNumber*) = ^(NSString *item, NSNumber *price){
[cart addObject:item];
[prices addObject:price];
NSLog(@"Cart: %@", cart);
NSLog(@"Total: %@", [prices valueForKeyPath:@"@sum.self"]);
};
void (^removeItem)(NSInteger) = ^(NSInteger index){
[cart removeObjectAtIndex:index];
[prices removeObjectAtIndex:index];
NSLog(@"Cart: %@", cart);
NSLog(@"Total: %@", [prices valueForKeyPath:@"@sum.self"]);
};
// Simulate actions
addItem(@"Apple", @2);
addItem(@"Banana", @3);
removeItem(0);
}
return 0;
}Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to C. It has been the primary language for macOS and iOS development before Swift, enabling developers to create applications for Apple platforms with dynamic runtime capabilities.
Origin & Creator
Developed in the early 1980s by Brad Cox and Tom Love, combining C with Smalltalk-style object-oriented messaging.
Industrial Note
Objective-C remains critical for maintaining legacy macOS and iOS applications and for interfacing with older Apple frameworks that are not fully migrated to Swift.