0

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
Ronald Aja
  • 139
  • 1
  • 2
  • 8

1 Answers1

0

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.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • See the second reason: You have to put the code into a method. – vadian May 26 '16 at 14:18
  • I'm sorry, I am still new in Swift and even though your code works well, it shows up nil in the tweetID and thus breaking the process. I reverted the whole code to only using tweetID as Int. I will edit my question to the newer part of my app in which the error initially exists. – Ronald Aja May 26 '16 at 14:35
  • The reason of your edited question is reason 1: **You cannot cast (and assign) an `Int` to a dictionary** because the types are different. – vadian May 26 '16 at 14:41
  • Yes I realise that @vadian , but what can I do to overcome this? – Ronald Aja May 26 '16 at 14:45
  • Your code does not show where and how `twtID` is used so nobody (except you) can know what the variable is supposed to do. – vadian May 26 '16 at 14:47
  • I use twtID solely for converting id from Int (the API requirement) into NSObjects : AnyObjects (SLRequest.parameter requirement). If its redundant, can you show me how to do it? @vadian – Ronald Aja May 26 '16 at 14:55
  • `id` is an `Int` and a dictionary is a collection type containing key/value pairs. I wrote in my answer how to set a value (`twtID`) in a dictionary for a specific key (`"id"`). – vadian May 26 '16 at 14:59
  • Hey @vadian, I have edited my question (and also my code), would you please check on this? – Ronald Aja May 27 '16 at 12:55
  • What's that `[NSNumber : Int]!` for? It's also a dictionary and it's not used in the edited code. Please read the chapter about collection types in the Swift Language Guide to understand the function of a dictionary. PS: Meanwhile the question is confusing because of all the editing. – vadian May 27 '16 at 13:02
  • Yes I never added the part of why I am using NSNumber instead of Integer blindly (its in a different class), as it would be a very wild code and I am not sure of how to share it properly @vadian – Ronald Aja May 27 '16 at 13:03