将对象数组传递到另一个类

时间:2018-01-22 01:48:12

标签: ios arrays swift for-loop

我已经创建了achievementArrayAchievementObject并附加了所有必要的值。出于测试目的,我对第一个索引进行了硬编码。我想将这个数组传递给我的AchievementDatabase类,这样我就可以检查数组的大小。 (我希望在集成我的实际数据库之前让客户端工作)。

我收到此错误:

  

无法将AppDelegate.AchievementObject.Type类型的值转换为预期的参数类型AppDelegate.AchievementObject

这是我的相关代码,全部在AppDelegate.swift文件中:

var achievementArray = [AchievementObject]();
achievementArray.append(AchievementObject(achievementID: -1, achievementName: "achievementName", achievementDescription: "achievementDescription")); //Append to allow subscripting

achievementArray[1].achievementID = 1;
achievementArray[1].achievementName = "Hello World!";
achievementArray[1].achievementName = "Be Born!";

debugPrint(achievementArray[1].achievementName); //Returns Hello World!

var achievementDB = AchievementDatabase(achievementArray: AchievementObject); *<-- Error Here*

class AchievementObject {

    var achievementID: Int;
    var achievementName: String;
    var achievementDescription: String;
    init(achievementID: Int, achievementName: String, achievementDescription: String) {
        self.achievementID = achievementID;
        self.achievementName = achievementName;
        self.achievementDescription = achievementDescription;
        return;
    }

class AchievementDatabase {
    //Act as a local database storage until connection is made
    var numAchievements: Int = 1; //Will change to achievementArray.count();
    init(achievementArray: AchievementObject) {

        for i in 1...numAchievements {
            debugPrint("i: \(i)");
            //var achievement = AchievementObject(id:i); //
        }
    }

2 个答案:

答案 0 :(得分:1)

我认为您的代码中有一些更改。无论如何 主要问题是AchievementDatabase获得了AchievementObject,但实际上您需要一个数组,那么您应该使用符号[AchievementObject]

所以你可能想要遵循这个重构:

<强>测试

var achievementArray = [AchievementObject]()
achievementArray.append(AchievementObject(achievementID: -1, achievementName: "achievementName", achievementDescription: "achievementDescription"))
achievementArray[0].achievementID = 1
achievementArray[0].achievementName = "Hello World!"
achievementArray[0].achievementName = "Be Born!"

let achievementDB = AchievementDatabase(achievementArray: achievementArray)
print(achievementDB.achievementArray.count)

<强>型号:

class AchievementObject {
    var achievementID: Int
    var achievementName: String
    var achievementDescription: String

    init(achievementID: Int, achievementName: String, achievementDescription: String) {
        self.achievementID = achievementID
        self.achievementName = achievementName
        self.achievementDescription = achievementDescription
    }
}

class AchievementDatabase {
    var achievementArray: [AchievementObject]

    init(achievementArray: [AchievementObject]) {
        self.achievementArray = achievementArray
        achievementArray.enumerated().forEach { (index, value) in
            print("index: \(index), value: \(value.achievementName)")
        }
    }
}

<强>观察

  1. 因为多余,请避免使用分号;
  2. AchievementDatabase正在获取一个对象,但您应该传递一个类似[AchievementObject]
  3. 的数组
  4. 您可能希望使用简洁的forEach,而不是详细的for i..n,很好的解释here
  5. 您正在访问数组的第1位,这实际上是仅包含1个元素的数组的第二个位置,因此 - &gt;碰撞
  6. 使用achievementArray.first
  7. 访问此类位置无崩溃的最佳方法
  8. AchievementObject应该是一个结构,而不是一个类。那你甚至不需要提供init。
  9. 如果给定AchievementObject的值在设置后不会更改,请将所有属性更改为var。

答案 1 :(得分:-1)

尝试

 var achievementDB =  = AchievementDatabase(achievementArray: achievementArray))