I'm new to Obj-C:
I have completed assignmnet 1B and it works, except that I'm not sure of the
correct way to use selectors so that warning messages are not generated
during compile time.
E.g. I have a mutable array with 3 different object inside. 1 of these
objects does not respond to lowercaseString and the following code is how
I'm determining that:
NSMutableArray *mutArr= [NSMutableArray new];
NSString *str1 = @"string 1";
NSMutableString *str2 = @"string 2";
NSURL *url1 = [NSURL URLWithString:@"http://example.com"];
[mutArr addObject:str1];
[mutArr addObject:str2];
[mutArr addObject:url1];
for(NSObject *el in mutArr){
SEL sel= @selector(lowercaseString);
if([el respondsToSelector:sel){
NSString *str3= [el lowercaseString];
===========================================
WARNING: 'NSObject' may not respond to '-lowercaseString'
===========================================
}
}
So, what is the normal way in Objective-C that one can write code that
itterates accros a list of unknown objects and call methods/send messages to
each in turn were some objects are known not to support the method/message?
It's fine using the respondsToSelector thing, but I would like to know how
to write it so that the compiler won't generate this warning.
Thanks,
> for(NSObject *el in mutArr){
FYI this would generally be written as "for (id el in mutArr)".
> SEL sel= @selector(lowercaseString);
>
> if([el respondsToSelector:sel){
> NSString *str3= [el lowercaseString];
>
> ===========================================
> WARNING: 'NSObject' may not respond to '-lowercaseString'
> ===========================================
>
> }
>
> }
It's because "el" is still an NSObject, and the compiler doesn't have
any reason to expect it to respond to NSString methods. The
"respondsToSelector" check happens at run time, not at compile time, so
its meaning is lost to the compiler.
Make it
NSString *str3 = [(NSString *)el lowercaseString];
And the compiler should be happy.
--
Tom "Tom" Harrington
Independent Mac OS X developer since 2002
http://www.atomicbird.com/
> Regarding: Stanford's CS192P iPhone lecture assignment 1B
>
> I'm new to Obj-C:
>
> I have completed assignmnet 1B and it works, except that I'm not sure of the
> correct way to use selectors so that warning messages are not generated
> during compile time.
>
> E.g. I have a mutable array with 3 different object inside. 1 of these
> objects does not respond to lowercaseString and the following code is how
> I'm determining that:
>
> NSMutableArray *mutArr= [NSMutableArray new];
>
> NSString *str1 = @"string 1";
> NSMutableString *str2 = @"string 2";
> NSURL *url1 = [NSURL URLWithString:@"http://example.com"];
Someone else pointed out the answer to your compiler warning, but you
should also know that you're assigning an immutable string literal to
str2. If you attempt to modify it, you'll raise an exception. The @
prefix before a string creates a constant NSString behind the scenes.
You should assign str2 using NSMutableString's -initWithString: or
+stringWithString: methods so that str2 is given a mutable object. Or,
you can use [@"string 2" mutableCopy]. Since @"" string literals are
NSStrings, you can send them messages like any other object.
"Tom Harrington" <t...@pcisys.no.spam.dammit.net> wrote in message
news:tph-B8BC26.23305903082009@localhost...
NSMutableString *str2= [NSMutableString stringWithString:@"this is my string
2"];
And that works, although I've not attempted to modify the string yet.
"Preston" <pre...@stupid.com> wrote in message
news:2009080410564016807-preston@stupidcom...