History in calculator (Swift) - ios

I have code for running the history in the application.
My app screenshot:
How can I improve it so that the numbers are displayed immediately when pressed (as shown in the video), and not just by pressing =.
// Connected to button "="
#IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false
addHistory(text: operationSign + displayResultLabel.text!)
switch operationSign {
case "+":
operateWithTwoOperands{$0 + $1}
case "-":
operateWithTwoOperands{$0 - $1}
case "×":
operateWithTwoOperands{$0 * $1}
case "÷":
operateWithTwoOperands{$0 / $1}
default: break
}
}
func addHistory(text: String){
//Add text
resultLabelText.text = resultLabelText.text! + "" + text
}

One option could be to define a separate variable for the label string that is constantly updated using calls to addHistory() after every UIButton press (number or operator), and then the updating of the label itself handled by didSet inside the variable definition:
var resultLabelString: String = "" {
didSet {
self.resultLabelText.text = self.resultLabelText.text! + "" + resultLabelString
}
}
func addHistory(text: String){
self.resultLabelString = text
}

Related

Detect when a button is pressed outside of the IBAction in Swift?

I'm a beginner programmer making my first real app, a calculator in Swift.
It's mostly working, but one issue I'm running into is how it displays numbers after pressing one of the operator buttons. Currently, whenever an operator button is pressed, I have it set the label at the top of the calculator to "0". But on actual calculators, this top display won't change until another number button is pressed.
If I don't reset the display to 0, then any number buttons that are pressed will add to the current text at the top, and mess up the equation that the calculator will have to do (i.e. 2+2 displays 22, and the solution it displays is 22+2=24)
I'm wondering if it's possible to detect when one of the number buttons is pressed (listed in my code as the intButtonPressed IBAction) outside of the intButtonPressed function? That way I can keep the top label the same until the user starts inputting more text, then I can set it to 0 to prevent the calculator from breaking.
Any other possible (better) solutions would be welcome as well
Here's my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var topLabel: UILabel!
var num1 = Double()
var solution = Double()
var op = String()
#IBAction func intButtonPressed(_ sender: UIButton) {
if topLabel.text == "0" {
topLabel.text = sender.currentTitle
}
else if topLabel.text == String(solution) {
num1 = Double(topLabel.text!)!
solution = 0.00
topLabel.text = sender.currentTitle
// Basically stops user from adding to solution?
// Also stores solution as num1 and clears solution field
}
else {
topLabel.text = topLabel.text! + sender.currentTitle!
// Keep typing
}
if (topLabel.text?.count)! > 12 {
topLabel.text = "Error"
}
else {
return
}
// if its greater than 12 characters, display "error"
}
#IBAction func clearButtonPressed(_ sender: UIButton) {
num1 = 0.00
solution = 0.00
topLabel.text = "0"
}
#IBAction func opButtonPressed(_ sender: UIButton) {
if sender.currentTitle == "=" {
equals()
}
else {
op = sender.currentTitle!
num1 = Double(topLabel.text!)!
topLabel.text = "0"
}
// Need it to display num1 until I press another button, then I need it to only display that text.
}
func equals() {
switch op {
case "+":
add()
case "-":
subtract()
case "×":
multiply()
case "÷":
divide()
default:
print(Error.self)
}
}
func add() {
solution = num1 + Double(topLabel.text!)!
topLabel.text = String(solution)
}
func subtract() {
solution = num1 - Double(topLabel.text!)!
topLabel.text = String(solution)
}
func multiply() {
print("topLabel = ", topLabel.text!)
solution = num1 * Double(topLabel.text!)!
print("solution = ", solution)
topLabel.text = String(solution)
}
func divide() {
solution = num1 / Double(topLabel.text!)!
//answer()
}
}
Update for anyone who finds this in the future: I've solved the issue, and realized that I wasn't very clear with what I wanted it to do. I solved the problem simply by adding a condition to the if/else statement in the inButtonPressed function that detects if the topLabel is 0. By rewriting that to detect if the topLabel is 0 OR the same as num1, and then changing the else statement in the opButtonPressed function to set topLabel to String(num1), the program does exactly what I want. Here's the rewritten code:
#IBAction func intButtonPressed(_ sender: UIButton) {
if topLabel.text == "0" || topLabel.text == "0.0" || topLabel.text == String(num1){
dotPressed = false
// Resets dotPressed whenever the top is 0 or num1 and an int button is pressed
topLabel.text = sender.currentTitle
}
else if topLabel.text == String(solution) {
num1 = Double(topLabel.text!)!
solution = 0.00
topLabel.text = sender.currentTitle
// Basically stops user from adding to solution?
// Also stores solution as num1 and clears solution field
}
else {
topLabel.text = topLabel.text! + sender.currentTitle!
}
}
#IBAction func opButtonPressed(_ sender: UIButton) {
if sender.currentTitle == "=" {
equals()
}
else {
op = sender.currentTitle!
num1 = Double(topLabel.text!)!
topLabel.text = String(num1)
}
// Successfully displays num1 until I press another button, then only displays that text.
I also added a separate function to handle decimals, and I had to update the code in there as well to keep that working.

