Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4577900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:24:38+00:00 2026-05-21T20:24:38+00:00

I have an array with a bunch of strings and I want to check

  • 0

I have an array with a bunch of strings and I want to check if a certain string is contained in the array. If I use the containsObject: message on the array, I’m getting correct results. Do all NSString objects with the same string point to the same object? Or why is the containsObject: working?

NSArray *stringArray = [NSArray arrayWithObjects:@"1",@"2",@"3",anotherStringValue, nil];
if([stringArray containsObject:@"2"]){
  //DO SOMETHING
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:24:39+00:00Added an answer on May 21, 2026 at 8:24 pm

    Yes, hard-coded NSStrings (string literals) (that is any @"..." in your source code) are turned into strings that exist indefinitely while your process is running.

    However NSArray‘s containsObject: methods calls isEqual: on its objects, hence even a dynamically created string such as [NSString stringWithFormat:@"%d", 2] would return YES in your sample snippet.
    This is because NSString’s isEqual: (or more precisely its isEqualToString:) method is implemented to be content aware (vs. comparing pointer identities) and thus returns YES for any pair of strings containing the very same sequence of characters (at time of comparison), no matter how and when they were created.

    To check for equal (pointer-)identity you’d have to enumerate your array and compare via

    NSString *yourString = @"foo";
    BOOL identicalStringFound = NO;
    for (NSString *someString in stringArray) {
        if (someString == yourString) {
            identicalStringFound = YES;
            break;
        }
    }
    

    (which you most likely wouldn’t want, though).

    Or in a more convenient fashion:

    BOOL identicalStringFound = [stringArray indexOfObjectIdenticalTo:someString] != NSNotFound;
    

    (you most likely wouldn’t want this one either).


    Summing up:

    So the reason you’re getting a positive reply from containsObject: is NOT because literal strings share the same constant instance, BUT because containsObject: by convention calls isEqual:, which is content aware.

    You might want to read the (short) documentation for isEqual: from the NSObject protocol.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array of strings, String[] data and it's 10 elements has value P
I have an array of strings that I want to extract specific content from:
I have a string with bunch of special characters/symbols and I want to replace
I am trying to query a certain string/word and query a bunch of results
I have a php array that has a bunch of data that I need
I have a bunch of strings representing mathematical functions (which could be nested and
I have 5 different arrays for a bunch of excercises, like so: const oef1:Array
I have a bunch of strings structured like this one Trim(2714,8256)++Trim(10056,26448)++Trim(28248,49165) and what I
I have a bunch of String s I'd like a fast lookup for. Each
I have a bunch of urls like these. $urls = array( 'https://site1.com', 'https://www.site2.com', 'http://www.site3.com',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.