JSON for NSMutableArray (Swift in Xcode)
I am working on an application to use the YouTube Google API. I had this application fully functional in Objective-C, but I ran into some problems in Swift.
This is what it looks like in 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);
}];
This is what I have so far in 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)
})
}
and it prints this ... (It's just like Objective-C, but Obj-C starts where it says "items")
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";
};
etc.......
I know I need to navigate to items, so my main question is how to do what I did in Objective-C where I did self.posts [@ "data"] [@ "items"]; since this doubling of parentheses is not allowed in Swift.
Any help would be appreciated! Thank you! :)
If you would like to download my original product, made in Objective-C, to get an idea of ββwhat I am trying to accomplish, you can check it out - here
source to share
You can get a list or form element in a dictionary 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)
})
}
Hope it helps,
Enjoy coding!
source to share