• 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: Subclassing NSDateFormatter problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subclassing NSDateFormatter problem


  • Subject: Re: Subclassing NSDateFormatter problem
  • From: Luca Torella <email@hidden>
  • Date: Mon, 20 Sep 2004 18:00:59 +0200


On 20 Sep 2004, at 17:44, Luca Torella wrote:

Hi,
I subclassed NSDateFormatter since I had the following problem. I've got a table view where an user can insert in one specific column time. Not clock time but duration time. For example 1:12:13 means 1 hr 12 min and 13 sec. And until that no problems since I could use a simply nsdateformatter instance. But I'd like that when the user digit 1:23 it means 0:1:23, not 1:23:0 like the normal formatter does.

I've attached the code of the subclass. When I run the code I take the following error:

-[NSCalendarDate length]: selector not recognized

Do you know where's the problem?

[snip]
- (NSString *)stringForObjectValue:(id)anObject
{
if (![anObject isKindOfClass:[NSCalendarDate class]]) {
return nil;
}
return anObject;
}
returning an NSCalendarDate when you should be returning an NSString probably isn't a good idea.

I think I must return an NSCalendarDate.
I tried to return an NSString and all is ok (no more that problem), but my class doesn't recognized the date if I do that.
I've a typical table view wich contains instances of a class I had created. The column time answer to the accessor method -setTime which is:

- (void)setTime:(NSCalendarDate *)s
{
[s retain];
[time release];
time = s;
}


Since the accessor method needs an NSCalendarDate i have to return a calendar date object. Returning an NSString the time variable will be empty.


See also this example that I found on the net. It returns an NSNumber not an NSString

<x-tad-bigger>- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
errorDescription:(NSString **)error
{
float floatResult;
NSScanner *scanner;
BOOL retval = NO;
NSString *err = nil;

scanner = [NSScanner scannerWithString:string];
if ([string hasPrefix:@"$"]) [scanner setScanLocation:1];
if ([scanner scanFloat:&floatResult]
&& ([scanner scanLocation] == [string length] )) {
if (obj) {
</x-tad-bigger>
<x-tad-bigger>
</x-tad-bigger><x-tad-bigger> *obj = [NSNumber numberWithFloat:floatResult];
retval = YES;
}
else {
err = @"Couldn't convert to float";
}
}
if (error) {
*error = err;
}
return retval;
}
</x-tad-bigger>
<x-tad-bigger>
</x-tad-bigger>
<x-tad-bigger>- (NSString *)stringForObjectValue:(id)anObject
{
if (![anObject isKindOfClass:[NSNumber class]]) {
return nil;
}
return [NSString stringWithFormat:@"$%.2f", [anObject
floatValue]];
}

</x-tad-bigger>

Best,
Luca.
 _______________________________________________
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

  • Follow-Ups:
    • Re: Subclassing NSDateFormatter problem
      • From: Frederick Cheung <email@hidden>
References: 
 >Subclassing NSDateFormatter problem (From: Luca Torella <email@hidden>)
 >Re: Subclassing NSDateFormatter problem (From: Frederick Cheung <email@hidden>)

  • Prev by Date: Re: Subclassing NSDateFormatter problem
  • Next by Date: Re: Bug in NSTextField?
  • Previous by thread: Re: Subclassing NSDateFormatter problem
  • Next by thread: Re: Subclassing NSDateFormatter problem
  • Index(es):
    • Date
    • Thread