You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI iOS应用实例化Room对象时提示无参调用传参错误求助

问题:实例化Room对象时出现「Argument passed to call that takes no arguments」错误

在roomList中实例化Room对象时,每个实例都报「Argument passed to call that takes no arguments」错误,但明明是带参数调用的构造器,求解决。

相关代码

RoomListView.swift

struct RoomListView: View {
        @State private var roomList : [Room] = [
            Room(images: "room-1",roomType: "Master Room", ownerID: "1", roomDescription: "@#!@$$", favouritedBy: "1,2", roomPrice: 30, contactPeriod: "Long", currentBookedBy: ""),
            Room(images: "room-1",roomType: "Master Room", ownerID: "1", roomDescription: "@#!@$$", favouritedBy: "1,2", roomPrice: 30, contactPeriod: "Long", currentBookedBy: ""),
            Room(images: "room-1",roomType: "Master Room", ownerID: "1", roomDescription: "@#!@$$", favouritedBy: "1,2", roomPrice: 30, contactPeriod: "Long", currentBookedBy: ""),
        ]
        
        
        var body: some View {
            List{
                ForEach(roomList){
                    room in RoomCardView(room: room)
                        .padding(.top, 10)
                        .padding(.horizontal, 15)
                }
            }
        }
}

Models/Room.swift

import Foundation 
struct Room: Identifiable {
    let id = UUID()
    
    //    let id: String
    let images: String
    let roomType: String
    let ownerID: String
    let roomDescription: String
    let favouritedBy: String
    let roomPrice: Float
    let contactPeriod: String
    let currentBookedBy: String
    let updated_at: Double
    let created_at: Double
    
    init(images: String, roomType: String, ownerID: String, roomDescription: String, favouritedBy: String, roomPrice: Float, contactPeriod: String, currentBookedBy: String) {
        self.images = images
        self.roomType = roomType
        self.ownerID = ownerID
        self.roomDescription = roomDescription
        self.favouritedBy = favouritedBy
        self.roomPrice = roomPrice
        self.contactPeriod = contactPeriod
        self.currentBookedBy = currentBookedBy
        self.updated_at = Date()
        self.created_at = Date()
    }
}

问题原因

你的Room结构体自定义构造器里存在类型不匹配的问题:updated_at和created_at声明为Double类型,但你直接赋值Date()(Date类型),这会导致构造器编译失败。Swift此时会认为没有可用的匹配构造器,所以抛出「调用无参构造器」的错误(因为默认的成员构造器在自定义构造器后被覆盖了,而你的自定义构造器又无法正常使用)。

解决方案

把Date()转换为Double类型的时间戳,使用Date().timeIntervalSince1970即可获取从1970年到当前时间的秒数,完美匹配Double类型要求。

修正后的Room.swift代码

import Foundation 
struct Room: Identifiable {
    let id = UUID()
    
    let images: String
    let roomType: String
    let ownerID: String
    let roomDescription: String
    let favouritedBy: String
    let roomPrice: Float
    let contactPeriod: String
    let currentBookedBy: String
    let updated_at: Double
    let created_at: Double
    
    init(images: String, roomType: String, ownerID: String, roomDescription: String, favouritedBy: String, roomPrice: Float, contactPeriod: String, currentBookedBy: String) {
        self.images = images
        self.roomType = roomType
        self.ownerID = ownerID
        self.roomDescription = roomDescription
        self.favouritedBy = favouritedBy
        self.roomPrice = roomPrice
        self.contactPeriod = contactPeriod
        self.currentBookedBy = currentBookedBy
        // 修正:将Date转为Double类型的时间戳
        self.updated_at = Date().timeIntervalSince1970
        self.created_at = Date().timeIntervalSince1970
    }
}

修正后,自定义构造器即可正常工作,roomList中的实例化代码也不会再报错。


内容的提问来源于stack exchange,提问作者Hanyi Koh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:55:26