String letter by letter animation get mixed up when retrieving the next text

I have a pop up that is supposed to show the user instructions with letter by letter animation. The problem is whenever the user clicks "next" the letters get mixed up with the previous text.
The animation code:
extension UILabel {
func animate(newText: String, characterDelay: TimeInterval) {
DispatchQueue.main.async {
self.text = ""
for (index, character) in newText.enumerated() {
DispatchQueue.main.asyncAfter(deadline: .now() + characterDelay * Double(index)) {
self.text?.append(character)
}
}
}
}
The button action/calling method (as you can see I tried to empty the variable each time the user clicks "next" but it didn't work out)
#IBAction func nextbtt(_ sender: Any) {
var instructions = ["text"]
counter = counter + 1
var w1 = " لكن الوصول إليه يتطلب مواجهة وحل تحديات مختلفة"
var w2 = "هل بإمكانك مساعدتي في الحصول على الكنز؟"
let userId = UserDefaults.standard.object(forKey: "userId") as? String
ref = Database.database().reference()
let userLang = ref.child("users").child(userId!).child("lang")
userLang.observeSingleEvent(of: .value, with: { (snapshot) in
let lang = snapshot.value as? Int
if(lang==1){
////////////// if user's langague is English
w1 = "But finding it requires confronting and solving different challenges"
w2 = " Could you help me in getting the treasure" }
instructions.append(w1)
instructions.append(w2)
if(self.intrCounter < 3){
self.mytext.text = ""
var new = instructions[self.intrCounter]
self.mytext.text = new
self.mytext.animate(newText: new ?? "May the source be with you", characterDelay: 0.1)
self.intrCounter = self.intrCounter + 1
if(self.intrCounter == 3){
if(lang==1){
(sender as AnyObject).setBackgroundImage(UIImage(named: "engready"), for: UIControl.State.normal)
}
else {
(sender as AnyObject).setBackgroundImage(UIImage(named: "ready"), for: UIControl.State.normal)}
}
}
else{
}
})
if ( counter == 4){
status[0] = true
popUp.removeFromSuperview()
}
}
Screenshots:
one: text is showing to the user, user clicks "next" at the middle of the animation
two: when user clicks next before text 1 is complete
Code:
The issue occurs because you are calling an asynchronous block for each character and probably each char takes another amount of time.
Just to test this try this change, if it will help change the code accordingly :
var someCounter = 1
extension UILabel {
func animate(newText: String, characterDelay: TimeInterval) {
DispatchQueue.main.async {
self.text = ""
for (index, character) in newText.enumerated() {
someCounter += 1
DispatchQueue.main.asyncAfter(deadline: .now() +
someCounter + characterDelay * Double(index)) {
self.text?.append(character)
}
}
}
}
Let me know if this is indeed the issue, if so I will upload a more optimised code.

Pictures in the label (Swift)

I have code to display the history in the calculator but the signs (+, -, ×, ÷) are taken from the "case" (Photo 1)
How can I make it so that in the history the signs (+, -, ×, ÷) are displayed by the pictures I have set (Photo 2)
#IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false
addHistory(text: operationSign + displayResultLabel.text!)
switch operationSign {
case "+":
operateWithTwoOperands{$0 + $1}
case "-":
operateWithTwoOperands{$0 - $1}
case "×":
operateWithTwoOperands{$0 * $1}
case "÷":
operateWithTwoOperands{$0 / $1}
default: break
}
}
History:
func addHistory(text: String){
//Add text
resultLabelText.text = resultLabelText.text! + "" + text
}
You can make your symbols images and use NSTextAttachment to construct a NSAttributedAtring that replaces the text in your string with the corresponding NSTextAttachment with your symbol image. Here is an example playground that does it with one image, but you can easily add more images to the dictionary to replace all of the other symbols with images:
import PlaygroundSupport
import UIKit
class V: UIViewController {
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
label.textColor = .red
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let expression = "5 + 5"
let plusAttachment = NSTextAttachment()
plusAttachment.image = UIImage(named: "star.png")
let plusString = NSAttributedString(attachment: plusAttachment)
let substitutions: [Character: NSAttributedString] = ["+": plusString]
let attributedExpression = NSMutableAttributedString()
for character in expression {
if let substitution = substitutions[character] {
attributedExpression.append(substitution)
} else {
attributedExpression.append(NSAttributedString(string: String(character)))
}
}
label.attributedText = attributedExpression
label.sizeToFit()
}
}
PlaygroundPage.current.liveView = V()
I suggest you get an emoji font that displays mathematical signs with a border around them. Use that font to create NSAttributedStrings and set it as the label's attributedText.
As to how to use custom fonts, you can refer to here or just search on SO. There are lots of questions about this topic.
I also see that you want the text to be bold, that can be done with attributed strings as well.
Alternatively, you can add those cool math signs as attachments to NSAttributedStrings, but I doubt it's easy to get the sizes correct.

