API संदर्भ
इथेरियम API
Core API
गाइड

Swift टूल्स

Swift डेवलपर Ethereum विकास के लिए कई लाइब्रेरीज का उपयोग कर सकते हैं, जिसमें web3.swift iOS और macOS अनुप्रयोगों के लिए सबसे लोकप्रिय विकल्प है।

ध्यान दें!
नमस्ते! बस एक मित्रवत सूचना: यह पृष्ठ आपको काम करने का एक ठोस अवलोकन देने के लिए है Swift अनुरोध। व्यावहारिक कोड उदाहरणों के लिए, हमारा देखें API Methods documentation जहां आप सभी समर्थित भाषाओं में उपयोग के लिए तैयार उदाहरण पाएंगे!

# Web3.swift

Ethereum के लिए नेटिव Swift कार्यान्वयन।

import Web3
import BigInt

class EthereumClient {
    private let web3 = Web3(rpcURL: "YOUR_ETHEREUM_NODE_URL")

    func getBalance(address: String) async throws -> Double {
        let address = try EthereumAddress(hex: address, eip55: true)
        let balance = try await web3.eth.getBalance(address: address)
        return balance.converted(to: .ether).value
    }

    func sendTransaction(
        from: EthereumPrivateKey,
        to: String,
        amount: Double
    ) async throws -> String {
        let toAddress = try EthereumAddress(hex: to, eip55: true)
        let amount = EthereumAmount(value: amount, unit: .ether)

        let transaction = try await web3.eth.prepareTransaction(
            to: toAddress,
            value: amount,
            from: from.address
        )

        let signed = try transaction.sign(with: from)
        return try await web3.eth.send(transaction: signed)
    }
}
  • GitHub: web3.swift
  • विशेषताएँ:
    • नेटिव Swift कार्यान्वयन
    • iOS और macOS समर्थन
    • Async/await समर्थन
    • टाइप-सेफ कॉन्ट्रैक्ट इंटरैक्शन
    • वॉलेट प्रबंधन
    • ENS समर्थन

# SwiftUI एकीकरण

Ethereum इंटरैक्शन के लिए आधुनिक SwiftUI उदाहरण:

import SwiftUI
import Web3

struct WalletView: View {
    @StateObject private var viewModel = WalletViewModel()

    var body: some View {
        VStack {
            Text("Balance: \(viewModel.balance) ETH")

            Button("Refresh") {
                Task {
                    await viewModel.updateBalance()
                }
            }
        }
    }
}

class WalletViewModel: ObservableObject {
    private let client = EthereumClient()
    @Published var balance: Double = 0

    func updateBalance() async {
        do {
            balance = try await client.getBalance(address: "YOUR_ADDRESS")
        } catch {
            print("Error: \(error)")
        }
    }
}

# स्मार्ट कॉन्ट्रैक्ट एकीकरण

Swift का उपयोग करके स्मार्ट कॉन्ट्रैक्ट के साथ इंटरैक्ट करें:

struct Contract {
    let web3 = Web3(rpcURL: "YOUR_ETHEREUM_NODE_URL")
    let contractAddress: EthereumAddress

    func callMethod() async throws -> String {
        let contract = try await web3.eth.Contract(
            json: contractABI,
            address: contractAddress
        )

        return try await contract.method(
            "methodName",
            parameters: [param1, param2],
            extraData: Data()
        ).call()
    }
}

# त्रुटि नियंत्रण

Ethereum संचालन के लिए उचित Swift त्रुटि नियंत्रण:

enum EthereumError: Error {
    case invalidAddress
    case insufficientFunds
    case networkError(String)
}

extension EthereumClient {
    func safeGetBalance(address: String) async throws -> Double {
        guard address.hasPrefix("0x") else {
            throw EthereumError.invalidAddress
        }

        do {
            return try await getBalance(address: address)
        } catch {
            throw EthereumError.networkError(error.localizedDescription)
        }
    }
}

यह भी देखें

हमें बेहतर बनाने में मदद करें!
इस पृष्ठ को साझा करें और हमें आपके लिए और भी बेहतर उत्पाद बनाने में मदद करें।