Hello
In my previous post I asked:
https://developer.apple.com/forums/thread/689720
(Read my previous post before continuing, please)
My question now is, if I compare Dates between each other will they be formatted or not, and how would I fix this?
Thank You
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hello
What would be the best and most efficient way to save photos and videos using Core Data knowing that they will be synced using CloudKit.
Thanks
Hello
I'm trying to compose a layout using the Layout API.
I have already written the code for both the Layout Stack I want to use and the view I am using it in, however I am getting an "Extra trailing closure passed in call" error in the view I am using the Stack in.
Here is the code:
import SwiftUI
struct StairsView: View {
var body: some View {
Group{
MyStairsStack{
Text("Hello, World!")
Text("Hello, World!")
Text("Hello, World!")
}
}
}
}
struct MyStairsStack: Layout{
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout Void) -> CGSize {
return .init(width: proposal.width ?? 0, height: proposal.height ?? 0)
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Void) {
guard !subviews.isEmpty else { return }
let viewSize = maxSize(subViews: subviews)
var origin = bounds.origin
let maxWidth = bounds.width
subviews.forEach { view in
if (origin.x + (viewSize.width + 10) >= maxWidth){
origin.x = bounds.origin.x
}
view.place(at: origin, proposal: proposal)
origin.x += (viewSize.width + 10)
origin.y += (viewSize.height + 10)
}
}
private func maxSize(subViews: Subviews) -> CGSize{
subViews.map { $0.sizeThatFits(.unspecified) }.reduce(.zero) { currentMax, subviewSize in
CGSize(
width: max(currentMax.width, subviewSize.width),
height: max(currentMax.height, subviewSize.height))
}
}
}
The error is at line 5
Thank You for your time
Hello
I have two questions: I'm using a decimalpad keyboard and when I use the comma the app doesn't recognise it
How do I dismiss the keyboard?
I want that in every country the decimals are written with the comma ( , )
(Can you please write the solution if you know it, thank you for your time)
Code:
import SwiftUI
struct BMIView: View {
@State private var height = ""
@State private var weight = ""
@Environment(\.presentationMode) var presentationMode
var inputAfterConvertions: Float {
let hh = Float(height) ?? 0
let ww = Float(weight) ?? 0
var ris: Float = 0
if hh > 0 && ww > 0{
ris = (ww / (hh * hh)) * 1000
return ris
}
return 0
}
var health: String{
if inputAfterConvertions < 18.49 && inputAfterConvertions > 0 {
return ""
}
else if inputAfterConvertions > 18.5 && inputAfterConvertions < 24.99{
return ""
} else if inputAfterConvertions > 25{
return ""
}
return ""
}
@State var isWriting: Bool = false
var body: some View {
NavigationView{
Form{
Section(header: Text("Enter your height in cm")){
TextField("Input",text: $height)
.keyboardType(.decimalPad)
}
Section(header: Text("Enter your Weight in kg")){
TextField("Input",text: $weight)
.keyboardType(.decimalPad)
}
Section(header: Text("Check result")){
Text("\(inputAfterConvertions, specifier: "%.2f")")
}
}
.navigationBarTitle("BMI")
.navigationBarItems(trailing:
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "xmark").font(.title).foregroundColor(.blue)
}
)
}
}
}
Thank you very much for your time
Hello
I am getting a lot of errors in ForEach and I don't know why at all.
Here is the code:
The error is at line 12
import SwiftUI
import Combine
struct ContentView: View {
@EnvironmentObject var networkController: NetworkControllerItalia
var body: some View {
Form{
TextField("Input city name", text: $networkController.cityName)
Section {
ForEach(networkController.users.weather, id: \.self){ user in
}
}
}
}
}
class NetworkControllerItalia: ObservableObject {
private var can: AnyCancellable?
@Published var cityName: String = ""
@Published var users = [UserItalia(weather: Weather())]
init(cityName: String) {
self.cityName = cityName
let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=\(cityName)&appid=")!
self.can = URLSession.shared.dataTaskPublisher(for: url)
.map { $0.data }
.decode(type: [UserItalia].self, decoder: JSONDecoder())
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: {completion in
print(completion)
}, receiveValue: { users in
self.users = users
})
}
}
struct UserItalia: Decodable, Hashable{
var weather: Weather
}
struct Weather: Decodable, Hashable {
var main: String?
}
Thank you
Hello
I get errors in ForEach that I don't know how to solve:
Generic struct 'ForEach' requires that 'Main' conform to 'RandomAccessCollection'
Unable to infer type of a closure parameter 'user' in the current context
Error at line 30 - 31
Code:
//
// ContentView.swift
// ARWeather
//
// Created by Jad Taljabini on 08/04/21.
//
import SwiftUI
import Combine
struct ContentView: View {
@EnvironmentObject var networkController: NetworkControllerItalia
var body: some View {
NavigationView {
Form{
TextField("Input city name", text: $networkController.cityName, onEditingChanged: { te_xt in
networkController.url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(networkController.cityName)&appid=") ?? URL(string: "https://www.apple.com")!
networkController.fun()
}, onCommit: {
withAnimation{
networkController.url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(networkController.cityName)&appid=") ?? URL(string: "https://www.apple.com")!
networkController.fun()
}
})
Section {
ForEach(networkController.users.weather ?? [], id: \.self){ user in
Text(user.main ?? "")
}
ForEach(networkController.users.main ?? Main(temp: 0), id: \.self){ user in
Text("\(user.temp)")
}
}
}
}
}
}
class NetworkControllerItalia: ObservableObject {
private var can: AnyCancellable?
@Published var cityName: String = ""
@Published var users = UserItalia(weather: [])
var url = URL(string: "https://www.apple.com")!
func fun(){
self.can = URLSession.shared.dataTaskPublisher(for: url)
.map { $0.data }
.decode(type: UserItalia.self, decoder: JSONDecoder())
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: {completion in
print(completion)
}, receiveValue: { users in
self.users = users
})
}//Funzione che riempe users di dati da internet
}
struct UserItalia: Decodable, Hashable{
var weather: [Weather]?
var main: Main?
}
struct Weather: Decodable, Hashable {
var main: String?
}
struct Main: Decodable, Hashable {
var temp: Float
}
The JSON API is like this:
{
"coord": {
"lon": -0.1257,
"lat": 51.5085
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}
],
"base": "stations",
"main": {
"temp": 285.45,
"feels_like": 283.96,
"temp_min": 284.82,
"temp_max": 285.93,
"pressure": 1021,
"humidity": 47
},
"visibility": 10000,
"wind": {
"speed": 5.66,
"deg": 220
},
"clouds": {
"all": 20
},
"dt": 1617888145,
"sys": {
"type": 1,
"id": 1414,
"country": "GB",
"sunrise": 1617859180,
"sunset": 1617907473
},
"timezone": 3600,
"id": 2643743,
"name": "London",
"cod": 200
}
Thank you
Hello
I am developing an app with SwiftUI using CoreData and iCloudKit to sync data between platforms.
The problem is that the iCloud background update is not being triggered when staying in the application. If I make changes on both systems, the changes are being pushed, however not visible on the other device.
I need to reload the app, close the app and open again.
I already enabled iCloud capability, background notifications and push notifications.
This is my persistentContainer
var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "Test7")
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
return container
}()
func saveContext() {
let context = persistentContainer.viewContext
if context.hasChanges{
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
This is my model
class ItemsModel: ObservableObject {
init() {
readData()
}
@Published var dataInputs: [Item] = []
let context = persistentContainer.viewContext
func readData(){
let request: NSFetchRequest<Item> = Item.fetchRequest()
do {
let results = try context.fetch(request)
self.dataInputs = results
} catch {
print(error.localizedDescription)
}
}
func addItem(todo: String, date: Date){
let entity = NSEntityDescription.insertNewObject(forEntityName: "Item", into: context) as! Item
entity.todo = todo
entity.date = date
do {
try context.save()
self.dataInputs.append(entity)
} catch {
print(error.localizedDescription)
}
}
func deleteItems(indexSet: IndexSet){
for index in indexSet{
do {
let obj = dataInputs[index]
context.delete(obj)
try context.save()
let index = dataInputs.firstIndex(of: obj)
dataInputs.remove(at: index!)
} catch {
print(error.localizedDescription)
}
}
}
}
and this is my view
struct ContentView: View {
@EnvironmentObject var items: ItemsModel
var body: some View {
NavigationView{
List {
ForEach(items.dataInputs) { item in
Text("Item at \(item.date!)")
}
.onDelete(perform: items.deleteItems)
}
.toolbar {
Button {
items.addItem(todo: "Hello", date: Date())
} label: {
Image(systemName: "plus")
}
}
}
}
}
Thank you
Hello
Is there way to keep calling a function (efficiently) while the view is on screen, without using a timer, in SwiftUI?
Hello
I am making a To-Do list app where I use CoreData and CloudKit, the problem is that when I added this line of code
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: fileContainer.appendingPathComponent("MJ.sqlite"))]
to the PersistenceController, iCloud syncing stopped working. (I need that line of code in order to permit to extensions to access the CoreData database)
Any idea to solve the problem?
This is all the PersistenceController code
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "MJ")
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.name") else {
fatalError("Shared file container could not be created.")
}
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: fileContainer.appendingPathComponent("MJ.sqlite"))]
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
}
}
Thank you!
Hello
I noticed that in my app, when I add data (I am using Core Data) the size of the app increases, as expected, the problem occurs when I delete data, the size of the app remains unchanged and sometimes increases, I thought there was an error in the my code and so I created, from scratch, a project for iOS with SwiftUI and Core Data enabled (the default template that Xcode provides) and also with the SwiftUI & Core Data default app the same problem happens.
Is there a way to fix it or is there an explanation for this?
Thank you!
Hello
I created a custom shape in SwiftUI and I am trying to rotate it around a circle, but it works just on the top part of the circle, can you help me make it rotate exactly around the circle?
(And also can I get the same effect using radians? How?)
Here is the code:
import SwiftUI
struct MyGameView: View {
@State private var degress: Double = 0
let timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect()
var body: some View {
VStack{
ZStack{
Circle()
.frame(width: 80)
ZStack{
Circle()
.stroke(lineWidth: 1)
.frame(width: 300)
BallonShape()
.scaledToFit()
.scaleEffect(0.2)
.foregroundColor(.red)
.rotationEffect(.degrees(degress), anchor: .bottom)
.offset(x: 0, y: -170)
}
}
}
.onReceive(timer) { input in
withAnimation(.easeIn(duration: 0.05).speed(10)){
degress += 1
}
}
}
}
struct BallonShape: Shape {
func path(in rect: CGRect) -> Path {
Path { path in
path.move(to: CGPoint(x: rect.midX, y: (rect.maxY + rect.midY) / 2))
path.addCurve(to: CGPoint(x: rect.midX, y: rect.minY), control1: CGPoint(x: (rect.midX + rect.minX) / 2, y: rect.minY), control2: CGPoint(x: (rect.midX + rect.minX) / 2, y: rect.minY))
path.addCurve(to: CGPoint(x: rect.midX, y: (rect.maxY + rect.midY) / 2), control1: CGPoint(x: (rect.midX + rect.maxX) / 2, y: rect.minY), control2: CGPoint(x: (rect.midX + rect.maxX) / 2, y: rect.minY))
}
}
}
Thank You very much!
Hello
I have a list of data in SwiftUI. The data shown in the list can be saved or deleted by using Core Data.
In the @FetchRequest property that I am using to display data, I initialized an NSPredicate and in the view, I gave the possibility to the user to change the value of the predicate so that he can filter data, and that is all working, the problem shows up when I delete data from the list when I do so the predicate becomes nil and I don't know why.
Here is the code
struct SectionList: View {
@FetchRequest(
entity: LifetimeInputs.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], predicate: nil
) var lifetimeInputsModel: FetchedResults<LifetimeInputs>
@FetchRequest(entity: Limit.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Limit.date, ascending: false)]) var limit: FetchedResults<Limit>
@Environment(\.dynamicTypeSize) var dynamicTypeSize
var size: CGFloat{
if UIDevice.current.userInterfaceIdiom == .phone {
switch dynamicTypeSize {
case .xSmall: return 11
case .small: return 13
case .medium: return 15
case .large: return 17
case .xLarge: return 19
case .xxLarge: return 21
case .xxxLarge: return 23
default: return 23
}
} else {
switch dynamicTypeSize {
case .xSmall: return 13
case .small: return 15
case .medium: return 17
case .large: return 19
case .xLarge: return 21
case .xxLarge: return 23
case .xxxLarge: return 25
case .accessibility1: return 27
case .accessibility2: return 29
default: return 29
}
}
}
@StateObject var lifeTimeInputsViewModel = LifeTimeInputsViewModel()
@Environment(\.managedObjectContext) private var viewContext
var conversion: Double {
if !limit.isEmpty{
switch limit.last?.unita {
case Unit.ml.rawValue: return 1
case Unit.oz.rawValue: return 29.574
default: return 1
}
}
return 1
}
@State private var wantsToFilter: Bool = false
@State private var dateSelected = Date()
var body: some View {
Section{
HStack{
Text("Filter")
Spacer()
Image(systemName: wantsToFilter ? "checkmark.circle" : "xmark")
.font(.system(size: size + 6))
.foregroundColor(wantsToFilter ? .green : .red)
.onTapGesture {
wantsToFilter.toggle()
if wantsToFilter{
lifetimeInputsModel.nsPredicate = NSPredicate(
format: "date >= %@ && date <= %@",
Calendar.current.dateInterval(of: .day, for: dateSelected)!.start as CVarArg,
Calendar.current.dateInterval(of: .day, for: dateSelected)!.end as CVarArg
)
} else{
lifetimeInputsModel.nsPredicate = nil
}
}
}
DatePicker("Date", selection: $dateSelected, displayedComponents: .date)
} header: {
Text("Filter")
.font(.system(size: size - 4))
}
.onChange(of: dateSelected, perform: { _ in
if wantsToFilter{
lifetimeInputsModel.nsPredicate = NSPredicate(
format: "date >= %@ && date <= %@",
Calendar.current.dateInterval(of: .day, for: dateSelected)!.start as CVarArg,
Calendar.current.dateInterval(of: .day, for: dateSelected)!.end as CVarArg
)
}
})
Section{
ForEach(lifetimeInputsModel){ lifetimeInputs in
HStack{
Text("\(lifetimeInputs.valori / conversion, specifier: format(unita: !limit.isEmpty ? limit[limit.count - 1].unita ?? ml : ml)) \(!limit.isEmpty ? limit[limit.count - 1].unita ?? ml: ml)")
.font(.system(size: size))
Spacer()
Text("\(dateFormatter.string(from: lifetimeInputs.date ?? Date()))")
.font(.system(size: size))
}
}
.onDelete{lifeTimeInputsViewModel.deleteItems(offsets: $0, lifetimeInputsModel: lifetimeInputsModel); }
} header: {
Text("History \(lifetimeInputsModel.count)".localized()).font(.system(size: size - 4))
}
}
}
Thank You!
Hello
I created a simple SwiftUI app with Core Data and I want to be able to add data via the shortcuts app, I created a shortcut that takes some text as input and returns it in uppercase and when I run the shortcut in the shortcuts app, it works, however when I added an "add" function (to save data in the Core Data database) to the intent handle function, and I run it again nothing is saved in the app, here is the code:
class MakeUppercaseIntentHandler: NSObject, MakeUppercaseIntentHandling {
let persistenceController = PersistenceController()
func handle(intent: MakeUppercaseIntent, completion: @escaping (MakeUppercaseIntentResponse) -> Void) {
if let inputText = intent.text {
let uppercaseText = inputText.uppercased()
completion(MakeUppercaseIntentResponse.success(result: add(text: uppercaseText)))
} else {
completion(MakeUppercaseIntentResponse.failure(error: "The text entred is invalid"))
}
}
func resolveText(for intent: MakeUppercaseIntent, with completion: @escaping (MakeUppercaseTextResolutionResult) -> Void) {
if let text = intent.text, !text.isEmpty {
completion(MakeUppercaseTextResolutionResult.success(with: text))
} else {
completion(MakeUppercaseTextResolutionResult.unsupported(forReason: .noText))
}
}
func add(text: String) -> String{
let newItem = Item(context: persistenceController.container.viewContext)
newItem.text = text
do {
try persistenceController.container.viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return text
}
}
Thank You
Hello
Is there a view to loop different views, for example in a ForEach loop
Here is what I mean:
import SwiftUI
struct ContentView: View {
		@State var view1 = false
		@State var view2 = false
		@State var view3 = false
		@State var view4 = false
		
