Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created June 12, 2018 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramntry/5838deb93754d1058c1a07378f84fe42 to your computer and use it in GitHub Desktop.
Save ramntry/5838deb93754d1058c1a07378f84fe42 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#include <stdio.h>
@interface NSString (MyPrintable)
- (const char *)toCString;
@end
@implementation NSString (MyPrintable)
// NSString is a library provided class that doesn't have
// a toCString method. We're dynamically extending the class
// below adding the method to every class instance.
- (const char *)toCString {
return [self cStringUsingEncoding: NSASCIIStringEncoding];
}
@end
@interface NSNumber (MyPrintable)
- (const char *)toCString;
@end
@implementation NSNumber (MyPrintable)
// stringValue is a library provided method of NSNumber
// that returns NSString. We're dynamically extending
// NSNumber here with a new toCString method making
// use of the toCString we just added to NSString.
- (const char *)toCString {
return [[self stringValue] toCString];
}
@end
int main() {
// The array below contains instances of classes
// NSString, NSNumber, NSString, and NSNumber in order.
NSArray *list = @[@"3.14", @4.5, @"5", @6];
// NSString type annotation below is only used as a request
// to the compiler to check if the str variable is used
// as an instance of NSString, for instance, that only
// methods present in the NSString class are called.
// The compiler doesn't check that str actually is an
// NSString, and in fact, it's not as half of the time
// it's NSNumber. There will be neither warning nor error
// from the compiler here.
for (NSString *str in list)
printf("%s\n", [str toCString]);
int sum = 0;
for (NSNumber *num in list)
sum += [num intValue];
printf("%d\n", sum);
// The program successfully outputs the following:
//
// 3.14
// 4.5
// 5
// 6
// 18
//
// It's perfectly correct, there is no undefined
// behavior, no memory leaks or any other issues.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment