Swift -在call [closed]中缺少参数'data'的参数

jaxagkaj  于 2023-10-15  发布在  Swift
关注(0)|答案(2)|浏览(104)

已关闭此问题为not reproducible or was caused by typos。它目前不接受回答。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
10天前关闭。
Improve this question
我想在我的UITableView中显示一个用户列表。我得到这个错误后,我建立它。
我创建了一个数据模型来存储用户列表。
CRUDSwiftData.swift

import UIKit

class CRUDSwiftData: NSObject {

    var title: String
    var rating: Float
    
    init(title: String,rating: Float) {
        self.title = title
        self.rating = rating
    }
    
}

CRUDSwiftDoc.swift

import UIKit

class CRUDSwiftDoc: NSObject {
   
    var data:CRUDSwiftData
    var thumbImage:UIImage
    var fullImage:UIImage
    
    init(title: String,rating: Float,data:CRUDSwiftData, thumbImage:UIImage, fullImage:UIImage) {
        self.data = CRUDSwiftData(title: title,rating: rating);
        self.thumbImage = thumbImage
        self.fullImage = fullImage
    }
}

然后我把MasterViewController.swift类编辑成这样
MasterViewController.swift

var users: NSMutableArray = []

// At the end of viewDidLoad
self.title = "User List";
 
// Replace the return statement in tableView:numberOfRowsInSection with the following:
return users.count
 
// Replace tableView:cellForRowAtIndexPath with the following
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView
               dequeueReusableCellWithIdentifier:@"MyBasicCell"];
  let user = users[indexPath.row] as CRUDSwiftDoc
  cell.textLabel.text = user.data.title
  cell.imageView.image = user.thumbImage
  return cell;
}

最后我的AppDelegate.swift是这样的
AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        // Override point for customization after application launch.
        let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: "4", thumbImage: "potatoBugThumb.jpg", fullImage: "potatoBug.jpg")
        let user2 = CRUDSwiftDoc(title:"House Centipede", rating: "3", thumbImage: "centipedeThumb.jpg", fullImage: "centipede.jpg")
        let user3 = CRUDSwiftDoc(title:"Wolf Spider", rating: "5", thumbImage: "wolfSpiderThumb.jpg", fullImage: "wolfSpider.jpg")
        let user4 = CRUDSwiftDoc(title:"Lady Bug", rating: "1", thumbImage: "ladybugThumb.jpg", fullImage: "ladybug.jpg")
        let shoppingList: NSMutableArray[] = [user1, user2, user3, user4]
        
        return true
    }

我在AppDelegate.swift中得到4个错误

kupeojn6

kupeojn61#

编译器说的是实话!您呼叫

let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: "4", thumbImage: "potatoBugThumb.jpg", fullImage: "potatoBug.jpg")

但你的声明要求

init(title: String, rating: Float, data:CRUDSwiftData, thumbImage:UIImage, fullImage:UIImage) {
  • 你没有提供数据!
    更新您需要通过init将数据传递给CRUDSiftDoc(),但A)您没有将其传递进去,B)您正在计算它。您需要将init更改为
class CRUDSwiftDoc: NSObject {

    var data:CRUDSwiftData
    var thumbImage:UIImage
    var fullImage:UIImage

    init(title: String, rating: Float, thumbImage:UIImage, fullImage:UIImage) { // No data!!!
        self.data = CRUDSwiftData(title: title,rating: rating);
        self.thumbImage = thumbImage
        self.fullImage = fullImage
    }
}

另请注意,您将rating作为字符串传递(rating:“4”),而不是声明的Float。

o7jaxewo

o7jaxewo2#

你的CRUDSwiftDoc类有这样的初始化器:

init(title: String,rating: Float,data:CRUDSwiftData, thumbImage:UIImage, fullImage:UIImage)

第三个参数是数据,但是当您在

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool

你不提供数据参数:

let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: "4", thumbImage: "potatoBugThumb.jpg", fullImage: "potatoBug.jpg")

您必须在rating和thumbImage之间添加数据。
你也传递字符串到评级参数,但它需要浮点数,你再次传递字符串到thumbImage和fullImage,但它需要UIImage。将其更改为:

let data1 = CRUDSwiftData(title: "title 1", rating: 2.1);
let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: 4.0, data:data1, thumbImage: UIImage(named:"potatoBugThumb.jpg"), fullImage: UIImage(named:"potatoBug.jpg"))

最后一个问题是,你创建了NSMUtableArray数组,但传递了CRUDSiftDoc对象数组。将您的代码替换为:
让shoppingList:CRUD SwiftDoc [] = [user1,user2,user3,user4]

相关问题