Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-29-2012, 10:26 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 173
mistergreen2011 is on a distinguished road
Default When a String is not a String?

Ok, I'm really confused with this one.

I have a NSMutable array it looks like this.
Code:
{
    Fe = "0.1";
    name = "DTPA Fe (10%)";
    target = Fe;
    tsp = 4290;
}........
I want to grab the objectKey name and do some comparisons.
Code:
//shared.elements is above
NSArray *temp = [[shared.elements objectAtIndex:i] allKeys];
// temp gives me Fe,name,target,tsp

for(int j=0; j < [temp count]; j++) {
        //making sure it's a string!!!
        if ([[temp objectAtIndex:j] isKindOfClass:[NSString class]]) {
            NSLog(@"yes i'm a string");
        }

        if([(NSString *)[temp objectAtIndex:j] lowercaseString] == @"name") {
            NSLog(@"expletive");
            // nothing
        }

   if([temp objectAtIndex:j] == @"name") {
            NSLog(@"expletive");
            // nothing
        }
       
    }
None of the conditions are true except that it's a confirmation it's a NSString.

confused.
mistergreen2011 is offline   Reply With Quote
Old 01-29-2012, 10:38 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by mistergreen2011 View Post
Ok, I'm really confused with this one.

I have a NSMutable array it looks like this.
Code:
{
    Fe = "0.1";
    name = "DTPA Fe (10%)";
    target = Fe;
    tsp = 4290;
}........
I want to grab the objectKey name and do some comparisons.
Code:
//shared.elements is above
NSArray *temp = [[shared.elements objectAtIndex:i] allKeys];
// temp gives me Fe,name,target,tsp

for(int j=0; j < [temp count]; j++) {
        //making sure it's a string!!!
        if ([[temp objectAtIndex:j] isKindOfClass:[NSString class]]) {
            NSLog(@"yes i'm a string");
        }

        if([(NSString *)[temp objectAtIndex:j] lowercaseString] == @"name") {
            NSLog(@"expletive");
            // nothing
        }

   if([temp objectAtIndex:j] == @"name") {
            NSLog(@"expletive");
            // nothing
        }
       
    }
None of the conditions are true except that it's a confirmation it's a NSString.

confused.

You can't use == to compare objects. (NSStrings are objects).

In Objective C, a variable that refers to an instance of an object is actually a pointer.

So when you say

Code:
MyObject *anObject = [MyObject alloc] init];
the "anObject" variable is actually a pointer to the memory address of an object of type MyObject.

When you use == to compare 2 objects, you are actually comparing 2 pointer variables. The only time

Code:
if (objectA == objectB)
will evaluate as TRUE is if the variables objectA and objectB point to the same address.

To compare strings, use code like this:

Code:
if ([string1 isEqualToString: @"string"])
{
  //strings are equal, do something
}

Note that you have the same problem in trying to compare 2 C strings.

If you have code like this

Code:
char *aString = "foo";

char *bString = "foo";

if (aString == bString) 
{
  //won't work
}
Will not work, because the if statement compares the addresses of aString and bString,
not the string contents.


To compare 2 C strings, you use the strcmp function:

Code:
char *aString = "foo";

char *bString = "foo";

if (strcmp(aString,bString))
{
  //C strings are equal
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-29-2012, 10:38 PM   #3 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

To compare strings, you need to use isEqualToString:. "==" compares memory addresses. isEqualToString will compare the actual values of the strings.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 01-29-2012, 10:49 PM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 173
mistergreen2011 is on a distinguished road
Default

ah! thanks folks.
mistergreen2011 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 418
17 members and 401 guests
Atatator, chiataytuday, condor304, dre, FrankWeller, imac74, ipodphone, kukat, LunarMoon, MAMN84, mraalex, n00b, PowerGoofy, QuantumDoja, Retouchable, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:01 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0