Swift style initializers

81 views
Skip to first unread message

Doug

unread,
Feb 10, 2015, 12:33:12 PM2/10/15
to rubym...@googlegroups.com
Hey.

So this isn’t particularly robust, but I’ve been tinkering and was wondering whether something similar to this could actually be included within a gem or maybe RubyMotion itself?

This means that
label = UILabel.alloc.initWithFrame([[50, 100], [100, 30]])
color
= UIColor.alloc.initWithRed(0.5, green: 0.5, blue: 0.5, alpha: 0.9)

becomes
label = UILabel.new(frame: [[50, 100], [100, 30]])
color
= UIColor.new(red:0.5, green: 0.5, blue: 0.5, alpha: 0.9)

Cheers

Eric Henderson

unread,
Feb 13, 2015, 12:01:00 PM2/13/15
to rubym...@googlegroups.com
Cool idea.  I just added a suggestion to the gist.

Eric Henderson

--
You received this message because you are subscribed to the Google Groups "RubyMotion - Ruby for iOS, OS X, and Android" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubymotion+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubymotion/baeddcfa-6452-44dd-bcb4-7d6630ffa85d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Doug

unread,
Feb 14, 2015, 6:05:54 PM2/14/15
to rubym...@googlegroups.com
Thanks. I’ve updated the gist following yours and Colin’s comments, and renamed the method to self.with. This means the examples above become
label = UILabel.with(frame: [[50, 100], [100, 30]])
color
= UIColor.with(red:0.5, green: 0.5, blue: 0.5, alpha: 0.9)

This actually reads fairly well to me. Although the less ruby style nature of it might defeat the point of making such a helper.

Cheers

Doug

Colin T.A. Gray

unread,
Feb 14, 2015, 7:38:21 PM2/14/15
to rubym...@googlegroups.com
Good idea! Avoiding 'new' in this case is a good idea. And you get a gem name for free! 😉

--
You received this message because you are subscribed to the Google Groups "RubyMotion - Ruby for iOS, OS X, and Android" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubymotion+...@googlegroups.com.

Doug

unread,
Feb 15, 2015, 6:34:38 AM2/15/15
to rubym...@googlegroups.com
Ok, so before jumping straight to a gem, the next problem is that a method such as
UIColor.with(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.9)

calls
UIColor.alloc.send('initWithRed:green:blue:alpha:’, *[0.5, 0.5, 0.5, 0.9])

And presents the error

Objective-C stub for message `initWithRed:green:blue:alpha:' type `@@:dddd' not precompiled. Make sure you properly link with the framework or library that defines this message.


It works absolutely fine calling the method from the repl, but from compiled code (e.g. my app delegate or view controller), it always errors. A single argument works fine such as the call
label = UILabel.alloc.initWithFrame([[50, 100], [100, 30]])

Which leads me to think that using splat in the send method isn’t working properly in the runtime? Would that make sense?

Doug

unread,
Feb 15, 2015, 6:35:54 AM2/15/15
to rubym...@googlegroups.com
That working example should be
label = UILabel.new(frame: [[50, 100], [100, 30]])

I can’t seem to edit posts on here? Is that normal?

Jamon Holmgren

unread,
Feb 16, 2015, 5:46:34 PM2/16/15
to rubym...@googlegroups.com
Yes -- you'll need to provide that method somewhere in your source so RubyMotion can precompile it. Even just a dummy class usually works.

class DummyStubs
 
def initWithRed(_, green: _, blue: _, alpha: _)
    abort
"Don't call this, it's just for RM"
 
end
end


- Jamon

Doug

unread,
Feb 18, 2015, 8:06:02 AM2/18/15
to rubym...@googlegroups.com
Ah, thanks Jamon! That explains an awful lot. I’ll have another look into this then.

Doug

Jamon Holmgren

unread,
Feb 19, 2015, 12:43:56 AM2/19/15
to rubym...@googlegroups.com
I'd be interested to check out the gem when it's ready, for sure!

Jamon

Doug

unread,
Feb 22, 2015, 2:22:50 PM2/22/15
to rubym...@googlegroups.com
So there’s still some issues with it, but here you go, my first Gem!


Doug

Jamon Holmgren

unread,
Feb 23, 2015, 12:14:21 PM2/23/15
to rubym...@googlegroups.com
Nice work!

Jamon Holmgren
Owner, ClearSight, LLC
Office: 360.450.2922 | Cell/Text: 360.609.4328


--
You received this message because you are subscribed to a topic in the Google Groups "RubyMotion - Ruby for iOS, OS X, and Android" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubymotion/UVgxewAiKJ0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubymotion+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubymotion/d97a33ac-b425-4e93-beaf-83f534130382%40googlegroups.com.

Nicholas Pachulski

unread,
Feb 23, 2015, 12:22:10 PM2/23/15
to rubym...@googlegroups.com
sweet, thanks

--
You received this message because you are subscribed to the Google Groups "RubyMotion - Ruby for iOS, OS X, and Android" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubymotion+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubymotion/CAD0Vak%2Btrq1uVcVW20p%2ByPCXzEFQQCKcDtiM-EzG60FbGkwQ-A%40mail.gmail.com.

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



--
no trees were hurt transmitting this message, however several trillion electrons were terribly inconvenienced

Doug

unread,
Feb 24, 2015, 9:20:30 AM2/24/15
to rubym...@googlegroups.com
Cheers, I’ve just had an awesome pull request that does away with Stubs generation by calling the methods through NSInvocation. So now, as far as I can see, this works really well including nesting a .with inside a .with method!
Reply all
Reply to author
Forward
0 new messages