潇潇老公 發表於 2023-3-13 14:13:57

Swift 重构重载运算符示例解析

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、思路来源</li><li>二、基础类型 String 为例</li><ul class="second_class_ul"><li>重载乘法运算符</li><li>源码实现:</li></ul><li>三、对象类型 UIEdgeInsets 为例</li><ul class="second_class_ul"><li>重载加法运算符将</li><li>源码实现:</li></ul><li>四、总结</li><ul class="second_class_ul"></ul></ul></div><p class="maodian"></p><h2>一、思路来源</h2>
<div class="cros igoods"><div class="goodsin" data-img="https://img14.360buyimg.com/pop/jfs/t3229/39/2924280771/392779/d242f52a/57e8d68eN57129bbf.jpg" data-name="精通Swift设计模式(图灵出品)" data-owner="京东自营" data-price="77.4" data-tgid="38" data-url="https://union-click.jd.com/jdc?e=&amp;p=JF8BAMoJK1olXwUFU1xdCE4TBl8IG1MTWgIAVm4ZVxNJXF9RXh5UHw0cSgYYXBcIWDoXSQVJQwYCXFhaDEkVHDZNRwYlIn9gLFY4fSh3RB1eGRpJDlljEFtcTkcbM2gNHF4dXwMBZF5eDkwXAmoIK2sVXDZQOobrvpOysnPcsdTA1ZEyVW5dD00eBGYBG1MRWQILZF5VDHtUVypcWBhdbTYyV25tOEsnAF9WdVpGWwQCVl9cZhZABDhVUgMcMwYLUlleDk0XC18KGloXXzYy"></div></div>
<p>研究自定义运算符的时候,又重新看了一下重载运算符,觉得挺有意思的,随手封装了几个。</p>
<p class="maodian"></p><h2>二、基础类型 String 为例</h2>
<p class="maodian"></p><h3>重载乘法运算符</h3>
<p>将一段字符串重复 n 次拼接在一起;</p>
<div class="jb51code"><pre class="brush:java;">let c = "abc"*3;
print("c: \(c)");//c: abcabcabc
</pre></div>
<p class="maodian"></p><p class="maodian"></p><h3>源码实现:</h3>
<div class="jb51code"><pre class="brush:java;">public extension String{
    static func * (lhs: String, rhs: Int) -&gt; String {
      return String(repeating: lhs, count: rhs);
   }
}
</pre></div>
<p class="maodian"></p><h2>三、对象类型 UIEdgeInsets 为例</h2>
<p class="maodian"></p><h3>重载加法运算符将</h3>
<p>将 top, left, bottom, right 属性逐个求和;</p>
<div class="jb51code"><pre class="brush:java;">let edg1 = UIEdgeInsets(top: 1, left: 2, bottom: 3, right: 4);
let edg2 = UIEdgeInsets(top: 4, left: 3, bottom: 2, right: 1);
let e = edg1 + edg2;

print("edg1: \(edg1)");//edg1: UIEdgeInsets(top: 1.0, left: 2.0, bottom: 3.0, right: 4.0)
print("edg2: \(edg2)");//edg2: UIEdgeInsets(top: 4.0, left: 3.0, bottom: 2.0, right: 1.0)
print("e: \(e)");//e: UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
</pre></div>
<h3>源码实现:</h3>
<div class="jb51code"><pre class="brush:java;">public extension UIEdgeInsets{

    static func + (lhs: UIEdgeInsets, rhs: UIEdgeInsets) -&gt; UIEdgeInsets {
      return UIEdgeInsets(lhs.top + rhs.top,
                            lhs.left + rhs.left,
                            lhs.bottom + rhs.bottom,
                            lhs.right + rhs.right
      );
   }
}
</pre></div>
<p class="maodian"></p><h2>四、总结</h2>
<p>1、重载运算符可以将繁琐的操作简单化,复杂的操作封装化,属于质变的重构方式;</p>
<p>2、编程的核心是创造力,你创造各种工具函数的能力越强则工作越轻松,反之亦然。</p>
<p>以上就是Swift 重构重载运算符示例解析的详细内容,更多关于Swift 重构重载运算符的资料请关注琼殿技术社区其它相关文章!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>Swift重构自定义空等运算符&nbsp;“??=”&nbsp;实例</li><li>iOS Swift逻辑运算符示例总结</li><li>swift中自定义正则表达式运算符=~详解</li><li>Swift心得笔记之运算符</li><li>Swift之运算符重载示例详解</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: Swift 重构重载运算符示例解析