• 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
How to add a -(unsigned long long)unsignedLongLongValue method to NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to add a -(unsigned long long)unsignedLongLongValue method to NSString


  • Subject: How to add a -(unsigned long long)unsignedLongLongValue method to NSString
  • From: email@hidden
  • Date: Sun, 11 Jan 2009 07:24:53 +0100 (CET)
  • Importance: Normal

         Hello all,


   I have a Cocoa app that performs some computations on
large integers (but still in the "unsigned long long" range), some
of which are entered by the user in a NSTextField.

  The problem , of course, is that NSControl has no
 -(unsigned long long)unsignedLongLongValue method, and
as specified in the documentation, if the user writes too many digits
the -(int)intValue method just returns UINT_MAX.

   It seems logical to deal with this problem by calling the
-(NSString*)stringValue method of NSControl, and implementing
a  -(unsigned long long)unsignedLongLongValue method for
NSString.

   This is what I did in my project, using the code below. I can't
help feeling that this code is dirty because it relies on the character
for the "j" digit being indexed as unichar number j+48. Can anyone
tell me what would be the clean way to do this ?

                                                                              TIA,

                                                                                     Ewan

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* contents of NSString_UnsignedLongLongValue.h */

extern int const GGCharacterIsNotADigit;

@interface NSString (UnsignedLongLongValue)
     +(unsigned)digitValue: (unichar) c;
      -(unsigned long long)unsignedLongLongValue;
@end

/* contents of NSString_UnsignedLongLongValue.m */

@implementation NSString (UnsignedLongLongValue)

int const GGCharacterIsNotADigit=10;

+(unsigned)digitValue: (unichar) c
{

   if ((c>47)&&(c<58)) {
        return (c-48);
   }
   return GGCharacterIsNotADigit;

+(unsigned long long)unsignedLongLongValue
{
    unsigned n=[self length];
	unsigned long long v,a;
	unsigned small_a,j;
	v=0;
	for (j=0;j<n;j++) {
	      unichar c=[self characterAtIndex:j];
	      small_a=[self digitValue:c];
		  if (small_a==GGCharacterIsNotADigit) continue;
		  a=(unsigned long long)small_a;
		  v=(10*v)+a;
	}
	return v;

}



@end




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to add a -(unsigned long long)unsignedLongLongValue method to NSString
      • From: "Michael Ash" <email@hidden>
    • Re: How to add a -(unsigned long long)unsignedLongLongValue method to NSString
      • From: Ken Thomases <email@hidden>
    • Re: How to add a -(unsigned long long)unsignedLongLongValue method to NSString
      • From: "Stephen J. Butler" <email@hidden>
  • Prev by Date: Re: CAD programming.
  • Next by Date: Re: CAD programming.
  • Previous by thread: Re: Using UIImageView for animations
  • Next by thread: Re: How to add a -(unsigned long long)unsignedLongLongValue method to NSString
  • Index(es):
    • Date
    • Thread