• 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: Comparing results of @encode
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Comparing results of @encode


  • Subject: Re: Comparing results of @encode
  • From: "Michael Ash" <email@hidden>
  • Date: Sun, 26 Oct 2008 12:28:17 -0400

On Sun, Oct 26, 2008 at 7:45 AM, Graham Cox <email@hidden> wrote:
> I'm porting some older code to my project. In this code, a variety of data
> types (integer, string, etc) are enumerated in a simple enumeration. I'd
> like to be able to automatically figure out what data type I need to use
> from a property's type or, since many properties are encapsulated in a
> NSNumber, for the -objCType returned by this.
>
> My question is does @encode(...) return a static string for a given type, or
> not? Basically I'm wondering if I can do this:
>
> if([myNumber objCType] == @encode(float)){...}
>
> Or is it necessary to do a full string comparison? Performance could be an
> issue here, so I'd like to avoid a string compare if I don't need to do it.

For this particular case, it would be roughly just as fast and more
reliable to do something like:

if([myNumber objCType][0] == 'f')

The type encoding characters are documented here:

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_13_section_9.html

Also note that NSNumber is smart enough to automatically convert its
internal representation to whatever you ask for. So for example if
'double' encompasses every value you might need, then it's safe to
simply use [myNumber doubleValue] even if you created it as an int or
float value.

Mike
_______________________________________________

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: Comparing results of @encode
      • From: Graham Cox <email@hidden>
References: 
 >Comparing results of @encode (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: CFPreferences and init.
  • Next by Date: Finding out that a volume is going away before it does
  • Previous by thread: Re: Comparing results of @encode
  • Next by thread: Re: Comparing results of @encode
  • Index(es):
    • Date
    • Thread