Skip to content

Instantly share code, notes, and snippets.

@PaulTaykalo
Created January 29, 2017 22:14
Show Gist options
  • Select an option

  • Save PaulTaykalo/3320996ec1fc2e233f97005675cc38b9 to your computer and use it in GitHub Desktop.

Select an option

Save PaulTaykalo/3320996ec1fc2e233f97005675cc38b9 to your computer and use it in GitHub Desktop.
When floats stops working
#import <Foundation/Foundation.h>
@implementation NSObject (Private)
- (float)doSomethng {
return 42.0;
}
@end
int main(int argc, char * argv[]) {
@autoreleasepool {
NSObject * object = [NSObject new];
for (int i = 0; i < 10; i++) {
float value = [object doSomethng];
printf("%f\n", value);
[object performSelector:@selector(doSomethng) withObject:nil];
}
for (int i = 0; i < 10; i++) {
NSLog(@"Fix");
float value = [object doSomethng];
printf("%f\n", value);
[object performSelector:@selector(doSomethng) withObject:nil];
}
}
return 0;
}
@PaulTaykalo
Copy link
Author

PaulTaykalo commented Jan 29, 2017

When running code above, you'll see that after some point, every value you expect from the method, will return NaN instead of 42
Then in next cycle, you'll realize that you can fix this behavior just by adding NSLog before calling method 🤷‍♂️
Works on iPhone5 simulator only (Actually on any 32-bit simulators)

Output:

42.000000
42.000000
42.000000
42.000000
42.000000
42.000000
42.000000
42.000000
nan         <-----
nan         <-----
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000   <-----
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000
1901-12-13 22:47:56.-2147483648 FixedByNSLog[34050:54261102] Fix
42.000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment