Swift style initializers

瀏覽次數:81 次
跳到第一則未讀訊息

Doug

未讀,
2015年2月10日 中午12:33:122015/2/10
收件者: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

未讀,
2015年2月13日 中午12:01:002015/2/13
收件者: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

未讀,
2015年2月14日 下午6:05:542015/2/14
收件者: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

未讀,
2015年2月14日 晚上7:38:212015/2/14
收件者: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

未讀,
2015年2月15日 清晨6:34:382015/2/15
收件者: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

未讀,
2015年2月15日 清晨6:35:542015/2/15
收件者: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

未讀,
2015年2月16日 下午5:46:342015/2/16
收件者: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

未讀,
2015年2月18日 上午8:06:022015/2/18
收件者:rubym...@googlegroups.com
Ah, thanks Jamon! That explains an awful lot. I’ll have another look into this then.

Doug

Jamon Holmgren

未讀,
2015年2月19日 凌晨12:43:562015/2/19
收件者:rubym...@googlegroups.com
I'd be interested to check out the gem when it's ready, for sure!

Jamon

Doug

未讀,
2015年2月22日 下午2:22:502015/2/22
收件者:rubym...@googlegroups.com
So there’s still some issues with it, but here you go, my first Gem!


Doug

Jamon Holmgren

未讀,
2015年2月23日 中午12:14:212015/2/23
收件者: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

未讀,
2015年2月23日 中午12:22:102015/2/23
收件者: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

未讀,
2015年2月24日 上午9:20:302015/2/24
收件者: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!
回覆所有人
回覆作者
轉寄
0 則新訊息