简体   繁体   中英

Swift: using the value of a ui element in a dictionary

I have a dictionary params as below

var params : Dictionary<String,AnyObject> = [
 "name"          :   self.name.text
]

I can assign self.name.text to a variable and see it has a value.

but when i do

 println(params)

I get a EXC_BAD_ACCESS at runtime

i can fix it with "as String" as below

var params : Dictionary<String,AnyObject> = [
    "name"          :   self.name.text as String
]

Does anyone know why i need the cast to stop the EXC_BAD_ACCESS?

From the print documentation :

The value you supply for object must conform to the Printable or DebugPrintable protocol:

So I would expect that NSString does not conform to any of these protocols.

The following code also generates the same error

var str = NSString()
println(str as String) // fine
println(str) // fails

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM