xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

SwiftUI error All In One

SwiftUI error All In One

Instance member xxx cannot be used on type yyy

Instance member 'game' cannot be used on type 'ContentView_Previews'


//
//  ContentView.swift
//  MemorizeGame
//
//  Created by 夏凌晨 on 2020/10/28.
//  Copyright © 2020 xgqfrms. All rights reserved.
//

// viewModel
import SwiftUI

// ✅ some View,返回任意类型的 View, 自动推断 View 类型
// Identifiable
struct ContentView: View {
  var viewModel: EmojiMemoryGame

  var body: some View {
    HStack {
      // ForEach(0..<4) { index in
      ForEach(viewModel.cards) { card in
        // print("index \(index)")
        CardView(card: card).onTapGesture {
          // self
          self.viewModel.choose(card: card)
        }
        // CardView(isFaceUp: (index % 2 == 0))
      }
    }
//    HStack {
//      CardView(isFaceUp: true)
//      CardView(isFaceUp: false)
//      CardView(isFaceUp: true)
//      CardView(isFaceUp: false)
//    }
  }
}

struct CardView: View {
  var card: MemoryGame<String>.Card
  // var isFaceUp: Bool
  // var isFaceUp: Bool = false
  var body: some View {
    ZStack {
      if card.isFaceUp {
        RoundedRectangle(cornerRadius: 10.0).fill(Color.white)
        RoundedRectangle(cornerRadius: 10.0).stroke(lineWidth: 3.0)
        Text(card.content).font(.largeTitle)
        // Text("👻").font(.largeTitle)
      } else {
        RoundedRectangle(cornerRadius: 10.0).fill(Color.orange)
      }
    }.padding()
  }
}




//  PreviewProvider 仅用于预览,代码在项目中没有实际的意义
struct ContentView_Previews: PreviewProvider {
  let game = EmojiMemoryGame()
  static var previews: some View {
    ContentView(viewModel: game)
  }
}

change file name bug

  1. old MemorizeGame ❌
//
//  ContentView.swift
//  MemorizeGame

  1. new MemoryGame ✅

//
//  ContentView.swift
//  MemoryGame

refs



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-11-08 20:32  xgqfrms  阅读(229)  评论(8编辑  收藏  举报