NSNumber of seconds to Hours, minutes, seconds

Have you tried creating an NSDate from that and printing that using an NSDateFormatter? NSDate *date = [NSDate dateWithTimeIntervalSince1970:[theNumber doubleValue]]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@”HH:mm:ss”]; NSLog(@”%@”, [formatter stringFromDate:date]); [formatter release]; If you need the individual values for hours, minutes, and seconds, use NSDateComponents as suggested by nall. The above won’t print the … Read more

get type of NSNumber

I recommend using the -[NSNumber objCType] method. It allows you to do: NSNumber * n = [NSNumber numberWithBool:YES]; if (strcmp([n objCType], @encode(BOOL)) == 0) { NSLog(@”this is a bool”); } else if (strcmp([n objCType], @encode(int)) == 0) { NSLog(@”this is an int”); } For more information on type encodings, check out the Objective-C Runtime Reference.

Is it possible to replicate Swifts automatic numeric value bridging to Foundation (NSNumber) for (U)Int8/16/32/64 types?

Yes (it’s possible): by conformance to protocol _ObjectiveCBridgeable (The following answer is based on using Swift 2.2 and XCode 7.3.) Just as I was pondering over whether to post or simply skip this question, I stumbled over swift/stdlib/public/core/BridgeObjectiveC.swift in the Swift source code, specifically the protocol _ObjectiveCBridgeable. I’ve briefly noticed the protocol previously at Swiftdoc.org, … Read more

Objective-C: Find numbers in string

Here’s an NSScanner based solution: // Input NSString *originalString = @”This is my string. #1234″; // Intermediate NSString *numberString; NSScanner *scanner = [NSScanner scannerWithString:originalString]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@”0123456789″]; // Throw away characters before the first number. [scanner scanUpToCharactersFromSet:numbers intoString:NULL]; // Collect numbers. [scanner scanCharactersFromSet:numbers intoString:&numberString]; // Result. int number = [numberString integerValue]; (Some of … Read more