NSArray 转换为 Swift 数组后数组为空
Array is empty after the conversion of NSArray to Swift Array
我最近在玩Swift。我正在使用自定义 TableView 执行搜索功能。我已经关注 人以使我的搜索工作完美,但我在从 NSArray 到 Swift 数组的转换时遇到问题。
这是我的代码。
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 { [=11=] 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 进程
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
}
}
我把数组打印出来了,结果是这样的
array: (
"< X.Process: 0x7f8bc94cfa50>"
)
过滤后的转换。我得到一个空数组,它击中了我 EXC_BAD_INSTRUCTION.
有没有办法将我的 NSArray
转换为 Swift 数组而不影响我在 NSArray 中的数据?
谢谢
不需要将Swift数组[Process]
转换为NSArray
只是为了过滤,你可以像这样直接过滤你的[Process]
数组。
let filterProcess = self.proccessArray.filter { [=10=].status == "pending" }
在你的情况下没有必要使用 contains
但如果你想那么:
let filterProcess = self.proccessArray.filter { [=11=].status.localizedCaseInsensitiveContainsString("pending") }
我最近在玩Swift。我正在使用自定义 TableView 执行搜索功能。我已经关注
这是我的代码。
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 { [=11=] 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 进程
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
}
}
我把数组打印出来了,结果是这样的
array: (
"< X.Process: 0x7f8bc94cfa50>"
)
过滤后的转换。我得到一个空数组,它击中了我 EXC_BAD_INSTRUCTION.
有没有办法将我的 NSArray
转换为 Swift 数组而不影响我在 NSArray 中的数据?
谢谢
不需要将Swift数组[Process]
转换为NSArray
只是为了过滤,你可以像这样直接过滤你的[Process]
数组。
let filterProcess = self.proccessArray.filter { [=10=].status == "pending" }
在你的情况下没有必要使用 contains
但如果你想那么:
let filterProcess = self.proccessArray.filter { [=11=].status.localizedCaseInsensitiveContainsString("pending") }