Compare NSNumber with an int

  • How can I compare an NSNumber with an int?

    The first 2 methods return me errors

    with:
    if(Number == 10)

    with:
    if([Number isEqualToNumber]: 10)

    or with??
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Monday, December 15, 2003, at 11:50 AM, Roberto wrote:

    > How can I compare an NSNumber with an int?
    >
    > The first 2 methods return me errors
    >
    > with:
    > if(Number == 10)
    >
    > with:
    > if([Number isEqualToNumber]: 10)
    >
    > or with??

    With a look at the documentation.

    if ([tNumber intValue]==10)
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • I'm getting it from heart, but it should be:

    if([Number intValue] == 10)

    or

    if([Number isEqualToNumber:[NSNumber numberWithInt:10]])

    The first solution is preferable as it doesn't create a spurious object.

    On 15 Dec 2003, at 11:50, Roberto wrote:

    > How can I compare an NSNumber with an int?
    >
    > The first 2 methods return me errors
    >
    > with:
    > if(Number == 10)
    >
    > with:
    > if([Number isEqualToNumber]: 10)
    >
    > or with??
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • At 12:09 Uhr +0100 15.12.2003, Sscar Morales Vivs wrote:
    > I'm getting it from heart, but it should be:
    >
    > if([Number intValue] == 10)
    >
    > or
    >
    > if([Number isEqualToNumber:[NSNumber numberWithInt:10]])
    >
    > The first solution is preferable as it doesn't create a spurious object.

    The second, however, will also work if your NSNumber in Number
    doesn't contain an integer.
    --
    Cheers,
    M. Uli Kusterer
    ------------------------------------------------------------
            "The Witnesses of TeachText are everywhere..."
                        http://www.zathras.de
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • M. Uli Kusterer wrote:

    > At 12:09 Uhr +0100 15.12.2003, Sscar Morales Vivs wrote:
    >> I'm getting it from heart, but it should be:
    >>
    >> if([Number intValue] == 10)
    >>
    >> or
    >>
    >> if([Number isEqualToNumber:[NSNumber numberWithInt:10]])
    >>
    >> The first solution is preferable as it doesn't create a spurious
    >> object.
    >
    > The second, however, will also work if your NSNumber in Number
    > doesn't contain an integer.

    The docs for intValue say "Returns the receiver's value as an int,
    converting it as necessary.", which suggests the former should work just
    as well (but with less overhead).

    Just
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Am 15.12.2003 um 15:38 schrieb Just van Rossum:
    >
    > The docs for intValue say "Returns the receiver's value as an int,
    > converting it as necessary.", which suggests the former should work
    > just
    > as well (but with less overhead).
    >

    That depends, if you want 10.2 and 10 to be equal or not

    [[NSNumber numberWithInt:10] isEqual:[NSNumber numberWithDouble:10.2]]
    yields NO

    [[NSNumber numberWithInt:10] intValue] == [[NSNumber
    numberWithDouble:10.2] intValue]  yields YES

    Ciao
    Nat!

    I act professionally, when I get paid for it. - Unknown
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month december 2003 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today