0

I'm parsing JSON with Alamofire and SwiftyJSON. TableView Controller is working absolutely fine but I need do eliminate/filter some of the objects. Like if an object in array of dictionary with an 'id', i.e. '1', persists, it should be filtered.

This is how JSON data looks like:

[
{
    "id": 1,
    "title": "item1",
    "created_at": "2014-08-30T15:50:38.421Z",
    "updated_at": "2014-08-30T15:50:38.421Z"
},
{
    "id": 2,
    "title": "item2",
    "created_at": "2014-08-30T15:50:38.453Z",
    "updated_at": "2014-08-30T15:50:38.453Z"
},
{
    "id": 3,
    "title": "item3",
    "created_at": "2014-08-30T15:50:38.464Z",
    "updated_at": "2014-08-30T15:50:38.464Z"
},
]

this is how table view controller looks like:

import UIKit
import Alamofire

class ViewController: UITableViewController {

    var items: Array<Item>?
    var username: JSON!
    var id: JSON!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        items = Array()

        Alamofire.request(.GET, "http://brownbro-json-api-test.herokuapp.com/api/items")
            .responseJSON {(request, response, JSONresponse, error) in
                let result = JSON(JSONresponse!)

                for (var i = 0; i < result.arrayValue.count; i++) {
                    var item: Item = Item (
                        id: result[i]["id"].stringValue,
                        title: result[i]["title"].stringValue,
                        detail: "detail",
                        imageURL: "http://japan.cnet.com/storage/2013/11/14/98b86da26f62c9e12c07f1d49932d6eb/131114_wemake_180x135.jpg"
                    )
                    self.items?.append(item)
                }
                self.tableView.reloadData()
            }
    }    

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section:    Int) -> Int {
        //return 10
        return self.items!.count
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100;
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "ItemCell")

        cell.textLabel!.text = self.items?[indexPath.row].title
        cell.detailTextLabel!.text = self.items?[indexPath.row].detail


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
            var imageURL: NSURL = NSURL(fileURLWithPath: self.items![indexPath.row].imageURL)!
           // var imageData: NSData = NSData(contentsOfURL: imageURL)!
           // var image: UIImage = UIImage(data: imageData)!

            dispatch_async(dispatch_get_main_queue(), { () -> () in
                //cell.imageView!.image = image
            })
        })
        return cell
    }

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
        performSegueWithIdentifier("show_detail", sender: indexPath)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "show_detail" {
            var indexPath : NSIndexPath = sender as! NSIndexPath
            var item_id : String = self.items![indexPath.row].id
            var DVController = segue.destinationViewController as! DetailViewController
            DVController.item_id = item_id
        }
    }
}

this is the Item object:

import UIKit

class Item: NSObject {
    var id: String, title: String, detail: String, imageURL: String

    init(id: String, title: String, detail: String, imageURL: String) {
        self.id = id
        self.title = title
        self.detail = detail
        self.imageURL = imageURL
    }
}

I've got no idea how can I filter some rows.

1 Answer 1

2

You can get the items without ' id == "1" ' like this way.

var items: [Item] = ...
items = items.filter { $0.id != "1"} // This will eliminate items with id == "1".
// it is equivalent to the below line
items = items.filter({ (item:Item)->Bool in item.id != "1"})

Don't forget to call tableView.reloadData() after mutating the items.

Sign up to request clarification or add additional context in comments.

5 Comments

it keeps saying, cannot invoke 'filter' with an argument list of type '((_) -> _)'
I'm adding it below "self.items?.append(item)"
Oh, I didn't noticed that id is String. Please, try items = items.filter { $0.id != "1"}
For clarity of type, this may be better: items = items.filter { (item:Item)->Bool in item.id != "1"}
That's nice. Enjoy your programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.