When you embed a variable in a string with swift

Asked 2 years ago, Updated 2 years ago, 26 views

When you embed a variable in a string with swift, for example,

let item="Golden Sword"
print("The item you got is \(item)")

You can just embed \() in the string, but somehow it doesn't work. \(()) ← Why would it not work if it had to be double-enclosed like this?

import UIKit

classViewController:UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var myPickerView: UIPickerView!
    letcompos=[["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "early morning", "morning", "daytime", "nighttime"]]

    func numberOfComponents (impickerView:UIPickerView) - > Int {
        return compos.count
    }

    funcpickerView(_pickerView:UIPickerView, numberOfRowsInComponent component:Int) - > Int {
        let component=compos [component]
        return component.count
    }

    funcpickerView(_pickerView:UIPickerView, widthForComponent component:Int) - > CGFloat{
        if component == 0 {
            return 50
        } else{
            return 100
        }
    }

    funcpickerView(_pickerView:UIPickerView, titleForRow:Int, forComponent component:Int) - > String?{
        let item=compos [component] [row]
        return item
    }

    funcpickerView(_pickerView:UIPickerView, didSelectRowrow:Int, inComponent component:Int){
        // Selected Items
        let item=compos [component] [row]
        print("\(item) selected")
        // Current selected line number
        letrow1 =pickerView.selectedRow (inComponent:0)
        letrow2 =pickerView.selectedRow (inComponent:1)
        print("Currently selected line number\(row1,row2)")

        // Currently Selected Item Name
        letitem1 = self.pickerView (pickerView, titleForRow:row1, forComponent:0)
        letitem2 = self.pickerView (pickerView, titleForRow:row2, forComponent:1)
        print("Currently Selected Item Name\((item1!,item2!))")
        print("---------------")

    }
    override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        myPickerView.delegate=self
        myPickerView.dataSource=self
    }
}

swift

2022-09-29 22:44

1 Answers

Is the example in your question around here?

print("Currently selected line number\(row1,row2)")

In the comment, you wrote "If you write an expression in \(...), the value of the expression will be embedded", where "expression" is one expression.

If you want to embed two formulas: row1 and row2, you should do the following, for example:

 print("Currently selected line number (\(row1), \(row2)")")

There are also two \(...)s because they have two embedded values.

However, your code works correctly because (...,...) is tuple(tuple).Tables are one expression, so you can embed them in \(...) to use them in the embedded string.

Similar double parentheses can also occur when calling a function.

func myFunc(_tuple:(Int,Int)){
    print("tuple=\(tuple)")
}
myFunc((1,2))//->tuple=(1,2)
myFunc(1,2)//->Global function 'myFunc' expectations a single parameter of type' (Int, Int)'

(The outer myFunc(...) is the parenthesis as a function call, and the inner (1,2) is the parenthesis for creating tuples.)

I'm ashamed that I didn't notice it until you showed me the code, but I think you understood the feeling of using a tuple as an expression.

(By the way, calling like myFunc(1,2) was even more complicated because the old Swift allowed it.)

By the way, if you want to include a code containing \ in the body, sometimes it works if you enclose it in a backquote like `...`.(\ is a quote, so sometimes it doesn't work.) Also, when you quote the entire code block, if you leave only `` (three backquotes) lines at the beginning and the end, the entire code will be shaped into a code.For your information.


2022-09-29 22:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.