Forced conversion of array types in Swift

topic description

Is [String] in the picture below

a cast? It feels awkward!

related materials

clipboard.png

what result do you expect? What is the error message actually seen?

help me analyze why it is written this way. Is it okay not to write [String] ?

Jun.13,2022

The [String] in the picture below

is not cast.
is an initialization with the elements in the dict.

not writing [String] is not allowed.
does not write [String]. Codes is not of type [String], but of type Dictionary < String, String > .Keys.

one

[String] [String]

two

because [String] is not written, it is a shallow copy in Swift. The memory that codes points to, or the keys of dict.
writes [String], which means that codes is re-allocated a piece of memory, and the operation against him has nothing to do with the original dict.

if Swift is not designed in this way, every time you change codes, it is copy on write. Semantically, if it is not clear, it is not safe

.

the code is as follows:

let dict = ["one":"" , "two": ""]

var keys = dict.keys

var keysTwo = [String](dict.keys)

keysTwo = ["a"]

print(dict)

print(keysTwo)

this is grammar. If you don't write it, it's Any.


No, the syntax in Swift specifies what type to store when Array is created. If you haven't decided to declare it with Any or AnyObject, this is not a cast

.

if you study swift carefully, you will find that it is really a rigorous language. It thinks that developers should know and be clear about the possible results. If there are omissions, it will force an error prompt. OC often leads to some unexpected results due to a lot of implicit conversions.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-7c3da7-2ab47.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-7c3da7-2ab47.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?