How to use an API that returns a string by reference?

45 views
Skip to first unread message

Ashley Ward

unread,
Dec 1, 2013, 6:51:24 PM12/1/13
to jstal...@googlegroups.com
Hi all...

I'm having a bit of fun playing with JSTalk (using 1.0.1 downloaded from jstalk.org), trying to write a text-meddling script which I would normally throw together in awk (!).

I'd like to use -[NSScanner scanUpToString:intoString:], but this returns a string by reference, and I don't see how to get that string into a variable in JSTalk.  Is this possible?

The relevant NSScanner API is:
  - (BOOL)scanUpToString:(NSString *)stopString intoString:(NSString **)stringValue

I've tried the following, but this crashes JSTalk Editor...
  var inputContent = [NSString stringWithContentsOfFile:inputPath encoding:NSUTF8StringEncoding error:nil];

  var scanner = [NSScanner scannerWithString:inputContent];
  var input = @"";
  var ret = [scanner scanUpToString:@"," intoString:input];

  print("ret = " + ret);
  print("input = " + input);

Using the & operator like the following gives "Unexpected token '&'":
  var ret = [scanner scanUpToString:@"," intoString:&input];

Is there a suitable magic incantation?

Best wishes,

Ashley.

Gus Mueller

unread,
Dec 4, 2013, 1:08:40 AM12/4/13
to jstal...@googlegroups.com
On Dec 1, 2013, at 3:51 PM, Ashley Ward <ash...@thewards.org.uk> wrote:

I'd like to use -[NSScanner scanUpToString:intoString:], but this returns a string by reference, and I don't see how to get that string into a variable in JSTalk.  Is this possible?

There’s no good way to do this right now, sorry.

-gus

Ashley Ward

unread,
Dec 5, 2013, 4:44:42 PM12/5/13
to jstal...@googlegroups.com
Thanks for the reply, Gus.

For the record, I found a work around for this particular case:

var scanner = [NSScanner scannerWithString:inputContent];

var ret = [scanner scanUpToString:@"," intoString:nil];

var input = [inputContent substringWithRange:NSMakeRange(0, [scanner scanLocation])];


Ashley.

Reply all
Reply to author
Forward
0 new messages