Code · 1 file
Sample Objective-C File
Objective-C was Apple’s primary language before Swift and is still common in older iOS and macOS code. The Objective-C sample is a small program — a customer record, a VIP check and a formatted total — that is the same in every language we offer, so you can compare them side by side.
Objective-C · 861 B
Sample Objective-C file
An idiomatic Objective-C example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.
CC0 · free for any use · Verified by an independent tool · Report a problem
// Sample Objective-C program — samplefiles.com
#import <Foundation/Foundation.h>
@interface Customer : NSObject
@property (nonatomic) NSInteger identifier;
@property (nonatomic, copy) NSString *name;
@property (nonatomic) double balance;
- (BOOL)isVip;
@end
@implementation Customer
- (BOOL)isVip { return self.balance >= 10000; }
@end
int main(int argc, const char *argv[]) {
@autoreleasepool {
NSArray *data = @[@[@"Ada", @12500.0], @[@"Grace", @830.5], @[@"Linus", @10000.0]];
double total = 0;
for (NSArray *row in data) {
Customer *c = [Customer new];
c.name = row[0];
c.balance = [row[1] doubleValue];
NSLog(@"%-6@ %10.2f %@", c.name, c.balance, c.isVip ? @"VIP" : @"");
total += c.balance;
}File details
Download from the command line
curl -L -o Sample.m https://files.samplefiles.com/code/m/Sample.mwget https://files.samplefiles.com/code/m/Sample.mhttps://files.samplefiles.com/code/m/Sample.mprintf '%s %s\n' fe5c4bf54e8096076219e51e3af30ea90d12596d9de98c14a948f9187ecb24d0 Sample.m | shasum -a 256 -cOr check the download in your browser with the checksum calculator.