Calling Objective C methods that has variable args

112 views
Skip to first unread message

Hwee-Boon Yar

unread,
Mar 28, 2013, 2:01:00 AM3/28/13
to rubym...@googlegroups.com
What's the proper way to call Objective C methods that has variable args?

E.g. NSString.stringWithFormat

a = ["hello %@", "world"]
NSString.stringWithFormat(a) #crash
NSString.stringWithFormat(*a) #crash
NSString.send('stringWithFormat:', *a) #crash


Thanks.

Hwee-Boon

ecamacho

unread,
Mar 29, 2013, 3:05:24 AM3/29/13
to rubym...@googlegroups.com
Hi,

In those cases you can past the args separated by a comma:

puts NSString.stringWithFormat("hello %@", "world")

That should work

Erick

Hwee-Boon Yar

unread,
Mar 29, 2013, 3:21:10 AM3/29/13
to rubym...@googlegroups.com
Forgot to mention I tried this and it crashed too:

NSString.stringWithFormat("hello %@", "world")

:)

Also not sure if something similar would work with methods that has variadic args in between arguments such as UIActionSheet#initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:

Hwee-Boon

Joffrey Jaffeux

unread,
Mar 30, 2013, 1:44:48 PM3/30/13
to rubym...@googlegroups.com
Hi,

We are currently investigating issues about this : http://hipbyte.myjetbrains.com/youtrack/issue/RM-80

Matt Green

unread,
Mar 30, 2013, 6:39:13 PM3/30/13
to rubym...@googlegroups.com
I never had luck with this, I ran into it using NSPredicate. Settled on using an overload that took an array, rather than varargs.

Hwee-Boon Yar

unread,
Mar 31, 2013, 5:23:27 AM3/31/13
to rubym...@googlegroups.com
Thanks Matt.

Joffrey:

Not all ObjC variadic methods has an equivalent that accepts NSArray* though. It'll be a showstopper for some cases. In my case, I'm looking at UIAppearance.appearanceWhenContainedIn: which doesn't have a documented NSArray* equivalent.

Hwee-Boon

Ignacio Piantanida

unread,
Apr 8, 2013, 3:59:27 PM4/8/13
to rubym...@googlegroups.com
Have you found any workaround?

--
Piantanida, Ignacio Julián


2013/3/31 Hwee-Boon Yar <hwee...@gmail.com>

--
 
---
You received this message because you are subscribed to the Google Groups "RubyMotion - Ruby for iOS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubymotion+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hwee-Boon Yar

unread,
Apr 8, 2013, 11:52:51 PM4/8/13
to rubym...@googlegroups.com

Haven't had the chance to work on this yet. But a bunch of extension methods in ObjC should go the job.


Hwee-Boon
http://hboon.com


You received this message because you are subscribed to a topic in the Google Groups "RubyMotion - Ruby for iOS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubymotion/vbEEQE9-pvU/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to rubymotion+...@googlegroups.com.

Hwee-Boon Yar

unread,
Apr 13, 2013, 1:50:39 AM4/13/13
to rubym...@googlegroups.com
In the meantime, till this limitation is fixed, I've added https://github.com/hboon/MOShine which adds UINavigationBar.moAppearanceWhenContainedIn(), taking in a list of classes.

It lets me do:

UINavigationBar.moAppearanceWhenContainedIn([UIImagePickerController]).setBackgroundImage(nil, forBarMetrics:UIBarMetricsDefault)

A similar approach should work for other method/functions with only variadic-args versions.

Hwee-Boon

--
Piantanida, Ignacio Julián


2013/3/31 Hwee-Boon Yar <hwee...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to rubymotion+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "RubyMotion - Ruby for iOS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubymotion/vbEEQE9-pvU/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to rubymotion+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages