简体   繁体   中英

How to Sort using NSMutableArray?

I am trying to sort the NSMutableArray with this piece of code

let sortContacts = contactData! as NSArray as? [String]
        print("\(sortContacts.sort())") 

I am trying to convert the NSMutableArray to Array of String but when it comes to print("\\(sortContacts.sort())") its giving me nil

Anyone who can suggest me sorting through NSMutableArray?

The class NSMutableArray has several sort methods. You should read the documentation. One of the sort methods is this one .

By the way: a ! in Swift code is a code smell.

This works, just make sure your NSMutableArray contains only string, else use if let :

let contactData = NSMutableArray(array: ["c","b","a"])
let sortContacts = contactData as NSArray as! [String]
print(sortContacts.sort())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM