我想使用NSMutableArray在动态表视图中添加搜索,实际上我使用json在表中添加产品所以我需要NSMutableArray
首先我将值分配为
var filteredItems = [String]()
var searchController:UISearchController!
var products:NSMutableArray = NSMutableArray()
我正在使用moltin API并使用NSArray。
if let newProducts:NSArray = response?["result"] as? NSArray {
self.products.addObjects(from: newProducts as [AnyObject])
}
let responseDictionary = NSDictionary(dictionary: response!)
if let newOffset:NSNumber = responseDictionary.value(forKeyPath: "pagination.offsets.next") as? NSNumber {
self.paginationOffset = newOffset.intValue
我也将它用于细胞
let cell = tableView.dequeueReusableCell(withIdentifier: CELL_REUSE_IDENTIFIER, for: indexPath) as! ProductsListTableViewCell
let row = (indexPath as NSIndexPath).row
let product:NSDictionary = products.object(at: row) as! NSDictionary
cell.configureWithProduct(product)
return cell
在视图中,我使用导航栏上搜索栏的正确方法加载了我。
我使用此功能过滤结果,但它无法正常工作并发出错误:
无法将值转换为类型(_) - > _接受参数类型' NSPredict'
func updateSearchResults(for searchController: UISearchController) {
filteredItems.removeAll(keepingCapacity: false)
// filter
filteredItems = products.filter{
item in
item.lowercased().contains(searchController.searchBar.text!.lowercased())
}
tableView.reloadData()
}
但是,当我使用简单的项目数组时,它对我来说非常有效。我如何在 NSMutableArray 中使用它?
答案 0 :(得分:1)
首先,使用json 并不意味着你需要NSMutableArray 。
我会将您的代码重写为:
(假设NSDictionary?
是var products: [String] = []
而响应["结果"]是一个字符串数组。如果这个假设与您的情况不符,您可能需要添加更多有关响应["结果"]包含的信息。)
if let newProducts = response?["result"] as? [String] {
self.products.append(contentsOf: newProducts)
}
和
filteredItems = ...
通过此更改,您的NSMutableArray
无需任何修改即可正常使用。
如果你坚持使用 filteredItems = (products as NSArray as! [String]).filter {
item in
item.lowercased().contains(searchController.searchBar.text!.lowercased())
}
,你可能需要写这样的东西:
filteredItems = products.filter {
item in
(item as! String).lowercased().contains(searchController.searchBar.text!.lowercased())
}.map{$0 as! String}
或者这个:
// postlab.cpp
// this program implements a student database
// user can print, and search by
// lastname, email, area code, and city
//
// HINT to search by area code, use the
// C++ string substring function, for example
// string s = "hamburger";
// cout<<s.substr(3,4); // prints burg
//
// http://www.cplusplus.com/reference/string/string/substr/
#include<iostream>
#include<fstream>
#include<cstdlib>
#include "Student.h"
using namespace std;
void get_stream(ifstream& ins);
int menu(); // print the menu of choicse
void print_list(int size, Student s[]); // print the whole list
void ln_search(int size, Student s[]); // search database for last name
void email_search(int size, Student s[]); // search for an email address
void ac_search(int size, Student s[]); // search for all in an area code
void city_search(int size, Student s[]); // search for all in a city
int main( )
{
int size, count, choice;
ifstream ins;
get_stream(ins);
ins >> size; // the first value in file is the number of records
Student *dbase = new Student[size]; // to store student's data in
// student array
for (int k=0; k<size; k++){
ins >> dbase[k];
cout << dbase[k]<<endl;
}
// database read, now we process it
do {
choice = menu(); // display menu and input choice
// process user choice
switch(choice){
case 1:
print_list(size,dbase);
break;
case 2:
ln_search(size,dbase);
break;
}
} while (choice != 6); // 6 is exit command
return 0;
}
void get_stream(ifstream& ins)
{
ins.open("student.txt");
if(ins.fail())
{
cout << "Failed to open the input file. \n";
exit(1);
}
}
int menu() // print the menu of choicse
{
int choice;
cout<<"Student Database Processor"<<endl;
cout<<"Menu:"<<endl;
cout<<"1 - Print All Students"<<endl;
cout<<"2 - Search by Last Name"<<endl;
cout<<"3 - Search by email address"<<endl;
cout<<"4 - Print All Students with Requested Area Code"<<endl;
cout<<"5 - Print All Students Living in Requested City"<<endl;
cout<<"6 - Exit"<<endl<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
return choice;
}
void print_list(int size, Student s[]) // print the whole list
{
cout << "Student database contain: " << endl;
for (int k = 1; k<size; k++)
cout << k << "-" << s[k] << endl;
// write a for loop and print all students in s.
// remember, s is an array, so, cout<<s[k]<<endl;
}
void ln_search(int size, Student s[])
{
string ln;
cout << "Enter the last name: " << endl;
cin >> ln ;
for(int k = 0 ;k<size;k++){
if(ln == s[k].lastname)
cout<< "Found";
cout<<s[k] << endl;
// of the last name we entered
}
}
如果上述代码无法解决您的问题,您可能需要修改问题以提供更多信息。