Strange Bool Type from UI

I have an array of (String,Bool)tuples:

var names : [(name:String, important:Bool)] = [("Paul",true), ("Peter",false)]

I also have a control UISwitch:

@IBOutlet weak var importantSwitch: UISwitch!

I want to add to the array using the state of the switch to determine Bool.

The following does not work:

names.append( name: "whatever", important: importantSwitch.on )

The problem reported by Xcode is "Type T does not conform to the IntegerLiteralConvertible protocol."

They work:

let i : Bool = importantSwitch.on
names.append( name: "whatever", important: i )

or

names.append( name: "whatever", important: importantSwitch.on==true )

My question, in fact, is why?

+4
source share
1 answer

Simple and satisfying rewriting: casting

names.append(name: "whatever", important: importantSwitch.on as Bool)

Even better, if you do not want to perform any casting or assignment dance, use extendinstead append:

names.extend([(name: "whatever", important: importantSwitch.on)])

Martin R, , :

typealias Pair = (name:String, important:Bool)
var names = [Pair]()
names.append(Pair(name: "whatever", important: importantSwitch.on))

, , extend , append , : . , , - , , , .

, names.append(...), - append, name: important: - , ? , , . , , ; , , ( ). extend , . .

+2
source

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


All Articles