coercion

2 views
Skip to first unread message

tim lindner

unread,
Aug 18, 2009, 2:53:02 PM8/18/09
to Programming Nu
I have a question about coercion:

% (set num 10)
10
% (set str "hello")
"hello"
% (str appendString:num)
2009-08-18 11:49:50.845 nush[5207:10b] *** -[NSCFNumber length]:
unrecognized selector sent to instance 0x252450
NSInvalidArgumentException: *** -[NSCFNumber length]: unrecognized
selector sent to instance 0x252450

I changed it to:

% (str appendString:(+ "" num))
()
% str
"hello10"

Is there a better way?

Tim Burks

unread,
Aug 18, 2009, 3:07:32 PM8/18/09
to program...@googlegroups.com

You could instead use (num stringValue)

In your case, the appendString: method is defined in the frameworks
and expects an object that responds to NSString methods. It might be
possible to add other methods to NSNumber to get that to work
automatically, but for now, sending the stringValue message will do
the job.

Tim
Reply all
Reply to author
Forward
0 new messages