Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.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 07-02-2009, 10:00 AM   #1 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 3
Question Casting and Obj-C

Is casting a bad thing? What are peoples thoughts on the matter? Should it be avoided where possible? or a necessary evil?

The reason I ask is that i'm following the Stanford lectures and working my way through the assignments and on the first one they ask you to make an NSDictionary and only print out the values from entries into the dictionary which have a key with a certain prefix.

Thats fine I thought as I wrote my first verision of the for loop:

Code:
for (NSDictionary *entry in bookmarkDict)
		
	{
		if ([entry hasPrefix:@"Stanford"]) 
		{
			NSLog(@"Key: '%@' URL: '%@'", entry, [bookmarkDict objectForKey:entry]);
		}
		
	}
Without using the Cast I get an warning: "warning: 'NSDictionary' may not respond to '-hasPrefix:'"

Whats the problem you may be thinking? A Simple cast and the warning dissappears! Well me being the perfectionist I would quite like my finished solution (and for future reference too) to have no cast, if possible.

So, have I missed a method name somewhere in the documentation or is this a case where a cast is the only solution?
Jonnebob is offline   Reply With Quote
Old 07-02-2009, 10:03 AM   #2 (permalink)
ftm
FasterThanMonkeys.com
iPhone Dev SDK Supporter
 
Join Date: Mar 2009
Location: Southern California
Posts: 521
Send a message via AIM to ftm
Default

Casting is the right way to go as long as you know you are casting to the right object type.
__________________


Website: http://fasterthanmonkeys.com

iScore Baseball Scorekeeper Top baseball scorekeeping app for iPhone
Bug Squash Reached #1 kids game in 15+ countries including USA, now with OpenFeint
Jam Packed! Fun puzzle game for all ages
Jam Packed Christmas Holiday puzzle game!
iScore Basketball Scorekeeper Best basketball scorekeeping application
ftm is offline   Reply With Quote
Old 07-02-2009, 10:41 AM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,569
Default

In this case you already have one cast, but I think it's casting to the wrong class. If bookmarkDict is full of strings, then your loop should be:

Code:
for (NSString *entry in bookmarkDict)
Casting is the way to go in situations like this, when dealing with arrays and dictionaries. The compiler has no idea what type of object is in an array or dictionary, so you need to help it out.

There are other situations where casting would be wrong. I can create and NSString and cast it as a UIView, but that won't make it respond to the right messages for a UIView. It doesn't actually do any "conversion" on the object.
__________________
smasher is offline   Reply With Quote
Old 07-02-2009, 10:44 AM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Change your code to this to get rid of the warning:

Code:
for (id entry in bookmarkDict)
		
	{
		if ([entry hasPrefix:@"Stanford"]) 
		{
			NSLog(@"Key: '%@' URL: '%@'", entry, [bookmarkDict objectForKey:entry]);
		}
		
	}
The problem you have is that the enumerator for iterating through the dictionary is not a (NSDictionary *). It's a generic pointer (id) because you don't actually know what the dictionary entry type is.

joe
FlyingDiver is offline   Reply With Quote
Old 07-02-2009, 11:00 AM   #5 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 3
Default

Thanks for all your replies.

I think part of my problem was that I was coming from java/php background I wasn't used to defining the type of element in the enumerator.

FlyingDiver: changing to id, causes two different warnings.

"invalid reciever type 'id*'"

"passing argument 1 of 'objectForKey:' from incompatible pointer type".

Using NSString in the enumerator doesnt produce any warnings. I think this must be because the the NSDictionary enumerator is looping through the keys which it knows are strings.
Jonnebob is offline   Reply With Quote
Old 07-02-2009, 11:09 AM   #6 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by Jonnebob View Post
Thanks for all your replies.

I think part of my problem was that I was coming from java/php background I wasn't used to defining the type of element in the enumerator.

FlyingDiver: changing to id, causes two different warnings.

"invalid reciever type 'id*'"

"passing argument 1 of 'objectForKey:' from incompatible pointer type".

Using NSString in the enumerator doesnt produce any warnings. I think this must be because the the NSDictionary enumerator is looping through the keys which it knows are strings.
Read the code above carefully. It's "id", not "id *". "id" is already a pointer, you don't add the "*" to it. Using "id" is the method in the Fast Enumerator documentation.

joe
FlyingDiver is offline   Reply With Quote
Old 07-02-2009, 11:19 AM   #7 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 3
Default

Ah yes, I see. My bad.

Thanks for all your help.
Jonnebob is offline   Reply With Quote
Old 07-02-2009, 07:36 PM   #8 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 870
Default

Quote:
Originally Posted by Jonnebob View Post
Using NSString in the enumerator doesnt produce any warnings. I think this must be because the the NSDictionary enumerator is looping through the keys which it knows are strings.
I know you already have a working answer but I want to mention here that writing

Code:
for (NSString *entry in bookmarkDict)
absolutely does NOT only filter out NSString entries! It simply tells the compiler that you want to treat each item as an NSString. If the item in question actually isn't an NSString, horrible bad things can happen...
jsd 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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,212
Threads: 38,996
Posts: 171,042
Top Poster: smasher (2,569)
Welcome to our newest member, wardz
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:08 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0