我跟着这部影片https://www.youtube.com/watch?v=G5UkS4Mrepo。我已完全按照视频所示复制了代码。在15:16左右,他成功构建了应用程序,但是,我得到了这个错误:


  无法将'(String,String,String,String,UIColor,UIColor,UIColor,UIFont,UIFont)'类型的返回表达式转换为'OnboardingItemInfo'类型


我知道其他人过去曾经问过这个问题,但是在他们的错误消息中,当我的内容到此为止时,他们有一些跟随“ OnboardingItemInfo”的内容。

这是我的代码:

@IBOutlet weak var onboardingView: OnboardingView!

override func viewDidLoad() {
    super.viewDidLoad()
    onboardingView.dataSource = self;
}

func onboardingItemsCount() -> Int {
    return 1
}

func onboardingItemAtIndex(_ index: Int) -> OnboardingItemInfo {
    let backgroundColorOne = UIColor(red: 217/255, green: 72/255, blue: 89/255, alpha: 1)
    let backgroundColorTwo = UIColor(red: 106/255, green: 166/255, blue: 211/255, alpha: 1)
    let backgroundColorThree = UIColor(red: 168/255, green: 200/255, blue: 78/255, alpha: 1)

    let titleFont = UIFont(name: "AvenirNext-Bold", size: 24)!
    let descriptionFont = UIFont(name: "AvenirNext-Regular", size: 18)!

    return [("rocket", "A Great Rocket Start", "lorem ipsum", "", backgroundColorOne, UIColor.white, UIColor.white, titleFont, descriptionFont)][index]
}


谢谢!

最佳答案

以下函数func onboardingItemAtIndex(_ index: Int) -> OnboardingItemInfo应该返回OnboardingItemInfo类型的某些对象,该对象可以是结构或类。但是,您正在做的是完全不同的return [("rocket", "A Great Rocket Start", "lorem ipsum", "", backgroundColorOne, UIColor.white, UIColor.white, titleFont, descriptionFont)][index]
您需要找到OnboardingItemInfo的定义位置,并查看其初始化程序(这样您才能知道如何正确构造和返回对象/项目)。

struct StudentInfo {
  let name: String
  let age : Int
   // by default the struct gives us a free initializer, so no need to declare one
}

func something(name: String, age: Int) -> StudentInfo {
   /*
      here our something function takes a name and age as params
      and it returns a StudentInfo
   */
    return StudentInfo(name: name, age: age) //< here we are constructing a StudentInfo with the name and age provided
}

关于ios - 带有OnboardingItemInfo的PaperOnboarding错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48434776/

10-11 08:26