Post not yet marked as solved
This should work
class PersistantStorage<T: Codable & Identifiable> {
func store(_ object: T) throws { }
func objectFor(_ key: T.ID) -> T? {
return nil
}
}
You could also write it like this:
class PersistantStorage<T> where T: Codable & Identifiable{
func store(_ object: T) throws { }
func objectFor(_ key: T.ID) -> T? {
return nil
}
}
Post not yet marked as solved
@Published var cards: [Card] = [
Card(content: "😀"),
Card(content: "😀"),
Card(content: "😘"),
Card(content: "🥶"),
Card(content: "😡"),
Card(content: "🥶"),
Card(content: "😘"),
Card(content: "😡")
]
Represents all your cards, in this case 8 cards in the array
Post not yet marked as solved
import SwiftUI
struct Card: Identifiable{
var id = UUID()
var content: String
var isFacedUp: Bool = false
var isMatched = false
}
class ViewModel: ObservableObject{
@Published var cards: [Card] = [
Card(content: "😀"),
Card(content: "😀"),
Card(content: "😘"),
Card(content: "🥶"),
Card(content: "😡"),
Card(content: "🥶"),
Card(content: "😘"),
Card(content: "😡")
]
func tapped(_ card: Card){
if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}), !cards[chosenIndex].isFacedUp, !cards[chosenIndex].isMatched{
if let potenialMatcehIndex = cards.indices.filter({ cards[$0].isFacedUp}).oneAndOnly{
print("ONE")
if cards[chosenIndex].content == cards[potenialMatcehIndex].content{
print("TWO")
cards[potenialMatcehIndex].isMatched = true
cards[chosenIndex].isMatched = true
}
cards[chosenIndex].isFacedUp = true
} else{
print("THREE")
cards.indices.forEach({cards[$0].isFacedUp = ($0 == chosenIndex)})
}
}
}
}
struct ContentView: View {
let columns = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]
@StateObject var viewModel = ViewModel()
var body: some View {
LazyVGrid(columns: columns) {
ForEach(viewModel.cards) { card in
RoundedRectangle(cornerRadius: 20)
.frame(width: 70, height: 70)
.foregroundColor(card.isFacedUp || card.isMatched ? Color(.systemIndigo) : .purple)
.padding()
.overlay(Text(card.content).font(.system(size: 30)).rotation3DEffect(Angle(degrees: card.isFacedUp || card.isMatched ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(card.isFacedUp || card.isMatched ? 1 : 0))
.rotation3DEffect(card.isFacedUp || card.isMatched ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
.animation(.default, value: card.isFacedUp)
.onTapGesture { viewModel.tapped(card) }
}
}
}
}
extension Array{
var oneAndOnly: Element?{
if self.count == 1{
return self.first
} else{
return nil
}
}
}
Try this:
import SwiftUI
struct Card: Identifiable{
var id = UUID()
var content: String
var isFacedUp: Bool = false
var isMatched = false
}
class ViewModel: ObservableObject{
@Published var cards: [Card] = [
Card(content: "😀"),
Card(content: "😀"),
Card(content: "😘"),
Card(content: "🥶"),
Card(content: "😡"),
Card(content: "🥶"),
Card(content: "😘"),
Card(content: "😡")
]
func tapped(_ card: Card){
if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}), !cards[chosenIndex].isFacedUp, !cards[chosenIndex].isMatched{
if let potenialMatcehIndex = cards.indices.filter({ cards[$0].isFacedUp}).oneAndOnly{
print("ONE")
if cards[chosenIndex].content == cards[potenialMatcehIndex].content{
print("TWO")
cards[potenialMatcehIndex].isMatched = true
cards[chosenIndex].isMatched = true
}
cards[chosenIndex].isFacedUp = true
} else{
print("THREE")
cards.indices.forEach({cards[$0].isFacedUp = ($0 == chosenIndex)})
}
}
}
}
struct ContentView: View {
let columns = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]
@StateObject var viewModel = ViewModel()
var body: some View {
LazyVGrid(columns: columns) {
ForEach(viewModel.cards) { card in
RoundedRectangle(cornerRadius: 20)
.frame(width: 70, height: 70)
.foregroundColor(card.isFacedUp || card.isMatched ? Color(.systemIndigo) : .purple)
.padding()
.overlay(Text(card.content).font(.system(size: 30)).rotation3DEffect(Angle(degrees: card.isFacedUp || card.isMatched ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(card.isFacedUp || card.isMatched ? 1 : 0))
.rotation3DEffect(card.isFacedUp || card.isMatched ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
.animation(.default, value: card.isFacedUp)
.onTapGesture { viewModel.tapped(card) }
}
}
}
}
extension Array{
var oneAndOnly: Element?{
if self.count == 1{
return self.first
} else{
return nil
}
}
}
I found a solution.
@FetchRequest(
entity: LifetimeInputs.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], predicate: nil
) var lifetimeInputsModel: FetchedResults<LifetimeInputs>
Becomes this (in my case)
@FetchRequest var lifetimeInputs: FetchedResults<LifetimeInputs>
init(nsPredicate: NSPredicate, sortDescriptors: [NSSortDescriptor]) {
let entity = LifetimeInputs.entity()
let fetchRequest = FetchRequest<LifetimeInputs>(entity: entity, sortDescriptors: sortDescriptors, predicate: nsPredicate, animation: .default)
self._lifetimeInputs = fetchRequest
}
The nsPredicate and the sortDescriptors must be passed to a parent view and you have to change/update (if you need to) them in that parent view.
Hope it works also for you
Post not yet marked as solved
I think that the solution is in your previous thread, anyway here is the solution:
struct ContentView: View {
@State private var flip: Bool = false
var body: some View {
Group {
Button {
withAnimation {
flip.toggle()
}
} label: {
Rectangle()
.frame(width: 140, height: 170)
.foregroundColor(Color(.systemTeal))
.cornerRadius(20)
.rotation3DEffect(.degrees(flip ? 180 : 0), axis: (x: 0, y: 1, z: 0))
}
}
}
}
Post not yet marked as solved
struct ContentView: View {
@State private var flipped = false
var body: some View {
Group {
RoundedRectangle(cornerRadius: 20)
.frame(width: 140, height: 170)
.foregroundColor(flipped ? .red : .orange)
.padding()
.overlay(Text("Hello, world!").rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(flipped ? 1 : 0))
.rotation3DEffect(flipped ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
.animation(.default, value: flipped)
.onTapGesture {
flipped.toggle()
}
}
}
}
struct ContentView: View {
@State private var flipped = false
var body: some View {
Group {
RoundedRectangle(cornerRadius: 20)
.frame(width: 140, height: 170)
.foregroundColor(flipped ? .yellow : .purple)
.padding()
.overlay(Text("Hello, world!").rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(flipped ? 1 : 0))
.rotation3DEffect(flipped ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
.animation(.default, value: flipped)
.onTapGesture {
flipped.toggle()
}
}
}
}
Is this ok?
Or maybe this:
struct ContentView: View {
@State private var flipped = false
var body: some View {
Group {
ZStack{
RoundedRectangle(cornerRadius: 20)
.frame(width: 140, height: 170)
.foregroundColor(flipped ? .red : .orange)
.padding()
Text("Hello, world!")
.rotation3DEffect(.degrees(flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
}
.rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
.onTapGesture {
withAnimation(.linear(duration: 1)) {
flipped.toggle()
}
}
}
}
}
I don't know if I understand what you mean but you can try user .overlay( Text("Hello, World!") ) as a modifier to the RoundedRectangle
Post not yet marked as solved
Hello, have you found a solution for this? Thank You
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init() {
container = NSPersistentCloudKitContainer(name: "MJ")
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.Jad.MJ")?.appendingPathComponent("MJ.sqlite") else {
fatalError("Shared file container could not be created.")
}
let storeDescription = NSPersistentStoreDescription(url: fileContainer)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
storeDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "YOUR containerIdentifier")
container.persistentStoreDescriptions = [storeDescription]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
}
}
Post not yet marked as solved
import CoreData
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentContainer
init() {
container = NSPersistentContainer(name: "SiriShort")
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.SiriShortcut2")?.appendingPathComponent("SiriShort.sqlite") else {
fatalError("Shared file container could not be created.")
}
let storeDescription = NSPersistentStoreDescription(url: fileContainer)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
container.persistentStoreDescriptions = [storeDescription]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
}
}
Post not yet marked as solved
Why do you absolutely want to use predicate ?
Because I Want to use it in @FetchRequest