我一直在开发一个应用程序来利用谷歌的 YouTube API。我让这个应用程序在 Objective-C 中完全正常运行,但我在 Swift 中遇到了一些问题。
这是在 Objective-C 中的样子,
-(void) retrieveData {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//Change this link in order to get the data you want Change the Username!!!
[manager GET:[NSString stringWithFormat:@"https://gdata.youtube.com/feeds/api/users/%@/uploads?v=2&alt=jsonc", _YoutuberName] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//Parsing the JSON into something readable
self.posts = (NSDictionary *)responseObject;
self.post = self.posts[@"data"][@"items"];
title = [[self.post valueForKeyPath:@"title"]count];
// Reloading the Data in the Table
[self.tableView reloadData];
[self.refreshControl endRefreshing];
NSLog(@"%@", self.post);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
这就是我目前在 Swift 中所拥有的,
var posts = NSDictionary()
var post = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var manager = AFHTTPRequestOperationManager()
manager.GET("https://gdata.youtube.com/feeds/api/users/archetapp/uploads?v=2&alt=jsonc", parameters: nil, success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
self.posts = NSDictionary(objects: [responseObject], forKeys: ["data"])
NSLog("\(self.posts)")
}, failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
然后它打印这个...(这与 Objective-C 完全相同,但 Obj-C 从它说“项目”的地方开始)
data = {
apiVersion = "2.1";
data = {
items = (
{
accessControl = {
autoPlay = allowed;
comment = allowed;
commentVote = allowed;
embed = allowed;
list = allowed;
rate = allowed;
syndicate = allowed;
videoRespond = moderated;
};
aspectRatio = widescreen;
category = Education;
commentCount = 10;
content = {
1 = "rtsp://r4---sn-a5m7zu7s.c.youtube.com/CigLENy73wIaHwl-BISTGI_oVhMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp";
5 = "https://www.youtube.com/v/VuiPGJOEBH4?version=3&f=user_uploads&app=youtube_gdata";
6 = "rtsp://r4---sn-a5m7zu7s.c.youtube.com/CigLENy73wIaHwl-BISTGI_oVhMYESARFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp";
};
description = "Have an awesome day! #DFTBA\n\nSupport me on Patreon : Patreon.com/archetapp\n\nSubscribe to my channel! - http://www.youtube.com/archetapp\n\nCheck out my Website! - http://www.archetapp.com\nCheck me out on Twitter - http://www.twitter.com/archetapp";
duration = 437;
favoriteCount = 0;
id = VuiPGJOEBH4;
likeCount = 21;
player = {
default = "https://www.youtube.com/watch?v=VuiPGJOEBH4&feature=youtube_gdata_player";
mobile = "https://m.youtube.com/details?v=VuiPGJOEBH4";
};
等等……
我知道我必须认真对待这些项目,所以我的主要问题是如何完全按照我在 Objective-C 中所做的那样去做 self.posts[@"data"][@"items"];因为在 Swift 中不允许将括号加倍。
任何帮助将不胜感激!谢谢! :)
如果您想下载我在 Objective-C 中完成的原始产品,以了解我正在努力完成的工作,您可以查看 - here
最佳答案
您可以从self.posts
字典中获取列表或项目。
manager.GET("https://gdata.youtube.com/feeds/api/users/archetapp/uploads?v=2&alt=jsonc", parameters: nil, success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
self.posts = NSDictionary(objects: [responseObject], forKeys: ["data"])
NSLog("\(self.posts)")
let itemArray : NSMutableArray = self.posts.objectForKey("items") as! NSMutableArray
NSLog("\(itemArray)")
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
希望对你有帮助
享受编码吧!
关于ios - JSON 到 NSMutableArray(Xcode 中的 Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29739841/