无法识别的选择器到实例名称。
我想从section创建partion数组。我试图在swift 2中这样做,但我无法让它工作。
var currentCollation : UILocalizedIndexedCollation!
var sections: [Section] {
let selector: Selector = "name"
let users: [User] = array.map { name in
let a = name["fullName"] as? String
let b = name["email"] as! String
let c = name["mobile"] as! String
let d = name["img"] as! String
let user = User(name: a! )
user.email = b
user.mobile = c
user.img = d
user.section = UILocalizedIndexedCollation.current().section(for: user, collationStringSelector:"name")
return user
}
var sections = [Section]()
for _ in 0..<currentCollation.sectionIndexTitles.count {
sections.append(Section())
}
for user in users {
sections[user.section!].addUser(user: user)
}
print(sections)
for section in sections {
print(section.users)
var user = section.users as? User
print(user?.name)
section.users = self.currentCollation.sortedArray(from: section.users, collationStringSelector: "name") as! [User]
}
return sections
}
@objc class User: NSObject {
let name: String
var section: Int?
var img: String?
var email: String?
var mobile : String?
init(name: String) {
self.name = name
}
}
class Section {
var users: [User] = []
func addUser(user: User) {
self.users.append(user)
}
}
答案 0 :(得分:0)
不要在Swift中使用字符串作为选择器。使用#selector
构造:
let selector: Selector = #selector(name)
但是,只有当前的类是NSObject且具有Objective-C函数&#34; name&#34;没有参数:
class Foo: NSObject {
@objc func name() {
print("In \(#function)")
}
}
(实际上,你可以为不能从NSObject继承的对象创建一个选择器,但你不能在这样的选择器上使用像perform()
这样的函数,所以它不是很有用)
你似乎没有一个名为&#34; name&#34;的功能。在您的班级中,类型为@objc
。