A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Fibonacci in Objective-C page! Here, you'll find the source code for this program as well as a description of how the program works.
#import <Foundation/Foundation.h>
@interface NSString (Parsing)
@property(nonatomic, readonly, nullable) NSNumber* strictIntegerNumber;
@end
@implementation NSString (Parsing)
- (NSNumber*)strictIntegerNumber {
NSString* trimmed = [self
stringByTrimmingCharactersInSet:NSCharacterSet
.whitespaceAndNewlineCharacterSet];
if (trimmed.length == 0) return nil;
NSInteger offset = 0;
if ([trimmed hasPrefix:@"-"] || [trimmed hasPrefix:@"+"]) {
offset = 1;
}
if (trimmed.length <= offset) return nil;
NSString* core = [trimmed substringFromIndex:offset];
NSRange invalidRange =
[core rangeOfCharacterFromSet:NSCharacterSet.decimalDigitCharacterSet
.invertedSet];
if (invalidRange.location != NSNotFound) {
return nil;
}
return @(trimmed.longLongValue);
}
@end
static void PrintFibonacciSequence(NSUInteger count) {
unsigned long long previous = 0;
unsigned long long current = 1;
for (NSUInteger i = 1; i <= count; i++) {
printf("%ld: %llu\n", i, current);
unsigned long long next = previous + current;
previous = current;
current = next;
}
}
int main(int argc, const char* argv[]) {
@autoreleasepool {
const char* usage =
"Usage: please input the count of fibonacci numbers to output";
if (argc < 2) {
puts(usage);
return 1;
}
NSNumber* number = @(argv[1]).strictIntegerNumber;
if (!number || number.integerValue < 0) {
puts(usage);
return 1;
}
PrintFibonacciSequence(number.unsignedIntegerValue);
}
return 0;
}
Fibonacci in Objective-C was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.