0

I am playing around with Swift recently. I am doing a search function with custom TableView. I have followed this guy in order to make my search work perfectly, but I a problem at the conversion from NSArray to Swift Array.

Here is my code.

func updateSearchResultsForSearchController(searchController: UISearchController) {
    filtered.removeAll(keepCapacity: false)
    proccessArray.removeAll(keepCapacity: false)
    print("start")
    var array = []
    for i in 0...arrProcessName.count{
        if(arrProcessName[safe: i] != nil && arrRequestedBy[safe: i] != nil && arrCreationTime[safe: i] != nil && arrStatus[safe: i] != nil)
        {
            var newProcess = Process(process: arrProcessName[i], requestor: arrRequestedBy[i], time: arrCreationTime[i], status: arrStatus[i])
            self.proccessArray.append(newProcess)
        }
    }
    print("array: ", proccessArray)
    let searchPredicate = NSPredicate(format: "status CONTAINS[c] %@", "pending")
    array = (proccessArray as NSArray).filteredArrayUsingPredicate(searchPredicate)
    print("array: ", array)
    let arr2 = (array as Array).map { $0 as? String ?? "" }
    filtered = arr2
    print("filtered: ", filtered)

    if(filtered.count == 0){
                    searchActive = false;
                } else {
                    searchActive = true;
                }
    tableView?.reloadData()
}

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

    cell.selectionStyle = .None
    cell.accessoryType = UITableViewCellAccessoryType.None

    if self.searchCtrler.active {
        print("processArray: ",proccessArray[indexPath.row].status as String!)
        cell.lbl_status.text = proccessArray[indexPath.row].status as String!
    } else {
        cell.lbl_request_name.text = arrProcessName[indexPath.row]
        cell.lbl_requested_by.text = "Requested by: " + arrRequestedBy[indexPath.row]
        cell.lbl_creation_time.text = arrCreationTime[indexPath.row]
        cell.lbl_status.text = arrStatus[indexPath.row]

        switch arrStatus[indexPath.row] {
            case "Completed", "Approved":
                cell.img_status.image = UIImage(named: "ic_approved")
            case "Running", "Processing", "Pending":
                cell.img_status.image = UIImage(named: "ic_pending")
            case "Denied", "Rejected":
                cell.img_status.image = UIImage(named: "ic_reject")
            case "Error":
                cell.img_status.image = UIImage(named: "ic_terminated")
            case "Retracted":
                cell.img_status.image = UIImage(named: "ic_retracted")
            default:
                cell.img_status.image = UIImage(named: "")
        }
    }

    return cell
}

Class Process

import Foundation

class Process:NSObject{
var process: NSString!
var requestor: NSString!
var time: NSString!
var status: NSString!

init(process:NSString, requestor:NSString, time: NSString, status: NSString){
    self.process = process
    self.requestor = requestor
    self.time = time
    self.status = status
}

}

I have printed the array and the result is like this.

array:  (
   "< X.Process: 0x7f8bc94cfa50>"
)

After the conversion which is filtered. I get an empty array which hit me the EXC_BAD_INSTRUCTION.

Is there anyway to convert my NSArray to Swift Array without affect my data inside the NSArray?

Thank you

9
  • Please add tableView delegate methods contents Commented Nov 18, 2016 at 10:18
  • proccessArray is not contains string directly it may contains custom class object Process, and in filter array you try to convert it to optional String i.e the reason your filter array contains empty string object. Commented Nov 18, 2016 at 10:20
  • @NiravD so what i can do about it? Commented Nov 18, 2016 at 10:21
  • @DaveCruise You need to show only status when you are searching? Commented Nov 18, 2016 at 10:26
  • @NiravD no i need to show every detail like id:1, status:pending, country:TH. Commented Nov 18, 2016 at 10:27

1 Answer 1

1

There is no need to convert Swift Array [Process] to NSArray just for filter, you can directly filter your [Process] array like this.

let filterProcess = self.proccessArray.filter { $0.status == "pending" }

In your case there is no need to use contains but if you want then:

let filterProcess = self.proccessArray.filter { $0.status.localizedCaseInsensitiveContainsString("pending") }
Sign up to request clarification or add additional context in comments.

12 Comments

i hit this error with cannot assign value of type '[Process]' to type '[String]'
this is my code "filtered = self.proccessArray.filter{ $0.status == "pending" }" may i know what is $0 mean?
@DaveCruise You need to declare your filtered also as [Process] not as [String] because you want whole process object
@DaveCruise it will iterate through whole array elements one by one and each will represent by $0 one after another.
I still don't get result when i print my filtered.
|

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.