DEV Community

SameX
SameX

Posted on

HarmonyOS Next type conversion practice: a guide to safe conversion from basic data to objects

In distributed device development, type conversion is like a translator in different languages.The on-board system collapsed due to coercive conversion. Later, this safety conversion system was summarized. Now I will share it with you to avoid pitfalls.

1. Basic type conversion: the safety bottom line of explicit operation

1. Numerical type conversion rules

  • Must be explicitly converted: target type (value)
  • Overflow protection: Compilation period checks limit value exceeded
let a: Int32 = 200
let b: Int8 = Int8(a) // Compile error: 200 out of Int8 range
let c: UInt32 = UInt32(a) // Correct: 200 within the UInt32 range
Enter fullscreen mode Exit fullscreen mode

2. Rune and numerical value transfer

  • Rune→UInt32: Get Unicode code points
  • UInt32→Rune: Make sure the value is within the range of 0x0-0x10FFFF
let rune: Rune = 'medium'
let code: UInt32 = UInt32(rune)  // 20013
let invalid: Rune = Rune(0x200000) // Runtime exception
Enter fullscreen mode Exit fullscreen mode

2. Object conversion: a security bridge in a polymorphic world

1. Type Check Three Musketeers

  • is: judge type (return to Bool)
  • as?: Safe conversion (return to None if failed)
  • as!: Cases (failed crash)
open class Animal {}
class Dog <: Animal {}

let pet: Animal = Dog()
if pet is Dog {
let dog = pet as? Dog // Safe conversion
    dog.bark()
}
Enter fullscreen mode Exit fullscreen mode

2. Transformation logic between interface and class

  • Class → Interface: Implicit Upward Transformation (Polymorphic Basics)
  • Interface → Class: Explicit downward transformation (need to type matching)
interface Flyable {}
class Bird <: Flyable {}

let bird: Bird = Bird()
let flyable: Flyable = bird // Legal, interface transformation
let backToBird = flyable as? Bird // Convert successfully
Enter fullscreen mode Exit fullscreen mode

3. Cross-scene conversion traps and countermeasures

1. Type erase trap

Generic containers lose specific type information:

let list: Array<Any> = [Dog()]
let dog = list[0] as? Dog // Success
let cat = list[0] as? Cat // Failed, return None
Enter fullscreen mode Exit fullscreen mode

2. Cases abuse

Counterexample (danger):

let obj: Any = "text"
let num = obj as! Int // crashes during runtime
Enter fullscreen mode Exit fullscreen mode

Regular example (safety):

if let str = obj as? String {
    processString(str)
}
Enter fullscreen mode Exit fullscreen mode

4. Practical combat: Type conversion design of equipment adaptation layer

1. Unified interface definition

interface DeviceData {
    func toJSON(): String
}

class DeviceAData <: DeviceData {
    private let value: Int
    public func toJSON() -> String {
        "{\"value\": \(value)}"
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Adaptation layer conversion logic

func processData(data: Any) {
    if let deviceData = data as? DeviceData {
        let json = deviceData.toJSON()
        sendToCloud(json)
    } else {
print("Unsupported Type")
    }
}
Enter fullscreen mode Exit fullscreen mode

5. The golden rule of safe conversion

  1. Basic Type: Always explicit conversion, using compile-time overflow check
  2. Object conversion: Priority for as? safe conversion, avoid as! cast
  3. Architecture Design: Reduce runtime conversion requirements through interface abstraction

Top comments (0)