Dave Cruise Dave Cruise - 1 year ago 200
Swift Question

Cannot convert value of type ... to type

I am facing error to put my tuple as a NSArray.

My code is shown as below.

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)

I wanted to use filteredArrayUsingPredicate in this case. But I am facing

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?

Answer Source

Can anyone tell me why am I facing this error?

Certainly. NSArray is an Objective-C class. The only objects that can be elements of an NSArray are Objective-C objects. There is no such thing as a tuple in Objective-C; a tuple is a Swift object. An array of Swift objects cannot be turned into an NSArray.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download