Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 309 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NULL vs nil in Objective-C

#1
In `observeValueForKeyPath:ofObject:change:context:` - why do the docs use `NULL` instead of `nil` when not specifying a context pointer?
Reply

#2
They're technically the same thing (0), but nil is usually used for an Objective-C object type, while NULL is used for c-style pointers (void *).
Reply

#3
They're technically the same thing and differ only in style:

- Objective-C style says `nil` is what to use for the `id` type (and pointers to objects).
- C style says that `NULL` is what you use for `void *`.
- C++ style typically says that you should just use `0`.

I typically use the variant that matches the language where the type is *declared*.
Reply

#4
`nil` should only be used in place of an `id`, what we Java and C++ programmers would think of as a pointer to an object. Use `NULL` for non-object pointers.

Look at the declaration of that method:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context

Context is a `void *` (ie a C-style pointer), so you'd definitely use `NULL` (which is sometimes declared as `(void *)0`) rather than `nil` (which is of type `id`).
Reply

#5
`NULL` is the **`C equivalent`** of `nil`, a pointer to nothing;

where **`nil is zero typed as id`**,

**`NULL is zero typed as void*`**.

**One important point** you can’t send a message to NULL. So it is preferred to use nil in objective-C at many places.
Reply

#6
They almost are the same thing except,

`nil` is used in an Objective-C style.
where `NULL` is for C type pointers and is typdef'ed to `(void *)`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through