<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLTensor/cast(like:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "s:6CoreML8MLTensorV4cast4likeA2C_tF"
  },
  "title" : "cast(like:)"
}
-->

# cast(like:)

Casts the elements of the tensor to the scalar type of the given array.

```
func cast(like other: MLTensor) -> MLTensor
```

## Parameters

`other`

The other tensor whose scalar type is used for the cast.

## Return Value

A new tensor with its contents cast to the scalar type of `other`.

## Discussion

For example:

```swift
let x = MLTensor([1, 2, 3], scalarType: Float.self)
let y = MLTensor([1, 2, 3], scalarType: Int32.self)
let z = y.cast(like: x)
z.scalarType // is Float
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)