代码之家  ›  专栏  ›  技术社区  ›  Sedat Kapanoglu johnnywhoop

你能在swift中重载类型的铸造操作员吗?

  •  3
  • Sedat Kapanoglu johnnywhoop  · 技术社区  · 6 年前

    我想在swift中实现已知类型之间的自动类型转换。C的方法是重载类型转换运算符。如果我想要我的 X 可以交叉分配的类型,例如, string ,我会写:

    public class X 
    {
         public static implicit operator string(X value)
         {
              return value.ToString();
         }
    
         public static implicit operator X(string value)
         {
              return new X(value);
         }
    }
    

    在那之后,我可以写一些东西,比如:

    string s = new X();
    X myObj = s;
    

    它们会被自动转换。这在斯威夫特有可能吗?

    1 回复  |  直到 6 年前
        1
  •  3
  •   akaralar    6 年前

    不,该语言没有为自定义类型提供这样的功能。objective-c集合和swift集合之间存在桥接,但这是内置的,不可定制。

    // Swift array of `String` elements
    let swiftArray: [String] = ["Bob", "John"] 
    
    // Obj-C array of `NSString` elements, but element type information 
    // is not known to the compiler, so it behaves like an opaque NSArray
    let nsArray: NSArray = ["Kate", "Betty"] 
    
    // Obj-C array of an `NSString` and an `NSNumber`, element type
    // information is not known to the compiler
    let heterogeneousNSArray: NSArray = ["World", 3]
    
    // Casting with `as` is enough since we're going from Swift array to NSArray 
    let castedNSArray: NSArray = swiftArray as NSArray
    
    // Force casting with `as!` is required as element type information 
    // of Obj-C array can not be known at compile time
    let castedSwiftArray: [String] = nsArray as! [String]
    
    // Obj-C arrays can not contain primitive data types and can only 
    // contain objects, so we can cast with `as` without requiring a 
    // force-cast with `!` if we want to cast to [AnyObject]
    let heterogeneousCastedNSArray: [AnyObject] = heterogeneousNSArray as [AnyObject]
    

    提供类型铸造文件 here

    我认为你可以实现你想要做的初始化。

    extension X {
        init(string: String) {
            self = X(string)
        }        
    }
    
    extension String {
        init(x: X) {
            // toString is implemented elsewhere
            self = x.toString
        }        
    }
    
    let x = X()
    let string = "Bobby"
    
    let xFromString: X = X(string: string)
    let stringFromX: String = String(x: x)
    

    与你的问题没有直接关系,但也有一系列的协议 ExpressibleBy... ,使您能够执行以下操作: 假设我们要从整数文本初始化字符串。我们可以通过遵守和实施 ExpressibleByIntegerLiteral

    // Strings can not be initialized directly from integer literals
    let s1: String = 3 // Error: Can not convert value of type 'Int' to specified type 'String'
    
    // Conform to `ExpressibleByIntegerLiteral` and implement it
    extension String: ExpressibleByIntegerLiteral {
      public init(integerLiteral value: Int) {
        // String has an initializer that takes an Int, we can use that to 
        // create a string
        self = String(value)
      }
    }
    
    // No error, s2 is the string "4"
    let s2: String = 4 
    

    一个很好的用例 ExpressibleByStringLiteral 可以找到 here