有关我做错的任何建议吗?初始化对象时,我似乎无法将另一个类中的数组作为参数。这是我的具体情况:
我正在尝试创建一个锻炼应用程序。该应用程序包含几种不同的锻炼。每次锻炼都有自己独特的训练阵容。
我有WorkoutObj的代码(这个结构创建了一个锻炼,需要一个NSArray workoutDrillSet)
import UIKit
struct WorkoutObj {
var workoutType: NSString
var workoutNumber: Int
var workoutDesc: NSString
var workoutDuration: Int
var workoutDrillSet: NSArray
init(type: String, number: Int, duration: Int, desc: String, drillSet: NSArray)
{
workoutType = type
workoutNumber = number
workoutDuration = duration
workoutDesc = desc
workoutDrillSet = drillSet
}
}
以下是我的DrillObjects(此结构是钻取数组所包含的对象 - 换句话说,钻取数组将由DrillObject1,DrillObject2,DrillObject3等组成)
import UIKit
struct DrillObject {
var drillDesc: NSString
var drillDurationMin: Int
var drillDurationSec: Int
var drillHRZone: Int
var drillEffort: Int
var drillCadence: Int
init(M: Int, S: Int, hRZone: Int, effort: Int, cadence: Int, name: NSString)
{
drillDesc = name
drillDurationMin = M
drillDurationSec = S
drillHRZone = hRZone
drillEffort = effort
drillCadence = cadence
}
}
我的下一堂课是我的EDrills课程。此类将每个DrillObject放入一个数组中。创建的数组名为arrayE1Drills
import UIKit
class EDrills: NSObject {
let dE1: DrillObject
let dE2: DrillObject
let dE3: DrillObject
let dE4: DrillObject
let dE5: DrillObject
let dE6: DrillObject
let dE7: DrillObject
let dE8: DrillObject
let dE9: DrillObject
let dE10: DrillObject
let dE11: DrillObject
let dE12: DrillObject
let dE13: DrillObject
let dE14: DrillObject
var arrayE1Drills : [DrillObject]
override init()
{
dE1 = DrillObject ( M: 5, S: 0, hRZone: 2, effort: 8, cadence: 95, name: "W/U Pedal Easy @ 90 RPM")
dE2 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Stand")
dE3 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Sit")
dE4 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Stand")
dE5 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Sit")
dE6 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Stand")
dE7 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Sit")
dE8 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Stand")
dE9 = DrillObject ( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Sit")
dE10 = DrillObject( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Stand")
dE11 = DrillObject( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Sit")
dE12 = DrillObject( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Seated 80% left leg")
dE13 = DrillObject( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Seated 80% right leg")
dE14 = DrillObject( M: 0, S: 30, hRZone: 2, effort: 10, cadence: 95, name: "W/U Seated 80% left leg")
arrayE1Drills =
[dE1,
dE2,
dE3,
dE4,
dE5,
dE6,
dE7,
dE8,
dE9,
dE10,
dE11,
dE12,
dE13,
dE14]
}
}
最后,我有了ECEouts类。这是一个举行多次训练的课程。 e1 workoutObject需要保存EDrills类的arrayE1Drills。
import UIKit
class EWorkouts {
var e1: WorkoutObj
var e2: WorkoutObj
var e3: WorkoutObj
var e4: WorkoutObj
var e5: WorkoutObj
var arrayEAll : [WorkoutObj]
var arrayE0to45 : [WorkoutObj]
var arrayE30to45: [WorkoutObj]
var arrayE45to60: [WorkoutObj]
var arrayEGreater45: [WorkoutObj]
var arrayE0to60: [WorkoutObj]
var arrayEGreater60: [WorkoutObj]
var myEDrillsClass : EDrills
init()
{
myEDrillsClass = EDrills()
let myArray = myEDrillsClass.arrayE1Drills
e1 = WorkoutObj(type: "E", number: 1, duration: 63, desc: "Rolling Hilly Course", drillSet: myArray)
}
当试图将arrayE1Drills加载到锻炼中时,我收到错误:Cannot invoke initializer for type "WorkoutObjc" with an argument list of type (type: String, number: Int, duration: Int, desc: String, drillSet: [DrillObject])
答案 0 :(得分:1)
存在类型不匹配
var workoutType: NSString
- > init : String
var workoutDesc: NSString
- > init : String
以及之后NSArray
< - > Array
从Swift 1.2开始,Foundation基本类型不会隐式地转换为Swift对应的
我建议只使用Swift类型