Post

Replies

Boosts

Views

Activity

Reply to Append to Double
Yes it is, but not this way. You need to convert first to String, then append then convert back to Double. Such as: var num: Double = 1 let numString = String(num) let numStrPlus = "1" + numString var num2 = Double(numStrPlus)! // num.append(1) print(num2) You can have a more condensed form: var num: Double = 1 var num2 = Double("1" + String(num)) ?? 0 or var num2 = Double(String(1) + String(num)) ?? 0 If you really want to write it as append, you can define an extension to Double: extension Double { func append(_ val: Int) - Double { return Double(String(val) + String(self)) ?? 0 } } num2 = num.append(1)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to Comma not working
You have defined virgola as dot. case virgola = "." Is it intentional ? May be you should define: case virgola = "," case punto = "." and add punto to numeri However, I tested your code, it seems to work. How do you enter comma ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to Comma not working
it doesn't consider the numbers that I write after the decimal point. I tapped: 8 . 5 X 2 and get 17. All works. Except that it displays 0 when tapping X. But it doesn't work when decimal in the second operand.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to Comma not working
I instrumented your code and there is a design flaw: var body: some View { VStack{ Spacer() HStack{ Spacer() Text("\(digits)") .font(.system(size: 50)) .bold() .padding() } Spacer() LazyVGrid(columns: gridItemLayout, content: { ForEach(0..5){ i in ForEach(0..4){ j in RoundedRectangle(cornerRadius: 35.0) .foregroundColor(.orange) .frame(width: 80, height: 80, alignment: .center) .overlay( Text("\(numeri[i][j].rawValue)") .font(.largeTitle) .foregroundColor(.black) ).onTapGesture { switch numeri[i][j] { case Nums.AC: operat = Nums.AC; res = 0 case Nums.uguale: operat = Nums.uguale; res = final print("uguale", "final", final, "digits", digits) case Nums.somma: operat = Nums.somma; previousVar = res; res = 0; con = 0 print("somma", "previousVar", previousVar, "digits", digits) case Nums.meno: operat = Nums.meno; previousVar = res; res = 0 con = 0 case Nums.divisione: operat = Nums.divisione; previousVar = res; res = 0; con = 0 case Nums.moltiplicazione: operat = Nums.moltiplicazione; previousVar = res; res = 0; con = 0 case Nums.percentuale: operat = Nums.percentuale; res = res / 100 case Nums.piùMeno: operat = Nums.piùMeno; if digits.hasPrefix("-") { digits = String(digits.dropFirst()) } else { digits = "-" + digits } con = 0 case Nums.virgola: operat = Nums.virgola; print("res", res, "con", con, "digits", digits) if !digits.contains(".") { digits += "." } default: if digits == "0" { digits = numeri[i][j].rawValue } else { digits += numeri[i][j].rawValue } con += 1 print("default res", res, "con", con, "digits", digits) } } } } }).onReceive(timer) { _ in if con != 0 { if operat == Nums.divisione{ final = previousVar / res } else if operat == Nums.meno{ final = previousVar - res } else if operat == Nums.moltiplicazione{ final = previousVar * res } else if operat == Nums.somma{ final = previousVar + res print("final", final, "res", res, "previous", previousVar, "digits", digits) } } I type : 3.8 + 2.1 = I get: default res 3.0 con 1 digits 3 res 3.0 con 1 digits 3 default res 3.8 con 2 digits 3.8 somma previousVar 3.8 digits 0 default res 2.0 con 1 digits 2 final 5.8 res 2.0 previous 3.8 digits 2 … this line repeated because of timer final 5.8 res 2.0 previous 3.8 digits 2 res 2.0 con 1 digits 2 default res 2.1 con 2 digits 2.1 uguale final 5.8 digits 5.8 This shows that the computation: final = previousVar + res print("final", final, "res", res, "previous", previousVar, "digits", digits) is done before the second operand with the decimal is completely read into res: it is read after the computation. Note: with the names of your var (what is con ?) it is extremely difficult to grasp your code. Why don't you save the 2 operands and just compute (as you would do with a reverse polish notation) ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to Question about laying out text on a fairly static screen
Stackviews are really a pain to manage with constraints. I often avoid them. the children should be a combination of aligned labels to the right describing data to the left, and some monospaced text. Is the number of those children objects (labels and text) fixed or created dynamically ? If fixed, I would try do proceed like this (You did not givet a precise description, so I cannot fully detail) In the VC Create a view which is as high as needed (even more than the screen) to contain all sections declare a constraint for its height and create an IBOutlet for this constraint create subviews for each section create labels and text in this subview and set constraints for each object, notably vertical constraints with the top label constrained to the subview top and the bottom object constrained to the bottom of the view. for the object that should have dynamic height, create a constraint on its height, with an IBOutlet Adjust constraints as needed in code. Note: setting constraints for ScrollView is a bit tricky. I wrote a short notice for my own use and I always follow step by step. Create a ScrollView Define its dimensions by constraints relating to the Safe area leading = 0 trailing = 0 top = maybe non zero if you scroll a part of the screen bottom = 35 (here to leave room for some buttons below) you can keep the content layout and frame layout ON Create a UIView (this is the content View, named View here) Place it inside the scrollView (appears inside Scroll in the object hierarchy) Define the constraints with respect to the scrollView (Superview) leading = 0 trailing = 0 equalWidth with Scroll: we don't want to scroll horizontally top = 0 bottom = 0 Above all, do NOT make equal height with Scroll: there would be no more vertical scroll Define the dimension constraints of the ContentView to correspond to the complete content above which we will scroll (here the 4 items and the 3 switches) height = 262 width has been set equal to the width of the scrollView Note: this is necessary to eliminate red warnings from IB Place objects in the ContentView for each Define the constraints in relation to the ContentView.
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’21
Reply to Why does my Timer get paused sometimes?
Timers are not made to work in background, except some specific case. See: https://stackoverflow.com/questions/42319172/swift-3-how-to-make-timer-work-in-background and h ttps://www.raywenderlich. com/5817-background-modes-tutorial-getting-started See this for more in depth discussion: https://developer.apple.com/forums/thread/127444
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Getting notified when the user pays with Apple Pay ?
The app could request access to the user's Apple Pay activity. The user would have to grant the access. I don't see a "privacy" breach... Yes it could, if Apple allowed to do so. Hence my point. My understanding is that Apple does not allow this, for privacy reason. The topic has been discussed in several threads. https://developer.apple.com/forums/thread/25677
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21