casting - Cast value of type AnyObject contained in Dictionary -


I have a dictionary with a string key, which contains different types of values. It is full of a plist file. I want to assign a value to a variable by value. For some reason, I need two rows to put a value for a concrete type. For example, this code works:

  var stock idadata: [string: any object ] Type var: AnyObject? However, this does not work:  
  var stock idadata: [string: any object] type var [string: AnyObject] var type = typeTemp as string  

= Static

in the form of the stock data [["type"]] here gives the compiler error message (string, obozage) not variable for string. However, , Because the above code does the same thing in two lines of code code.

Can anyone explain to me why this is the matter? Besides, what is another great way to do this?

The reason is that the string AnyObject does, but NSString does when you do it in two steps Quickly able to put any unknown object in NSString and then pulling it into a fast string There are two ways you can find around this.

The easiest way to make your dictionary a [string: anni] Since the string corresponds to any one, so you can insert any as any string. ["Type": "Hello"] as the

var type = stock item data ["type"]

  var stock item data: if If you are not able to change the type of your dictionary, you can still cast two steps in a row and remove the need for a temporary variable.  
  var stock idadata: [string: AnyObject] = ["type": "hello"] var type = stockitamadata ["type"] as AnyObject? String as  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -