简体   繁体   中英

set a variable of int into a variable of [NSNumber : Int]

I am having trouble of using the parameters in SLRequest for Twitter (as it must be a type of [NSObject : AnyObject]!, yet the Twitter API requested Int as parameters). What I do is leaving the parameter constant as [NSObject : AnyObject] and planning to declare another variable to change it to Integer. How to achieve this?

var userName : String!
var text : String!
var name : String!
var tweetID : [NSObject : AnyObject]!
var twtID : Int!

tweetID = twtID as [NSObject : AnyObject]

var userLocation : String!
var UserImgURLStr: String?

init(jsonDataDictiony : [String : AnyObject])
{

    self.text = jsonDataDictiony["text"] as! String
    self.twtID = jsonDataDictiony["id"] as! Int!

the declaration of tweetID = twtID as [NSObject : AnyObject] gives error

Or is it possible to replace the whole variable type and use Integer in the parameters?

edit 2: I have decided to change the type (after reinspecting, [NSObject : AnyObject] is only the expected argument type) into [NSNumber : Int], any idea how to hook up the int value found from the Twitter API into the variable TweetID?

var userName : String!
var text : String!
var name : String!
var tweetID : [NSNumber : Int]!
var tweetIDInt : Int!

var userLocation : String!
var UserImgURLStr: String?

init(jsonDataDictiony : [String : AnyObject])
{

    self.text = jsonDataDictiony["text"] as! String
    self.tweetIDInt = jsonDataDictiony["id"] as! Int

The declaration of tweetID = twtID as [NSObject : AnyObject] causes two errors.

  • You cannot cast an Int to a dictionary ( [NSObject : AnyObject] ).
  • You cannot execute code like this line on the top level of the class.

Assign the value for the appropriate key in a method

tweetID["id"] = twtID 

Since both text and twtID seem to exist all the time and are initialized in the init method declare them as non-optional.

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