Home

NSNotFound

NSNotFound is a constant in Apple's Foundation and related Cocoa frameworks used as a sentinel value to indicate that a requested item or index could not be found. It is most commonly associated with methods that return a numeric location, such as NSString rangeOfString: or rangeOfCharacterFromSet:, where a location equal to NSNotFound signals that the search term was not found.

Value and type: NSNotFound is defined as NSIntegerMax, the maximum value for the NSInteger type. On 64-bit

Practical usage: In Objective-C, a common pattern is to perform a search and then check the location

Notes: NSNotFound represents a not-found condition and is not a valid index. Not every API uses NSNotFound;

platforms
this
corresponds
to
9223372036854775807,
and
on
32-bit
platforms
it
is
2147483647.
Although
location
values
in
many
APIs
are
unsigned,
the
sentinel
uses
the
signed
NSIntegerMax,
so
comparisons
typically
test
for
r.location
==
NSNotFound
rather
than
-1.
field
against
NSNotFound
to
determine
whether
the
search
succeeded.
For
example,
NSString
*s
=
...;
NSRange
r
=
[s
rangeOfString:@"foo"];
if
(r.location
==
NSNotFound)
{
/*
not
found
*/
}.
When
bridging
to
Swift,
optionals
are
often
used
instead
of
NSNotFound,
though
some
older
APIs
may
still
expose
the
sentinel
value.
some
return
error
codes
or
different
indicators.
Developers
should
consult
the
specific
API
documentation
to
understand
whether
and
how
NSNotFound
is
used
as
a
sentinel
value
in
that
context.