IBAction sometimes not being called when touching buttons - Swift

I am trying to develop a word game where players click buttons to select letters.
There seems to be problem where my buttons sometimes do not register touches. It only seems to occur if there is a pause for a few seconds with no user interaction before a button touch. If the first touch works, quick follow up touches also work.
#IBAction func tileButton1(_ sender: UIButton) {
print("Tile 1 Selected")
tileSelected(tileSelected: 1)
}
#IBAction func clearButton(_ sender: Any) {
clearSelectedTiles()
}
#IBAction func SubmitButton(_ sender: Any) {
//print("Submit Button Pressed")
checkIfSubmittedWordIsValid()
}
checkIfSubmittedWordIsValid
func checkIfSubmittedWordIsValid() {
var alreadySelectedWords: [String] = []
switch currentPlayer {
case 1:
alreadySelectedWords = player1words
case 2:
alreadySelectedWords = player2words
case 3:
alreadySelectedWords = player3words
case 4:
alreadySelectedWords = player4words
default:
break
}
if currentWord.characters.count < 3 {
print("Too short")
playSound(fileName: "invalidWord", fileExtension: "aiff", volume: 1.0)
} else if alreadySelectedWords.contains(currentWord) {
print("Already picked this word")
playSound(fileName: "invalidWord", fileExtension: "aiff", volume: 1.0)
} else if wordList.contains(currentWord.lowercased()) {
print("Valid Word")
playSound(fileName: "goodWord", fileExtension: "wav", volume: 0.5)
addWordToPlayerList(word: currentWord)
} else {
print("Not in dictionary")
playSound(fileName: "invalidWord", fileExtension: "aiff", volume: 1.0)
}
clearSelectedTiles()
}
clearSelectedTiles
func clearSelectedTiles() {
tile1.alpha = 1
tile2.alpha = 1
tile3.alpha = 1
tile4.alpha = 1
tile5.alpha = 1
tile6.alpha = 1
tile7.alpha = 1
tile8.alpha = 1
tile9.alpha = 1
tile10.alpha = 1
tile11.alpha = 1
tile12.alpha = 1
tile13.alpha = 1
tile14.alpha = 1
tile15.alpha = 1
tile16.alpha = 1
selectedTiles.removeAll()
validTiles = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
selectedWordLabel.text = ""
currentWord = ""
}
Nothing gets printed when the issue occurs. Following up quickly with a second touch will trigger the IBAction and print to log.
It seems to happen also with all my other buttons (another 15 'tile' buttons and a 'Clear' and 'Submit' button)
What am I doing wrong?
Link to video showing issue First few touches work but then weirdness.
tileSelected
func tileSelected(tileSelected: Int) {
if isTileValid(tile: tileSelected) {
selectedTiles.append(tileSelected)
var surroundingTiles: [Int] = []
switch tileSelected {
case 1:
tile1.alpha = 0.5
surroundingTiles = [2,5,6]
case 2:
tile2.alpha = 0.5
surroundingTiles = [1,3,5,6,7]
case 3:
tile3.alpha = 0.5
surroundingTiles = [2,4,6,7,8]
case 4:
tile4.alpha = 0.5
surroundingTiles = [3,7,8]
case 5:
tile5.alpha = 0.5
surroundingTiles = [1,2,6,9,10]
case 6:
tile6.alpha = 0.5
surroundingTiles = [1,2,3,5,7,9,10,11]
case 7:
tile7.alpha = 0.5
surroundingTiles = [2,3,4,6,8,10,11,12]
case 8:
tile8.alpha = 0.5
surroundingTiles = [3,4,7,11,12]
case 9:
tile9.alpha = 0.5
surroundingTiles = [5,6,10,13,14]
case 10:
tile10.alpha = 0.5
surroundingTiles = [5,6,7,9,11,13,14,15]
case 11:
tile11.alpha = 0.5
surroundingTiles = [6,7,8,10,12,14,15,16]
case 12:
tile12.alpha = 0.5
surroundingTiles = [7,8,11,15,16]
case 13:
tile13.alpha = 0.5
surroundingTiles = [9,10,14]
case 14:
tile14.alpha = 0.5
surroundingTiles = [9,10,11,13,15]
case 15:
tile15.alpha = 0.5
surroundingTiles = [10,11,12,14,16]
case 16:
tile16.alpha = 0.5
surroundingTiles = [11,12,15]
default:
// do nothing
break
}
updateValidTiles(surroundingTiles: surroundingTiles)
//print("Updated Valid Tiles")
//print(validTiles)
//print("Selected Tiles")
//print(selectedTiles)
currentWord = currentWord + boardTiles[tileSelected - 1].tileLetter
selectedWordLabel.text = currentWord
}
}
Try changing touch down to touch up inside, and try changing the state of the button each time it is clicked
I had some labels constrained to the Top Layout Guide.bottom even though I was hiding the status bar with override var prefersStatusBarHidden.
Changing the relevant label constraints to topMargin instead of Top Layout Guide.bottom has solved my problem with touches sometimes being missed.

