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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 03-21-2010, 12:28 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 1
Default Referencing an Object within an Array

Hello!

I've been trying to figure this problem out for the latter half of the day, and I'm just plain stumped!

Here's the code:

-(IBAction)checkWord : (id)sender
{
NSArray *array = [[NSArray alloc] initWithObjects: @"BIKE", @"BUS", @"BILL", nil];

self.wordList = array;

[array release];

if([wordList containsObject: theWord])
{
NSString *dummyText = [[NSString alloc] initWithFormat:@"%@ is a real word.", theWord];
checkText.text = dummyText;
[dummyText release];
}
else{

NSString *dummyText = [[NSString alloc] initWithFormat:@"%@ is not a real word.", theWord];
checkText.text = dummyText;
NSLog(@"NOT A WORD");
[dummyText release];
}


}


The variable "theWord" equals BIKE, in this instance, and it's meant to be compared to the array so that the "if" statement executes.

"checkText" is a label on IB, it is currently empty, but should display the text within the "if" statement.

"wordList" is another array already defined in the header file.

I've looked at my header file, but everything is as it should be.

Thank you for your help in advance!

-SS
sunnystormy is offline   Reply With Quote
Old 03-21-2010, 03:21 AM   #2 (permalink)
Dr. Touch Cocoa Helpdesk
iPhone Dev SDK Supporter
 
Join Date: Sep 2008
Location: Vienna, Austria
Posts: 537
Send a message via AIM to Oliver Drobnik Send a message via MSN to Oliver Drobnik Send a message via Skype™ to Oliver Drobnik
Default

Quote:
Originally Posted by sunnystormy View Post
Hello!

I've been trying to figure this problem out for the latter half of the day, and I'm just plain stumped!

Here's the code:

-(IBAction)checkWord : (id)sender
{
NSArray *array = [[NSArray alloc] initWithObjects: @"BIKE", @"BUS", @"BILL", nil];

self.wordList = array;

[array release];

if([wordList containsObject: theWord])
{
NSString *dummyText = [[NSString alloc] initWithFormat:@"%@ is a real word.", theWord];
checkText.text = dummyText;
[dummyText release];
}
else{

NSString *dummyText = [[NSString alloc] initWithFormat:@"%@ is not a real word.", theWord];
checkText.text = dummyText;
NSLog(@"NOT A WORD");
[dummyText release];
}


}


The variable "theWord" equals BIKE, in this instance, and it's meant to be compared to the array so that the "if" statement executes.

"checkText" is a label on IB, it is currently empty, but should display the text within the "if" statement.

"wordList" is another array already defined in the header file.

I've looked at my header file, but everything is as it should be.

Thank you for your help in advance!

-SS

Your approach cannot work because containsObject checks if the string object ITSELF is contained (i.e. the same pointer). You can add multiple identical string objects to an array.

If you were to use an NSSet instead then you would be more in luck because NSSet differentiates objects by hash and thus would be able to respond with YES to a containsObject there.
__________________
regards

Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.

Linguan – makes localizing strings file fun!

Cocoanetics Parts Store – easy to use yet professionally looking components that you can use to spruce up your own apps. Augmented Reality, Calendar Control, Pin Lock or Purchase Button are only some examples. You get full source code, no static library crap, and lifetime support. Check it out today!
Oliver Drobnik is offline   Reply With Quote
Old 03-21-2010, 06:07 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

In my experience, containsObjects works with different pointers as long as the strings have the same letters and words.
For example:
Code:
NSString * aString = @"some words";
NSString * anotherString = @"some words";

NSArray * anArray = [NSArray arrayWithObject:aString];
if ([anArray containsObject:anotherString]){
    NSLog(@"yep");
}
If you run that you will get the NSLog.
harrytheshark is offline   Reply With Quote
Old 03-21-2010, 06:24 AM   #4 (permalink)
Dr. Touch Cocoa Helpdesk
iPhone Dev SDK Supporter
 
Join Date: Sep 2008
Location: Vienna, Austria
Posts: 537
Send a message via AIM to Oliver Drobnik Send a message via MSN to Oliver Drobnik Send a message via Skype™ to Oliver Drobnik
Default

Sorry, but that works for ŕ different reason than you think.

The Compiler optimizes NSString literals to use the same instance.

So containsObject works because it really IS the same object.
__________________
regards

Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.

Linguan – makes localizing strings file fun!

Cocoanetics Parts Store – easy to use yet professionally looking components that you can use to spruce up your own apps. Augmented Reality, Calendar Control, Pin Lock or Purchase Button are only some examples. You get full source code, no static library crap, and lifetime support. Check it out today!
Oliver Drobnik is offline   Reply With Quote
Old 03-21-2010, 06:28 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Probably not the best example then, but in my experience, it will work however you create the NSString.

The way sunnystormy is doing it should be working.

EDIT: Replicating your code exactly gives the desired results. You might want to check that "theWord" has not been autoreleased.

Last edited by harrytheshark; 03-21-2010 at 06:51 AM.
harrytheshark is offline   Reply With Quote
Old 03-21-2010, 11:39 AM   #6 (permalink)
Dr. Touch Cocoa Helpdesk
iPhone Dev SDK Supporter
 
Join Date: Sep 2008
Location: Vienna, Austria
Posts: 537
Send a message via AIM to Oliver Drobnik Send a message via MSN to Oliver Drobnik Send a message via Skype™ to Oliver Drobnik
Default

Quote:
Originally Posted by harrytheshark View Post
Probably not the best example then, but in my experience, it will work however you create the NSString.

The way sunnystormy is doing it should be working.

EDIT: Replicating your code exactly gives the desired results. You might want to check that "theWord" has not been autoreleased.
Harry, you are right. RTFM'ing, I found this:

Quote:
This method determines whether anObject is present in the receiver by sending an isEqual: message to each of the receiver’s objects (and passing anObject as the parameter to each isEqual: message).
__________________
regards

Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.

Linguan – makes localizing strings file fun!

Cocoanetics Parts Store – easy to use yet professionally looking components that you can use to spruce up your own apps. Augmented Reality, Calendar Control, Pin Lock or Purchase Button are only some examples. You get full source code, no static library crap, and lifetime support. Check it out today!
Oliver Drobnik is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, objects

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: 275
20 members and 255 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, dre, glenn_sayers, HDshot, HemiMG, JasonR, karlam963, nobre84, prchn4christ, Rudy, spiderguy84, themathminister, tomtom100, viniciusdamone, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

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