2017-10-04 1 views
1

Ceci est mon code de classe de modèle actuel.Comment alphabétiser le tableau par son prénom dans Swift?

final class ContactData 
{ 
    static let sharedInstance = ContactData() 

    private var contactList : [Contact] = 
      [Contact(name:"Mike Smith",email:"[email protected]"), 
      Contact(name:"John Doe",email:"[email protected]"), 
      Contact(name:"Jane Doe",email:"[email protected]")] 


    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] 
    } 
} 

Tout fonctionne correctement, mais je l'ai essayé le tableau de classement par ordre alphabétique Contact s en utilisant:

var sortedContacts = contactList.sorted{ 
      $0.localizedCaseinsensitiveCompare($1)==ComparisonResult.orderedAscending} 
     } 

mais je reçois une erreur:

Value of type ' Contact ' has no member ' localizedCaseinsensitiveCompare '

En regardant à travers des questions ici J'ai seulement pu trouver cette seule manière d'alphabétiser le tableau.

Je courais Swift 3 et Xcode 8.3

Répondre

1

Vous devriez faire votre classe contact conforme au protocole Comparable:

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 
    } 
} 

test Playground

let c1 = Contact(name:"Mike Smith",email:"[email protected]") 
let c2 = Contact(name:"John Doe",email:"[email protected]") 
let c3 = Contact(name:"John Doe",email:"[email protected]") 
let c4 = Contact(name:"Jane Doe",email:"[email protected]") 
let c5 = Contact(name:"Jane Doe",email:"[email protected]") 

let people = [c1, c2, c3, c4, c5] 
print(people) // "[Mike Smith - [email protected], John Doe - [email protected], John Doe - [email protected], Jane Doe - [email protected], Jane Doe - [email protected]]\n" 
print(people.sorted()) // "[Jane Doe - [email protected], Jane Doe - [email protected], John Doe - [email protected], John Doe - [email protected], Mike Smith - [email protected]]\n" 
+0

Quelques points à considérer avec cette solution. Tout est lié à 'name 'seulement. Cela suppose que vous voulez toujours comparer par nom et ignorer les courriels. Il traite deux contacts différents avec le même nom mais un email différent comme étant égal. Cela peut causer beaucoup de bugs subtils. Il serait probablement préférable si l'email a été comparé des noms sont égaux. – rmaddy

+0

Mon commentaire est à l'OP fournir son tri personnalisé –

+0

ou comparer la description –

0

Vous ne pouvez pas comparer Contact classes personnalisées insensible à la casse. Vous devriez comparer les name propriétés d'entre eux:

var sortedContacts = contactList.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending} } 
+1

Ou faire contact sont conformes aux Protocole comparable –

+0

@LeoDabus Oui, c'est une meilleure approche si vous prévoyez t o utilisez beaucoup le tri dans votre code. – the4kman

+0

@ the4kman En faisant cela dans mon terrain de jeu, il est en train de lancer une erreur où mes fonctions ne sont pas essentiellement non-existantes. – Nonstoplive