For example:
var myArray = ["first string","second string"]
myArray.append(35) // Here will be an error
If you Option-click on myArray, you will see that it is of type [String] (array of String). You can only add Strings to myArray.
If you want to be able to add Int to your array, you will need a more inclusive type. If you make your array [Any], you can put anything in it.
var myArray:[Any] = ["first string","second string"]
myArray.append(35) // This now works
If you want to store only Int and String, you can create your own custom protocol:
protocol StringsAndInts {
}
extension String: StringsAndInts {
}
extension Int: StringsAndInts {
}
var myArray:[StringsAndInts] = ["first string","second string"]
myArray.append(35)
print(myArray) // ["first string", "second string", 35]
myArray.append(3.14) // error: argument type 'Double' does not conform to expected type 'StringsAndInts'
If you write
var myArray = ["first string","second string"]
print(myArray.dynamicType)
You will see that myArray is of type Array<String> or [String]. You cannot append an Int to that.
What you need to do is specify the type of the myArray yourself:
var myArray : [AnyObject] = ["first string","second string"]
print(myArray.dynamicType) // now prints Array<AnyObject> which you can add Int to
myArray.append(35)
[String], with elements of typeString. Hence, swift expects new members of the array to be, also, of typeString, whereas35above is an integer literal. TrymyArray.append("35")ormyArray.append(String(35) ?? "").[Any], I believe, and you can append 35. But this way you lose type information.