Can you set an NSInteger as NULL in Objective-C?
Asked Answered
G

1

8

I am trying to set an NSInteger to NULL. Though the compiler does not give any error, but I am not sure if this is the right way to do it. Can you set an NSInteger as NULL in ios..? Or is it forbidden for some reason..? Or Should I set it to Nil..? Which is better practice..?

Thanks

Gerhart answered 15/5, 2014 at 14:27 Comment(6)
NULL is just 0 which is why there is no issue.Natalya
nshipster.com/nilKep
Setting it to NULL suggests it's a pointer. If it isn't a pointer I would just set it to 0.Maceio
Agree with the above comments, besides what would NSInteger = NULL mean? What are you trying to do?Billmyre
@Natalya It will still generate a warning.Chlorous
@Chlorous NSInteger x = NULL; does not produce any warning. I'm not saying it is the right thing to do. But it shows no warning.Natalya
F
10

I think you are confusing NSInteger with an Objective-C class.

NSInteger is simply a typedefd integer:

#if __LP64__ || NS_BUILD_32_LIKE_64
  typedef long NSInteger;
  typedef unsigned long NSUInteger;
#else
  typedef int NSInteger;
  typedef unsigned int NSUInteger;
#endif

If you want something that holds a number which can be nil then you probably want NSNumber.

Frizzell answered 15/5, 2014 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.