def try(*a, &b) if a.empty? && block_given? yield self else public_send(*a, &b) if respond_to?(a.first) end end
Should be easy to do the same in swift. Let me take a stab at it.