Krishna Thakur Krishna Thakur - 1 year ago 91
Swift Question

Array list item count and number of element in an array mismatch while using swiftyjson?

import UIKit
import Alamofire
import SwiftyJSON

class RecentAdded: UIViewController ,UITableViewDataSource,UITableViewDelegate{

@IBOutlet var tableView: UITableView!
var list:JSON!
var sendurl:String!

override func viewDidLoad() {

Alamofire.request(.GET, "", parameters: ["token": "260674ecb51572a8faa4e77199"])
.responseJSON { response in
if let json = response.result.value {
self.list = JSON(data:!)
print(self.list) /// Showing less element if element is more than 25
self.tableView.dataSource = self
self.tableView.delegate = self
print(self.list.arrayValue.capacity) // Printing the actual capacity

// Do any additional setup after loading the view.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.list.arrayValue.capacity


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! RecentCellTableViewCell

let sampleArray = self.list.array
let imageURL:String! = sampleArray![indexPath.row]["image"]["thumb"]["url"].stringValue
if imageURL != ""{
Alamofire.request(.GET, imageURL).responseImage { (response) -> Void in
guard let image = response.result.value else { return }
cell.img!.image = image

cell.img!.image = UIImage(named: "rad")!

cell.nm?.text = sampleArray![indexPath.row]["name"].stringValue
let catarr = sampleArray![indexPath.row]["categories"].array
let des:String! = "category : " + catarr![0]["title"].stringValue + " " + "slug : " + catarr![0]["slug"].stringValue
cell.des?.text = des

return cell

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! RecentCellTableViewCell
let sampleArray = self.list.array
let url = sampleArray![indexPath.row]["streams"].array
sendurl = url![0]["stream"].stringValue
self.performSegueWithIdentifier("next", sender: self)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "next") {
// initialize new view controller and cast it as your view controller
var viewController = segue.destinationViewController as! Player
viewController.urll = sendurl



My problem is when i am printing list.arrayvalue.capacity it is showing the actual size of array which is correct but when i tried to print element of array it show's less element then its counting. so i am not sure what is wrong in code????/

The main problem is in printing element. Not printing all elements.

Answer Source

I think you're confusing the array capacity with the actual item count. For numberOfRowsInSection, use the count property of the array instead:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.list.arrayValue.count

More details about count vs. capacity in this answer: Swift array.capacity vs array.count