tags:

views:

2545

answers:

2

I am working through the Stanford iPhone class and I can't figure out why I am getting a compiler warning. I assume I need to cast my object to NSString, but I get an error when I try to do so. The code runs and gives me the expected output, but the warning bothers me.

NSLog(@"lowerCaseString is: %@", [object lowercaseString]);

This runs with the warning: 'NSObject' may not respond to '-lowerCaseString'

NSLog(@"lowerCaseString is: %@", [(NSString)object lowercaseString]);

This throws an error: conversion to non-scalar type requested

+5  A: 

Hi Joel, I believe this will do what you need:

NSLog(@"lowerCaseString is: %@", [(NSString *)object lowercaseString]);

Note I just added a * to your second line of code to make a pointer to NSString. Hope it helps!

Adam

Adam Alexander
Thanks Adam, I have all my books and docs open but couldn't find this single character answer anywhere. Cheers.
Joel Hooks
+2  A: 

Why is object declared as an NSObject if it's supposed to be an NSString? If you intend to call NSString methods on it, declare it as an NSString or leave it as an id. Then you won't get errors.

Chuck
It is a NSMutableArray of random objects that needs to be parsed. Output occurs when it encounters NSString objects. It ran without errors, but I like to eliminate the warnings too.
Joel Hooks
Having the variable typed id rather than NSObject * would be better. That's what id is for -- when you don't know the type of the object. Take a look at http://stackoverflow.com/questions/466777 if you want more info.
Chuck
+1 for promoting the use of id over NSObject*
Abizern
Thanks Chuck, I am going to give that a try now and see how it works out. The link was very helpful. Cheers.
Joel Hooks
I was using: for( id *object in myObjects) After removing the '*' all the warnings went away. I sort of get what the '*' does now, but not fully. Lots of study in my future. Thanks again for the help guys.
Joel Hooks