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

69 views Asked by At

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
1

There are 1 answers

10
vadian On

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.