簡體   English   中英

定義在Swift中調用自定義函數的中綴運算符

[英]Define an infix operator that calls a custom function in Swift

我正在制作一個調用函數的中綴運算符,該函數稍后在程序中定義。 這可能可行嗎?

我想要類似的東西

infix operator ∆+ { associativity left precedence 140 }

func ∆+ (left: String, right: String) -> String {
    return newAddition(left,second:right)
}

我發現的問題是它無法從我以后定義的代碼中提取。 我在類聲明之外聲明了這兩個部分。

是的你可以。 在全局范圍內定義的自定義運算符功能,也可以定義全局功能。

import UIKit

infix operator ∆+ { associativity left precedence 140 }

func ∆+ (left: String, right: String) -> String {
    return newAddition(left,second:right)
}

func newAddition(left: String, second: String) -> String
{
    return left + second
}

//....... Other sutffs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM