• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Equivalent of isKindOfClass for Foundation Types?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Equivalent of isKindOfClass for Foundation Types?


  • Subject: RE: Equivalent of isKindOfClass for Foundation Types?
  • From: "Christopher Hickman" <email@hidden>
  • Date: Thu, 4 May 2006 14:28:49 -0400

>  I'd be very surprised if that worked. typeof() (like sizeof()) is a
>compile-time construct that gets replaced with the type of whatever
>you put in the brackets. As such, all this code would ever do is
>return NIL, because the type of NSTimeInterval will never be equal to
>the type of "id". I'm not even sure you can say typeof
>(NSTimeInterval) because that'd be the type of a type, not of a
>variable.

Hmm, when I was trying to figure out what to do, I found a C reference that
said that typeof(int) and typeof(a), where a is an int, are the same.
However, it hadn't occurred to me that as a compile-time construct, it will
never evaluate as I want. :(

>  TransformedValue is supposed to be of type "id", so I'd guess that
>you'll get an object passed in. Your best bet would probably be to
>check whether this object is of class NSNumber, or even better,
>whether it implements the floatValue (doubleValue?) method. If it
>does, that'd be your best guess at whether you got an NSTimeInterval.

NSValueTransformers are used by Cocoa Bindings to modify the contents of
something bound.  My binding goes to a method that returns an NSTimeInterval
(which is really just a double).  Perhaps Cocoa Bindings converts this on
the fly to an object... I'll have to test that, I guess.

>  Though I can't fathom why you would want to know that? What are you
>really trying to achieve, in high-level terms? Do you want the user
>to input a time interval? Why not use a slider or a stepper with a
>text field and a string representation, or hack one of the clock
>views on the net to do time intervals in HH:MM:SS format instead?

My value transformer takes a time interval (NSTimeInterval) and returns a
localized NSString in days, hours, minutes and seconds.

For example:

An input of (NSTimeInterval)90061 should return @"1 day, 1 hour, 1 minute, 1
second" in English locale (@"1 Tag, 1 Uhr, 1 Minute, 1 Sekunde" wenn man
Deutsch Locale hat.)

Here's the complete code:

@interface DaysHoursMinutesSecondsTransformer: NSValueTransformer {}
@end

@implementation DaysHoursMinutesSecondsTransformer

+ (Class)transformedValueClass { return [NSString class]; }
+ (BOOL)allowsReverseTransformation { return NO; }

- (id)transformedValue:(id)value {

	if (typeof(value) != typeof(NSTimeInterval)) {
		return nil;

	double remainder;
      unsigned long days, hours, minutes, seconds = 0;
	NSMutableString *transformed = [NSMutableString string];


	days = value / 86400; //seconds per day
      remainder = value % 86400;

	hours = remainder / 3600; //seconds per hour
	remainder = remainder % 3600;

	minutes = remainder / 60; //seconds per minute
	remainder = remainder % 60;

	seconds = remainder;

	if (days > 0) { //we only want to show days, hours, and minutes if
they are non-zero
		if (days < 2) { //if it is 1, we use the singular tag
			[transformed appendFormat:@"%u%@", 1,
NSLocalizedString(@" day, ")];
		}
		else {
			[transformed appendFormat:@"%u%@", days,
NSLocalizedString(@" days, ")];
		}
	}
	if (hours > 0) {
		if (hours < 2) {
			[transformed appendFormat:@"%u%@", 1,
NSLocalizedString(@" hour, ")];
		}
		else {
			[transformed appendFormat:@"%u%@", hours,
NSLocalizedString(@" hours, ")];
		}
	}
	if (minutes > 0) {
		if (minutes < 2) {
			[transformed appendFormat:@"%u%@", 1,
NSLocalizedString(@" minute, ")];
		}
		else {
			[transformed appendFormat:@"%u%@", minutes,
NSLocalizedString(@" minutes, ")];		}
	}
	if (seconds = 1) {
		[transformed appendFormat:@"%u%@", 1, NSLocalizedString(@"
second")];
	}
	else { //we always want to show seconds, even if zero
		[transformed appendFormat:@"%u%@", seconds,
NSLocalizedString(@" seconds")];
	}

	return transformed;
}
@end

Vielen Dank fuer dein Helfen, Uli!

Topher

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Equivalent of isKindOfClass for Foundation Types? (From: Uli Kusterer <email@hidden>)

  • Prev by Date: Job opportunity in South Florida
  • Next by Date: NSTextView setNeedsDisplayInRect:avoidAdditionalLayout:
  • Previous by thread: Re: Equivalent of isKindOfClass for Foundation Types?
  • Next by thread: Re: Equivalent of isKindOfClass for Foundation Types?
  • Index(es):
    • Date
    • Thread