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

  • SEARCH
  • Home
  • 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 7401855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:35:16+00:00 2026-05-29T04:35:16+00:00

Possible Duplicate: Understanding NSString comparison in Objective-C Was just reading up about equality vs

  • 0

Possible Duplicate:
Understanding NSString comparison in Objective-C

Was just reading up about equality vs identity and I realized that I’ve been using some equal signs when comparing strings in my objc code. The weird thing is that it actually works from times to times and I was wondering why.

http://www.karlkraft.com/index.php/2008/01/07/equality-vs-identity/

I have two pieces of code, one work and one doesn’t.

WORKING. Here I have a object called ‘Category’ which has a NSString property called ‘name’.

@property (nonatomic, retain) NSString *name;

I then have a function where I pass in a ‘Catergory’ pointer and when I use the equal sign here, it works perfectly.

 -(void)addCategoryToStorage:(Category*)newcategory {
      if(newcategory.name != @"All") {  // this works

NON WORKING. Here I use the output from a NSFileManager call;

NSArray *dirContent = [self.fileManager 
                       contentsOfDirectoryAtPath:@"MyFiles"
                       error:nil];

I then try to compare on of the NSStrings in the array:

for(int i = 0; i < [dirContent count]; i++) { 
    if([dirContent objectAtIndex:i] == @"MyFile") // This will never be True

So my questions is, what would be the reason why these two NSString pointers would work so differently? I know I shouldn’t use “==” but at the moment, but I’m just so curious why it works sometimes.

I noticed some difference between them debugging. In the first case, the “newcategory”s “name” said (__NSCFConstantString*) and had the value visible.
For some reason, the “dirContent” arrays contains two (__NSString*) objects where the values aren’t visible.

This raises another question. What makes the NSString in the first case to become a constant? It seems to have something to with how it’s created when I pass it to the function. When I allocate a new object with a NSString property that I then pass in to a function, the function sees it a constant(The NSString property of the object). But when the object is stored in a list and I grab it by passing [array objectAtIndex:index] into a function, the functions doesn’t get it as a constant (the NSString property of the object that is).

Thanks!

  • 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-29T04:35:18+00:00Added an answer on May 29, 2026 at 4:35 am

    It may work, when the pointers point to the same object.

    If you have a string literal in your code, it is actually a statically allocated object of type NSString. At any point in your code, if you would reference this string value, you are actually using the same pointer. (A pointer is basically a memory address)

    If you have the string literal @"abc" at more than one point in your code, the compiler only puts in one instance, and uses a pointer to this single instance.

    This explains why @"abc" == @"abc".

    Taking your example:

    if(newcategory.name != @"All")
    

    will only work if at some point in your code you did:

    newcategory.name = @"All";
    

    And will not work if you would do:

    newcategory.name = [NSString stringWithFormat:@"%c%c%c",'A','l','l'];
    

    because in that last case you explicitly allocate a new string.

    edit just tested, this example was flawed:

    newcategory.name = [NSString stringWithString:@"All"];
    

    because this is optimized away so you will have the same pointer to @"All"

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

Sidebar

Related Questions

Possible Duplicate: Understanding Threads I have a public class that is implementing a Runnable
Possible Duplicate: Objective-C - When to use 'self' I am having problem understanding the
Possible Duplicate: Understanding Python decorators I was reading a django app source code where
Possible Duplicate: Understanding PHP's & operator I was just looking at array_filter() function doc
Possible Duplicate: Understanding @Protocols in Objective-C Could somebody explain what a protocol is in
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Understanding Enums in Java Why should we use enums rather Java constants?
Possible Duplicate: C header file loops Original Question: I always had problems understanding why
Possible Duplicate: Using ZXing to create an android barcode scanning app I know it

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.