Firstly, sorry for my english, thats not the best, but I will try my best.
I would like to develop a Swift UI app (im beginner) on a virtual machine (XCode version: 11.7, OS: macOS Catalina version 10.15.6.) I gave the virtual machine 8 giga ram and 2 core resources. As the code grew, XCode became slower and slower. Due to the above error, the code doesn't even build anymore.
The error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions.
What exactly could be causing the error? Do u have any tips, how I can increase the speed of XCode and avoid that error? I would be very grateful if someone could help me with this problem
Code:
import SwiftUI
public struct ClockView: View {
var width = UIScreen.main.bounds.width
public var body: some View {
ZStack {
Circle()
.fill(Color.gray).opacity(0.2)
ForEach(0..<60) { idx in
VStack {
Rectangle()
.fill(Color.primary)
.opacity(1)
.frame(width: 2, height: idx % 5 == 0 ? 15 : 6)
Spacer()
}
.rotationEffect(.degrees(Double(idx) * 6))
}
Spacer(minLength: 0)
ForEach(1..<13) { idx in
VStack {
Text("\(idx)")
.font(.system(size: 25))
.offset(y: 12)
.rotationEffect(.degrees(-Double(tick)/12 * 360))
Spacer()
}
.rotationEffect(.degrees(Double(idx) * 30))
}
Spacer(minLength: 0)
/*sec*/
Rectangle()
.fill(Color.red)
.frame(width: 1, height: (width - 160) / 2)
.offset(y: (width - 160) / 4)
/*min*/
Rectangle()
.fill(Color.gray)
.opacity(0.9)
.frame(width: 2, height: (width - 200) / 2)
.offset(y: (width - 180) / 4)
/*hour*/
Rectangle()
.fill(Color.primary)
.frame(width: 3, height: (width - 220) / 2)
.offset(y: (width - 220) / 4)
Circle()
.fill(.primary)
.frame(width: 8, height: 8)
Circle()
.fill(.primary)
.frame(width: 4, height: 4)
}.frame(width: width - 80, height: width - 80) /*!!!error here!!!*/
}
}
struct ClockView_Previews: PreviewProvider {
static var previews: some View {
ClockView()
}
}
Post
Replies
Boosts
Views
Activity
I would like to stop and start the timer by a boolean value, but thats not working. isEditMode variable binded to an other view's button. I would be very grateful if someone could help me.
There is the code:
import SwiftUI
import Combine
public struct ClockView: View {
var width = UIScreen.main.bounds.width
@State var currentTime = Time(sec: 0, min: 0, hour: 0.0)
@State var timer = Timer.publish(every: 1, on: .current, in: .default).autoconnect()
@State var connectedTimer: Cancellable? = nil
@Binding var isEditMode: Bool
public var body: some View {
ZStack {
//sec
Rectangle()
.fill(Color.red)
.opacity(isEditMode ? 0 : 1)
.frame(width: 1, height: (width - 175) / 2)
.offset(y: -(width - 155) / 4)
.rotationEffect(.degrees(Double(currentTime.sec * 6)))
//min
Rectangle()
.fill(Color.secondary)
.opacity(0.9)
.frame(width: 2, height: (width - 200) / 2)
.offset(y: -(width - 180) / 4)
.rotationEffect(.degrees(Double(currentTime.min * 6)))
//hour
Rectangle()
.fill(Color.primary)
.frame(width: 3, height: (width - 220) / 2)
.offset(y: -(width - 202) / 4)
.rotationEffect(.degrees(Double(currentTime.hour * 30)))
}.frame(width: width - 80, height: width - 80)
//first call
.onAppear(perform: {
withAnimation(Animation.linear(duration: 0.01)) {
self.currentTime = getCurrentTime()
}
})
//time changes
.onReceive(timer) { i in
withAnimation(Animation.linear(duration: 0.01)) {
if self.isEditMode {
self.timer.upstream.connect().cancel()
}
else if !self.isEditMode {
self.timer = Timer.publish(every: 1, on: .current, in: .default).autoconnect()
}
self.currentTime = getCurrentTime()
}
}
}
}
func getCurrentTime() -> Time {
let calendar = Calendar.current
let sec = calendar.component(.second, from: Date())
let min = calendar.component(.minute, from: Date())
let hour: Double = Double(calendar.component(.hour, from: Date())) + Double(min) / 60
return Time(sec: sec, min: min, hour: hour)
}
struct Time {
var sec: Int
var min: Int
var hour: Double
}
struct ClockView_Previews: PreviewProvider {
static var previews: some View {
ClockView(isEditMode: .constant(false))
}
}```
Is that true, our self developed iOS app cant run in background?
I would like to develop a smart alarm clock (in swift ui or kit), which wakes you up at the optimal time based on pulse and movement data extracted from the Apple Watch.
Is that feasible or not?
I would like an analog clock, I could set the hands (rectangle) of the clock manually, using DragGesture control. But if I want to set the hand backwards immediately after clicking, it turns 180 degrees. If I want to move it forward, it works fine, but not backwards.
What could be the problem?
struct ContentView: View {
@State private var angle: Double = 0
var body: some View {
Rectangle()
.fill(Color.blue)
.frame(width: 10, height: 100)
.rotationEffect(Angle(degrees: angle), anchor: .bottom)
.gesture(
DragGesture()
.onChanged { value in
let vector = CGVector(dx: value.translation.width, dy: value.translation.height)
let radians = atan2(vector.dy, vector.dx)
let newAngle = radians * 180 / .pi
self.angle = Double(newAngle)
}
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
How to remove space occupied by text's if the isEditMode bool false? Could u help me how to do this?
struct HomeView : View {
@Binding var isDarkMode: Bool
@State var isEditMode = false
var body: some View {
GeometryReader { proxy in
//ZStack() {
VStack(/*spacing: 15*/) {
TopBarView(isDarkMode: self.$isDarkMode, isEditMode: self.$isEditMode)//.opacity(self.viewAppared ? 1 : 0)
ScrollView(.vertical, showsIndicators: true)
{
VStack {
ClockView(isEditMode: self.$isEditMode)
Text("apple").opacity(self.isEditMode ? 0 : 1)
Text("apple").opacity(self.isEditMode ? 0 : 1)
Text("apple").opacity(self.isEditMode ? 0 : 1)
}
}
}
//}
//Spacer()
}
}
}
The texts are looks like this, in the bottom of the ClockView:
Text("The alarm will ring at").opacity(isEditMode ? 1 : 0).padding(.bottom, 10)
Text(TimeMH().ToString(minuteAngle, hourAngle))
.opacity(isEditMode ? 1 : 0)
.font(.system(size: 25))
.background(Rectangle()
.fill(Color(UIColor.lightGray))
.frame(width: 90, height: 40)
.cornerRadius(10)
.shadow(radius: 5))
.opacity(isEditMode ? 1 : 0)
isEditMode: True
isEditMode: False
Why am i getting the following error: Extra argument 'selection' in call? It works without the 'selection: $selection' parameter.
XCode Version 11.7
struct AlarmListView: View {
@ObservedObject var alarmModel : AlarmModel
@State var selection: String? = nil
var body: some View {
VStack {
List {
ForEach(alarmModel.alarms, id: \.self, selection: $selection) { alarm in /*error here*/
Text("\(alarm)")
}
}
}
}
}
class AlarmModel: ObservableObject {
@Published var alarms: [String] = []
func addAlarm(_ alarmTime: String) {
alarms.append(alarmTime)
}
func removeAlarm(at index: Int) {
alarms.remove(at: index)
}
}
If I ON/OFF the darkmode with a button (by a bool value) on my view, my List elements are deleted. Not only do they disappear on the view, the list is actually becomes empty. The list declared in the AlarmModel, maybe thats the problem, I initialize an AlarmModel in the HomeView? How can I solve it?
struct ContentView: View {
@State var isDarkMode = true
var body: some View {
NavigationView {
HomeView(isDarkMode: self.$isDarkMode)
.navigationBarTitle("")
.navigationBarHidden(true)
}.preferredColorScheme(self.isDarkMode ? .dark : .light)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct HomeView : View {
@Binding var isDarkMode: Bool
@State var isEditMode = false
@ObservedObject var alarmModel : AlarmModel = AlarmModel()
@ObservedObject var clockViewModel : ClockViewModel = ClockViewModel()
var body: some View {
GeometryReader { proxy in
VStack(/*spacing: 15*/) {
TopBarView(alarmModel: self.alarmModel, clockViewModel: self.clockViewModel, isDarkMode: self.$isDarkMode, isEditMode: self.$isEditMode)
ClockView(clockViewModel: self.clockViewModel, isEditMode: self.$isEditMode)
AlarmListView(alarmModel: self.alarmModel).opacity(self.isEditMode ? 0 : 1)
}
}
}
}
}
I have an Alarm struct, there is an Bool based on this I want to switch the toggle.
struct Alarm: Hashable {
var id: Int
var alarmTime: String
var selected: Bool
@Binding var isAlarmOn: Bool
init(id: Int, alarmTime: String, selected: Bool, isAlarmOn: Binding<Bool>) {
self.id = id
self.alarmTime = alarmTime
self.selected = selected
self._isAlarmOn = isAlarmOn
}
public var alarmId: Int {
get {
return self.id
}
set {
self.id = newValue
}
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Alarm, rhs: Alarm) -> Bool {
return lhs.alarmId == rhs.alarmId
}
}
I have an AlarmModel, there is the Alarm's array.
class AlarmModel: ObservableObject {
@Published var alarms: [Alarm] = []
func addAlarm(_ alarmTime: String, _ alarmId: Int) {
let newAlarm = Alarm(id: alarmId, alarmTime: alarmTime, selected: false, isAlarmOn: .constant(false))
alarms.append(newAlarm)
}
//other functions...
}
And I have a ListView, this has the toggle that doesn't want to work the way I want it to.
struct AlarmListView: View {
@ObservedObject var alarmModel : AlarmModel
var body: some View {
List {
ForEach(alarmModel.alarms, id: \.self) { alarm in
HStack {
Toggle("", isOn: alarm.$isAlarmOn)
}
}
}
}
}
So if I use this sintax the code build, but Toggle doesn't do anything:
Toggle("", isOn: alarm.$isAlarmOn)
Else if I use this sintax: $alarm.isAlarmOn -> I get two errors:
Cannot find '$alarm' in scope
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting...
Thank you in advance for your help!
How can I fix it (Swift 13.4!)?
Code:
struct ContentView: View {
var body: some View {
Menu("Options") {
Button("1", action: func1)
Button("2", action: func2)
Button("Cancel", action: cancel)
}
}
func func1() { }
func func2() { }
func cancel() {
}
}
Is there any alternative in Swift 3 if a @State variable change?
How can I track the alarm times in the background without constantly comparing them with the current time? How to get (and calculate) live data be retrieved continuously in the background (or when the phone is locked), for example heart rate data from HealthKit?