这是我当前的模型类代码。
final class ContactData
{
static let sharedInstance = ContactData()
private var contactList : [Contact] =
[Contact(name:"Mike Smith",email:"mike@smith.com"),
Contact(name:"John Doe",email:"john@doe.com"),
Contact(name:"Jane Doe",email:"jane@doe.com")]
private init()
{
// SORTING HERE
}
var index = 0
func newContact(new:Contact)
{
contactList.append(new)
//sort
}
func updateContact(updated:Contact)
{
contactList[index]=updated
//sort
}
func previousContact() -> Contact
{
index-=1
if index < 0
{
index = contactList.count-1
}
return contactList[index]
}
func nextContact() -> Contact
{
index+=1
if index == contactList.count
{
index = 0
}
return contactList[index]
}
func firstContact() -> Contact
{
return contactList[0]
}
func currentContact() -> Contact
{
return contactList[index]
}
}
一切都运行正常,但我尝试使用以下字符对Contact
的数组进行按字母顺序排列:
var sortedContacts = contactList.sorted{
$0.localizedCaseinsensitiveCompare($1)==ComparisonResult.orderedAscending}
}
但是我收到了一个错误:
类型的价值&#39;
Contact
&#39;没有会员&#39;localizedCaseinsensitiveCompare
&#39;
通过查看这里的问题,我只能找到按字母顺序排列数组的单一方法。
我正在运行Swift 3和Xcode 8.3
答案 0 :(得分:1)
你应该使你的班级联系符合可比协议:
payload = {"list":[{"a":"Name1","b":"Name2"},{"a":"Name3","b":"Name4"}]}
statement = "UNWIND {list} AS d "
statement += "MATCH (A:Person {name: d.a}) "
statement += "MATCH (B:Person {name: d.b}) "
statement += "MERGE (A)-[:KNOWS]-(B) "
tx = session.begin_transaction()
tx.run(statement,payload)
tx.commit()
游乐场测试
class Contact: Comparable, CustomStringConvertible {
let name: String
let email: String
init(name: String, email: String) {
self.name = name
self.email = email
}
var description: String {
return name + " - " + email
}
// provide your custom comparison
static func <(lhs: Contact, rhs: Contact) -> Bool {
return lhs.name.localizedCaseInsensitiveCompare(rhs.name) == .orderedAscending ||
lhs.email.localizedCaseInsensitiveCompare(rhs.email) == .orderedAscending
}
// you will need also to make it conform to Equatable
static func ==(lhs: Contact, rhs: Contact) -> Bool {
return lhs.name == rhs.name && lhs.email == rhs.email
}
}
答案 1 :(得分:0)
您无法比较Contact
自定义类不区分大小写。您应该比较它们的name
属性:
var sortedContacts = contactList.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending} }