Ambiguous use of append?

I am trying to make a basic calculator using Swift, but I cannot figure out how to add numbers to the top when they are clicked. I try to add a number to the line every time the button is pressed, but I get the "Ambiguous use of append" error message. What is wrong with my code that causes this error, and how can I fix it so that it functions?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var oneButton: UIButton!
    @IBOutlet weak var textField: UILabel!




    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func onePressed(sender: AnyObject) {

        textField.text?.append("1")

    }

}
+4
source share
4 answers

, Swift: append , extend . , append , extend ( , ).

String . Character, UnicodeScalar. append.

, :

image of completion options for String.append

Swift, :

textField.text?.append("1" as Character)

textField.text?.append("1" as UnicodeScalar)

, "" . "a", - String, a Character, a UnicodeScalar , , , StringLiteralConvertible (, , URL). String (append ), .

, , Character, :

let c: Character = "a"
textField.text?.append(c) // no ambiguity

Swift (.. ). + .extend(). += . arent , - String ( extend, , String), Swift String - .

+11

+ .

:

@IBAction func onePressed(sender: AnyObject)
{
    if let txt = textField?.text
    {
        textField?.text = txt + "1"
    }
}
+3

, append, extend. , :

 textField.text?.extend("1")
+2

Although the accepted answer is technically correct and very good, it seems (my guess) that all the OP attempts tried to do is add a line to a line and not add a UnicodeScalar or Character literal to the line.

If that was your intention, and I assume that most users will want to do something similar, use .appendContentsOf(other: String).

textfield.text?.appendContentsOf("1")

Also .extend()obsolete, no longer available.

0
source

Source: https://habr.com/ru/post/1570087/


All Articles