Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Objective-C)  »  Xcode, supress nsobject may not respond to...
Page 1 of 1    

Xcode, supress nsobject may not respond to...

Author Message
Joe Butler...
Posted: Tue Aug 04, 2009 5:17 am
Guest
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 = at (no spam) "string 1";
NSMutableString *str2 = at (no spam) "string 2";
NSURL *url1 = [NSURL URLWithString: at (no spam) "http://example.com"];

[mutArr addObject:str1];
[mutArr addObject:str2];
[mutArr addObject:url1];

for(NSObject *el in mutArr){

SEL sel= at (no spam) 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,
 
Tom Harrington...
Posted: Tue Aug 04, 2009 9:31 am
Guest
In article <4a778fa8$0$2475$db0fefd9 at (no spam) news.zen.co.uk>,
"Joe Butler" <ffffh.no.spam at (no spam) hotmail-spammers-paradise.com> wrote:

Quote:
for(NSObject *el in mutArr){

FYI this would generally be written as "for (id el in mutArr)".

Quote:
SEL sel= at (no spam) 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/
 
Preston...
Posted: Tue Aug 04, 2009 8:56 pm
Guest
On 2009-08-03 19:32:45 -0600, "Joe Butler"
<ffffh.no.spam at (no spam) hotmail-spammers-paradise.com> said:

Quote:
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 = at (no spam) "string 1";
NSMutableString *str2 = at (no spam) "string 2";
NSURL *url1 = [NSURL URLWithString: at (no spam) "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 at (no spam)
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 [ at (no spam) "string 2" mutableCopy]. Since at (no spam) "" string literals are
NSStrings, you can send them messages like any other object.
 
Joe Butler...
Posted: Wed Aug 05, 2009 1:42 am
Guest
Yes thanks. That solved it. The humble c-style typecast.
Also checked the for(id el in mutArr) and worked too. Will look into that
further, thanks.

"Tom Harrington" <tph at (no spam) pcisys.no.spam.dammit.net> wrote in message
news:tph-B8BC26.23305903082009 at (no spam) localhost...
Quote:
In article <4a778fa8$0$2475$db0fefd9 at (no spam) news.zen.co.uk>,
"Joe Butler" <ffffh.no.spam at (no spam) hotmail-spammers-paradise.com> wrote:

for(NSObject *el in mutArr){

FYI this would generally be written as "for (id el in mutArr)".

SEL sel= at (no spam) 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/
 
Joe Butler...
Posted: Wed Aug 05, 2009 1:48 am
Guest
Thanks for pointing that out.
Have changed str2 decleration to:

NSMutableString *str2= [NSMutableString stringWithString: at (no spam) "this is my string
2"];

And that works, although I've not attempted to modify the string yet.


"Preston" <preston at (no spam) stupid.com> wrote in message
news:2009080410564016807-preston at (no spam) stupidcom...
Quote:
On 2009-08-03 19:32:45 -0600, "Joe Butler"
ffffh.no.spam at (no spam) hotmail-spammers-paradise.com> said:

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 = at (no spam) "string 1";
NSMutableString *str2 = at (no spam) "string 2";
NSURL *url1 = [NSURL URLWithString: at (no spam) "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 at (no spam)
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 [ at (no spam) "string 2" mutableCopy]. Since at (no spam) "" string literals are
NSStrings, you can send them messages like any other object.
 
 
Page 1 of 1    
All times are GMT
The time now is Wed Dec 02, 2009 10:24 pm