Creating NSStrings in RubyCocoa
Creating NSStrings in RubyCocoa
- Subject: Creating NSStrings in RubyCocoa
- From: Knud Hinnerk Möller <email@hidden>
- Date: Thu, 17 Jan 2008 18:30:39 +0000
Hi,
I'm new to RubyCocoa, so this question may seem dumb. I know that, for
some of the fundamental types, RubyCocoa will convert instances of
ruby classes into instances of the corresponding cocoa classes when
you pass them to an Obj-C method. E.g., a ruby string would be
converted into an NSString. However, what is the proper way of
generating an NSString (or similar 'basic' objecs) from scratch? I had
this code:
desktopPath = "~/Desktop".stringByExpandingTildeInPath
This gave me an exception, complaining that
stringByExpandingTildeInPath is not a method of class "String":
undefined method `stringByExpandingTildeInPath' for "~/
Desktop":String (NoMethodError)
So, apparently, I had created a ruby string and not an NSString. After
some searching, I found that there is supposedly a method "to_ns" that
will convert ruby objects into corresponding cocoa objects. However,
desktopPath = "~/Desktop".to_ns.stringByExpandingTildeInPath
just gave this error:
undefined method `to_ns' for "~/Desktop":String (NoMethodError)
The only solution I found was using the cocoa constructor methods for
explicitly creating an NSString:
desktopPath = OSX::NSString.alloc.initWithString("~/
Desktop").stringByExpandingTildeInPath
This, however, looks rather ugly and complicated, especially for
something as simple as creating a string object. Is there a better
solution to doing this?
Cheers,
Knud
-------------------------------------------------
Knud Möller, MA
+353 - 91 - 495086
Smile Group: http://smile.deri.ie
Digital Enterprise Research Institute
National University of Ireland, Galway
Institiúid Taighde na Fiontraíochta Digití
Ollscoil na hÉireann, Gaillimh
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden