无法将类型 ... 的值转换为类型
Cannot convert value of type ... to type
我在将我的元组作为 NSArray 时遇到错误。
我的代码如下所示
var Name: [String] = []
var Requested: [String] = []
var Time: [String] = []
var Status: [String] = []
var filtered: [(process:String, requestor:String, creation:String, status:String)] = []
var allItems: [(process:String, requestor:String, creation:String, status:String)] = []
for i in 0...Name.count{
if(Name[safe: i] != nil && Requested[safe: i] != nil && Time[safe: i] != nil && Status[safe: i] != nil)
{
allItems += [(process:Name[i], requestor:Requested[i], creation:Time[i], status:Status[i])]
}
}
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (allItems as NSArray).filteredArrayUsingPredicate(searchPredicate)
我想在这种情况下使用 filteredArrayUsingPredicate。但是我正面临
Cannot convert value of type '[(process: String, requestor: String, creation: String, status: String)]' to type 'NSArray' in coercion
谁能告诉我为什么会遇到这个错误?
Can anyone tell me why am I facing this error?
当然可以。 NSArray 是一个 Objective-C class。唯一可以作为 NSArray 元素的对象是 Objective-C 个对象。 Objective-C中没有元组这种东西;一个元组是一个 Swift 对象。 Swift 个对象的数组无法转换为 NSArray。
我在将我的元组作为 NSArray 时遇到错误。
我的代码如下所示
var Name: [String] = []
var Requested: [String] = []
var Time: [String] = []
var Status: [String] = []
var filtered: [(process:String, requestor:String, creation:String, status:String)] = []
var allItems: [(process:String, requestor:String, creation:String, status:String)] = []
for i in 0...Name.count{
if(Name[safe: i] != nil && Requested[safe: i] != nil && Time[safe: i] != nil && Status[safe: i] != nil)
{
allItems += [(process:Name[i], requestor:Requested[i], creation:Time[i], status:Status[i])]
}
}
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (allItems as NSArray).filteredArrayUsingPredicate(searchPredicate)
我想在这种情况下使用 filteredArrayUsingPredicate。但是我正面临
Cannot convert value of type '[(process: String, requestor: String, creation: String, status: String)]' to type 'NSArray' in coercion
谁能告诉我为什么会遇到这个错误?
Can anyone tell me why am I facing this error?
当然可以。 NSArray 是一个 Objective-C class。唯一可以作为 NSArray 元素的对象是 Objective-C 个对象。 Objective-C中没有元组这种东西;一个元组是一个 Swift 对象。 Swift 个对象的数组无法转换为 NSArray。