所以我已经成功声明了一些由JsonDictionary获得的变量,但是继续获得nil,这是我在声明变量时的代码:
var userName : String!
var text : String!
var name : String!
var tweetID : [NSObject : String]!
var tweetIDStr : String!
var userLocation : String!
var UserImgURLStr : String?
var options : NSJSONReadingOptions!
init(jsonDataDictiony : [String : AnyObject])
{
self.text = jsonDataDictiony["text"] as! String
self.tweetIDStr = jsonDataDictiony["id_str"] as! String
//print(jsonDataDictiony)
if let userDictionary = jsonDataDictiony["user"] as? [String : AnyObject]
{
self.userName = userDictionary["screen_name"] as! String
self.userLocation = userDictionary["location"] as! String
self.name = userDictionary["name"] as! String
}
print(self.text)
print(self.userName)
print(self.userLocation)
print(self.tweetIDStr)
print("-")
}
func castStringToDictionary()
{
let jsonData : NSData = tweetIDStr.dataUsingEncoding(NSUTF8StringEncoding)!
self.tweetID = try! NSJSONSerialization.JSONObjectWithData(jsonData, options: options) as! [NSObject : String]
}
这是尝试调用函数(不同类)时的代码:
var selectedTweet : Tweet!
var twitterNetworkController : NetworkController!
var theTweet = [Tweet]()
override func viewDidLoad()
{
super.viewDidLoad()
self.navigationItem.title = selectedTweet.name + "'s tweet"
twitterNetworkController.fetchTweetID(selectedTweet.tweetID!)
//it gets a nil value here ^
{
(results, error) in
if error == nil
{
self.theTweet = results!
self.selectedFeedTableView.reloadData()
}
else
{
print(error)
}
}
}
这是控制台打印:
[“in_reply_to_user_id”:,“possible_sensitive_appealable”:0,“favorite_count”:0,“possible_sensitive”:0,“in_reply_to_status_id”:,“in_reply_to_user_id_str”:,“lang”:en,“favorited”:0,“ id“:736420019152637952,”text“:(在Sate Tegal Marem) - url,”coordinates“:,”geo“:,”user“:{ “contributors_enabled”= 0; “created_at”=“Mon May 21 09:06:46 +0000 2012”; “default_profile”= 0; “default_profile_image”= 0; description =“\ Uad1c \ Ucc2e \ Uc744 \ Ud150 \ Ub370 \ U2b50”; entities = { description = { urls =( ); }; }; “favourites_count”= 11; “follow_request_sent”= 0; “followers_count”= 155; 跟随= 1; “friends_count”= 134; “geo_enabled”= 0; “has_extended_profile”= 0; id = 586390966; “id_str”= 586390966; “is_translation_enabled”= 0; “is_translator”= 0; lang = en; “listed_count”= 0; location =“”; name =“tania。”; notifications = 0; “profile_background_color”= B4DEBA; “profile_background_image_url”= img url; “profile_background_image_url_https”= img url; “profile_background_tile”= 0; “profile_banner_url”= banner url = img url; “profile_image_url_https”= img url; “profile_link_color”= 44A681; “profile_sidebar_border_color”= FFFFFF; “profile_sidebar_fill_color”= FFFFFF; “profile_text_color”= 333333; “profile_use_background_image”= 1; protected = 0; “screen_name”=“tania_alice”; “statuses_count”= 2332; “time_zone”=雅加达; url =“”; “utc_offset”= 25200; 已验证= 0; },“id_str”:736420019152637952,“created_at”:Sat May 28 04:53:09 +0000 2016,
我替换了所有网址,因为它给出了错误(我的声誉小于10,我无法发布网址)