CS193P Assignment 1 π

I'm studying CS193P course on iTunesU. I have a question related to the 1st assignment - Programmable Calculator. I've attempted to add the π button as described in the lectures and the homework assignment. However, pressing the π key followed by enter or and operand causes a crash with the message: "fatal error: unexpectedly found nil while unwrapping an Optional value"
class CalculatorBrain
{
private enum Op {
case Operand(Double)
case NullaryOperation(String, () -> Double)
case UnaryOperation(String, Double -> Double)
case BinaryOperation(String, (Double,Double) -> Double)
var description: String{
get {
switch self {
case .Operand(let operand): return "\(operand)"
case .NullaryOperation(let symbol, _): return symbol
case .UnaryOperation(let symbol, _): return symbol
case .BinaryOperation(let symbol,_):return symbol
}
}
}
}
private var opStack = [Op]()
private var knownOps = [String:Op]() //initialize dictionary
init() {
func learnOp (op: Op) {
knownOps[op.description] = op
}
learnOp(Op.BinaryOperation("×", *))
learnOp(Op.BinaryOperation("÷", { $1 / $0 }))
learnOp(Op.BinaryOperation("+", +))
learnOp(Op.BinaryOperation("−", { $1 - $0 }))
learnOp(Op.UnaryOperation("√", sqrt))
learnOp(Op.UnaryOperation("sin", sin))
learnOp(Op.UnaryOperation("cos", cos))
learnOp(Op.NullaryOperation("π", { M_PI }))
}
I've been able to force it in the view controller, but know this is a hack:
var displayValue: Double{
get{
// I don't understand why I had to put this hack in for π
// if (calcDisplay.text != "π"){
return NSNumberFormatter().numberFromString(calcDisplay.text!)!.doubleValue
// } else {
// return M_PI
// }
}
set{
calcDisplay.text = "\(newValue)"
userIsInTheMiddleOfTyping = false
}
}
I'm new to Swift / Obj-C. Can someone please help point me in the right direction to resolve this?
Full source: https://github.com/philnewman/Calculator
#IBAction func appendPi(sender: UIButton) {
let x = M_PI
if userIsInTheMiddleOfTypingANumber {
displayValue = x
display.text = "\(displayValue)"
}else {
displayValue = x
userIsInTheMiddleOfTypingANumber = true
enter()
}
}
Created a new UIButton function for PI separate from the other functions. Working for me, give it a go.
NSNumberFormatter().numberFromString is designed to convert numerical strings into numbers. A "numerical string" is a string whose contents are of the form:
([:digit:]+(\.[:digit:]*)?)|(\.[:digit:]+)
Where [:digit:] is the set of digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
Essentially, it will only parse numbers from strings whose characters are within 0-9 and an optional ..
Because π doesn't belong to this set of 10 characters, it is rejected and the NSNumberFormatter is unable to parse a number from the string.
I am doing the same course, but as I am not yet at the point of separating my MVC (will do that in the next assignment), I can only give you a hint where you go wrong. As it works for me....
Your hack is testing calcDisplay.text as being "π", however at that moment calcDisplay.text should already be 3,14159.
That is what I checked with a println on my working assignment.
So I can show you part of my code which is not optimal for MVC yet:
case "cos": performOperation{cos($0)}
case "∏": performConstant(operation)
func performOperation (operation: (Double) -> Double) {
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
func performConstant (symbol: (String)) {
switch symbol {
case "∏": displayValue = M_PI
default: break
}
display.text = "\(displayValue)"
enter()
}
I think it can be more optimal too, but it might help you with finding the bug in your code.
I am also doing same course. I did solved "." and "pie" question. See if this help you.
#IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if digit == "." {
if (display.text!.rangeOfString(".") == nil) {
display.text = display.text! + "."
} else {
//Don't display anything
}
} else if digit == "∏" {
displayValue = pie
} else {
if userIsInTheMiddleOfTypingANumber {
display.text = display.text! + digit
} else {
display.text = digit
userIsInTheMiddleOfTypingANumber = true
}
}
}

Resources