		private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())]
		
		
		var textSize: CGFloat {
				if UIDevice.current.userInterfaceIdiom == .pad {
						return 48
				}
				return 23
		}
		
		var title: CGFloat {
				if UIDevice.current.userInterfaceIdiom == .pad {
						return 60
				}
				return 34
		}
		
		var height: CGFloat {
				if UIDevice.current.userInterfaceIdiom == .pad {
						return 0.15
				}
				return 0.15
		}
		
		var weight: CGFloat {
				if UIDevice.current.userInterfaceIdiom == .pad {
						return 0.44
				}
				return 0.43
		}
		
		var body: some View {
				NavigationView{
						GeometryReader { geometry in
								ScrollView(.vertical, showsIndicators: true) {
										LazyVGrid(columns: gridItemLayout, spacing: 18){
												Group{
														Text("View1")
														.foregroundColor(.black)
														.frame(width: geometry.size.width * weight, height: geometry.size.height * height)
														.background(Color.white)
														.onTapGesture {
																view1 = true
														}
														
														.sheet(isPresented: $view1) {
																View1()
														}
														
														Text("View2")
														.foregroundColor(.black)
														.frame(width: geometry.size.width * weight, height: geometry.size.height * height)
														.background(Color.white)
														.onTapGesture {
																view2 = true
														}
														
														.sheet(isPresented: $view2) {
																View2()
														}
														
														Text("View3")
														.foregroundColor(.black)
														.frame(width: geometry.size.width * weight, height: geometry.size.height * height)
														.background(Color.white)
														.onTapGesture {
																view3 = true
														}
														
														.sheet(isPresented: $view3) {
																View3()
														}
														
														Text("View4")
														.foregroundColor(.black)
														.frame(width: geometry.size.width * weight, height: geometry.size.height * height)
														.background(Color.white)
														.onTapGesture {
																view4 = true
														}
														
														.sheet(isPresented: $view4) {
																View4()
														}
														
														
												}
												
										}.padding()
										
								}
								
						}
						.navigationTitle("Title")
						
				}
		}
		
		
}
Is there a way to put the 4 views in a loop instead of making four different Text Views(they are different views)
Thank your for your time
Hello
Everytime I run a project I get this popup: The document “iosfwd” could not be saved. You don’t have permission.
How do I fix it?
Thank you