未确认选择器名称
无法识别选择器到实例名称。未确认选择器名称
我想从部分创建部分数组。我正在试图在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)
}
}
回答:
请勿在Swift中使用字符串作为选择器。使用#selector
结构:
let selector: Selector = #selector(name)
然而,这只会工作,如果当前类是NSObject的,有没有参数的Objective-C的功能“名”:
class Foo: NSObject { @objc func name() {
print("In \(#function)")
}
}
(实际上,你可以创建选择一个对象不从NSObject的继承,但是你不能就这么选择使用功能,如perform()
,所以不是非常有用)
你不APPE ar在类中有一个名称为“name”的函数,类型是@objc
或不是。
以上是 未确认选择器名称 的全部内容, 来源链接: utcz.com/qa/265496.html