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 3432744
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:29:18+00:00 2026-05-18T07:29:18+00:00

I have an int currentFontSize where I wanna compare with NSString stored in an

  • 0

I have an int currentFontSize where I wanna compare with NSString stored in an C array, here is the code.

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    static NSString* fontSizeName[] = 
    {
        @"14",
        @"18",
        @"22",
        @"26",
        @"30",
        @"34",
    };

    int fontSizeSegmentID;
    int currentFontSize = 22;

    NSString *val = [NSString stringWithFormat: @"%i", currentFontSize];
    NSString *val2 = @"22";
    NSLog(@"the current font size is %i", currentFontSize);
    NSLog(@"the current font Value1 is %i", val);
    NSLog(@"the current font Value2 is %i",val2);
    int i;
    for (i = 0; i < 6; i++) {

        NSLog(@"fontSizeNAme:%d",fontSizeName[i]);
        NSLog(@"val:%d",val);

            if (fontSizeName[i] == val) {
                NSLog(@"They are equal");
                fontSizeSegmentID = i;
                break;
            }

            NSLog(@"the fontSizeSegmentID is: %i" , fontSizeSegmentID);
        }

    [pool drain];
    return 0;
}

the strage part is , val give me a return value of 1879919632, which make my comparison failed. The strange part is when I hardcode the NSString as val2 = @”22″, the comparison become successful where val2 is: 4240, it’s the same as:

NSLog(@"fontSizeNAme:%d",fontSizeName[i]);

here is my return in NSLog:

2010-11-18 16:56:52.784 testINT[26034:a0f] the current font size is 22
2010-11-18 16:56:52.786 testINT[26034:a0f] the current font Value1 is 1879919632
2010-11-18 16:56:52.787 testINT[26034:a0f] the current font Value2 is 4240
2010-11-18 16:56:52.788 testINT[26034:a0f] fontSizeNAme:4176
2010-11-18 16:56:52.788 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.788 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.789 testINT[26034:a0f] fontSizeNAme:4208
2010-11-18 16:56:52.789 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.790 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.790 testINT[26034:a0f] fontSizeNAme:4240
2010-11-18 16:56:52.790 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.791 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.791 testINT[26034:a0f] fontSizeNAme:4272
2010-11-18 16:56:52.791 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.792 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.792 testINT[26034:a0f] fontSizeNAme:4304
2010-11-18 16:56:52.792 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.793 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.793 testINT[26034:a0f] fontSizeNAme:4336
2010-11-18 16:56:52.794 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.794 testINT[26034:a0f] the fontSizeSegmentID is: 32767

Can anybody kindly explain me why? why I cast the @”22″ NString and the one form int returns a different value????? 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-18T07:29:19+00:00Added an answer on May 18, 2026 at 7:29 am

    In Objective-C, you can’t just compare an int and NSString* which contains a string representation of an integer. To convert between the two, you need to do

    NSString* s=@"11";
    int i=[s intValue];  // i now contains 11
    

    Also, the % specification in NSLog does not specify how you want the object to be interpreted. You need to specify the correct % thing matching with the type of the object. Otherwise it just shows you junk. So, both

    NSLog(@"the current font Value1 is %i", val);
    NSLog(@"the current font Value2 is %i",val2);
    

    are illegal, because val and val2 are NSString*. Instead you need to do

    NSLog(@"the current font Value1 is %@", val);
    NSLog(@"the current font Value2 is %@",val2);
    

    or

    NSLog(@"the current font Value1 is %i", [val intValue]);
    NSLog(@"the current font Value2 is %i",[val2 intValue]);
    

    You can’t compare two NSString*s by == either. You need to use [aString isEqualTo:anotherString].

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

Sidebar

Related Questions

We have int main(int argc, char** argv, char** envc) for the ordinary. but I
Ok here is the problem, I have three NSString s with int values, when
Let's say I have int a() { /* Tons of code ....*/ return someInt;
I have an int and float array of length 220 million (fixed). Now, I
i have two int array of images , i have implemented it in grid
I have an int and float array each of length 220 million (fixed). Now,
I have an int array containing gray scale values from 0-254, i also have
I have an int array which has no elements and I'm trying to check
I have an array, say int a[]={2,0,1,0,1,1,0,2,1,1,1,0,1,0,1}; I need to append each of the
I have some function, int somefunction( //parameters here, let's say int x) { return

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.