一件式管理器课程
import Foundation
import UIKit
import GameKit
struct OnePieceManager {
let fact: String
let color: UIColor
let music: String
}
struct OnePiece {
private let onePieceFacts = [OnePieceManager(fact: "Oda was born in Kumamoto, Japan on January 1, 1975.", color: UIColor(red:1.00, green:0.42, blue:0.00, alpha:1.00), music: "song"),
OnePieceManager(fact: "One Piece is currently the best selling manga in the world, it sold well over 380 million copies and it is not stopping there.", color: UIColor(red:0.98, green:0.70, blue:0.00, alpha:1.00), music: "song"),
OnePieceManager(fact: "Even though Luffy is the main character of One Piece, Nami appears on screen before Luffy in the anime making her the first main character to be seen.", color: UIColor(red:0.28, green:0.21, blue:0.42, alpha:1.00), music: "song"),
OnePieceManager(fact: "The only way Luffy will part from his crew is either if he becomes the Pirate King or if he dies.", color: UIColor(red:1.00, green:0.63, blue:0.00, alpha:1.00), music: "song")]
func randomFact() -> OnePieceManager {
let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: onePieceFacts.count)
return onePieceFacts[randomNumber]
}
}
事实视图控制器
import Foundation
import UIKit
import AVFoundation
class FactsViewController: UIViewController {
@IBOutlet weak var onePieceFactlbl: UILabel!
@IBOutlet weak var onePieceFactBtn: UIButton!
let onePieceManager = OnePieceManager()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func onePieceFactBtnDidTouch(_ sender: Any) {
}
}
答案 0 :(得分:0)
问题出在这一行
let onePieceManager = OnePieceManager()
所以init
这样
let onePieceManager = OnePieceManager(fact: "Oda", color: UIColor(red:1.00, green:0.42, blue:0.00, alpha:1.00), music: "song")
OR
var onePieceManager:OnePieceManager?
答案 1 :(得分:0)
不同于具有非可选成员的class
和struct
,它提供了一个合成的(称为 memberwise )初始化器
init(fact: String, color: UIColor, music: String)
已指定。这意味着您必须使用它,无法使用默认的初始化程序()
。
在onePieceManager
中将变量FactsViewController
声明为可选变量–或在将其用作隐式未包装可选变量之前是否已设置值:
var onePieceManager : OnePieceManager? // or OnePieceManager!