 |
 |
|
 |
07-02-2009, 10:00 AM
|
#1 (permalink)
|
|
New Member
Join Date: Jul 2009
Posts: 3
|
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?
|
|
|
07-02-2009, 10:03 AM
|
#2 (permalink)
|
|
FasterThanMonkeys.com
iPhone Dev SDK Supporter
Join Date: Mar 2009
Location: Southern California
Posts: 521
|
Casting is the right way to go as long as you know you are casting to the right object type.
|
|
|
07-02-2009, 10:41 AM
|
#3 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,569
|
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.
__________________
|
|
|
07-02-2009, 10:44 AM
|
#4 (permalink)
|
|
Former NeXTStep Developer
Join Date: Mar 2009
Posts: 997
|
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
|
|
|
07-02-2009, 11:00 AM
|
#5 (permalink)
|
|
New Member
Join Date: Jul 2009
Posts: 3
|
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.
|
|
|
07-02-2009, 11:09 AM
|
#6 (permalink)
|
|
Former NeXTStep Developer
Join Date: Mar 2009
Posts: 997
|
Quote:
Originally Posted by Jonnebob
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
|
|
|
07-02-2009, 11:19 AM
|
#7 (permalink)
|
|
New Member
Join Date: Jul 2009
Posts: 3
|
Ah yes, I see. My bad.
Thanks for all your help.
|
|
|
07-02-2009, 07:36 PM
|
#8 (permalink)
|
|
at this moment
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 870
|
Quote:
Originally Posted by Jonnebob
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...
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 353 |
| 26 members and 327 guests |
| asabreu, BolderImage, bravetarget, cordoprod, cye, dbyers, DenVog, Eagle11, Ed99, Eskema, hello24, iTrackiGiveaway, Joseph Nardone, kbegemandev, LemonMeringue, mansi5, masc2279, mistergod, mlfarrell, moehac, obiwahn, Rossco, s.newave, vitalys, _nivek |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,212
Threads: 38,996
Posts: 171,042
Top Poster: smasher (2,569)
|
| Welcome to our newest member, wardz |
|