Any suggestions on what I'm doing wrong? When initializing an object, I can't seem to have an Array from another class as a parameter. Here is my specific situation:
对我做错了什么有什么建议吗?在初始化对象时,我似乎不能将来自另一个类的数组作为参数。以下是我的具体情况:
I am trying to create a workout app. The app contains several different workouts. Each workout has it's own unique array of drills.
我正在尝试创建一个锻炼应用程序。这个应用程序包含了几种不同的锻炼。每个训练都有自己独特的训练方法。
I have my code for WorkoutObj (This struct creates a workout and needs an NSArray workoutDrillSet)
我有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
}
}
Here are my DrillObjects (This struct is the object that the drill array will be comprised of - in others words, the drill array will consist of DrillObject1, DrillObject2, DrillObject3, etc)
这是我的drillobject(这个结构体是钻阵列将包含的对象——换句话说,钻阵列将由DrillObject1、DrillObject2、drillobt3等组成)
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
}
}
My next class is my EDrills Class. This Class puts each DrillObject into an array. The array created is called, arrayE1Drills
我的下一节课是副标题课。这个类将每个DrillObject放入一个数组中。创建的数组称为arraye1drill
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]
}
}
Finally, I have the Class EWorkouts. This is a class that holds several workouts. The e1 workoutObject needs to hold the arrayE1Drills of the EDrills class.
最后,我有课堂锻炼。这是一个可以进行多种训练的课程。e1 workoutObject需要持有EDrills类的arraye1drill。
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)
}
When Trying to load the arrayE1Drills into the workout, I get an error: Cannot invoke initializer for type "WorkoutObjc" with an argument list of type (type: String, number: Int, duration: Int, desc: String, drillSet: [DrillObject])
当尝试将arraye1drill加载到训练中时,我得到一个错误:无法调用类型为“WorkoutObjc”的初始化器,其参数列表为类型(类型:String, number: Int, duration: Int, desc: String, drillSet: [DrillObject])
1 个解决方案
#1
1
There are type mismatches
有类型不匹配
var workoutType: NSString
--> init : String
NSString——> init: String
var workoutDesc: NSString
--> init : String
var workoutDesc: NSString——> init: String
and later NSArray
<--> Array
然后NSArray <——>数组
Since Swift 1.2 the Foundation basic types are not implicitly casted to the Swift counterparts
由于Swift 1.2,所以基本类型并不隐式地赋给Swift对等项
I recommend to use only the Swift types
我建议只使用Swift类型
#1
1
There are type mismatches
有类型不匹配
var workoutType: NSString
--> init : String
NSString——> init: String
var workoutDesc: NSString
--> init : String
var workoutDesc: NSString——> init: String
and later NSArray
<--> Array
然后NSArray <——>数组
Since Swift 1.2 the Foundation basic types are not implicitly casted to the Swift counterparts
由于Swift 1.2,所以基本类型并不隐式地赋给Swift对等项
I recommend to use only the Swift types
我建议只使用Swift类型