I have this:
class Movies {
Name:String
Date:Int
}
and an array of [Movies]. How do I sort the array alphabetically by name? I've tried:
movieArr = movieArr.sorted{ $0 < $1 }
and
movieArr = sorted(movieArr)
but that doesn't work because I'm not accessing the name attribute of Movies.
In the closure you pass to sort, compare the properties you want to sort by. Like this:
movieArr.sorted { $0.name < $1.name }
or the following in the cases that you want to bypass cases:
movieArr.sorted { $0.name.lowercased() < $1.name.lowercased() }
Sidenote: Typically only types start with an uppercase letter; I'd recommend using name and date, not Name and Date.
Example, in a playground:
class Movie {
let name: String
var date: Int?
init(_ name: String) {
self.name = name
}
}
var movieA = Movie("A")
var movieB = Movie("B")
var movieC = Movie("C")
let movies = [movieB, movieC, movieA]
let sortedMovies = movies.sorted { $0.name < $1.name }
sortedMovies
sortedMovies will be in the order [movieA, movieB, movieC]
Swift5 Update
channelsArray = channelsArray.sorted { (channel1, channel2) -> Bool in
let channelName1 = channel1.name
let channelName2 = channel2.name
return (channelName1.localizedCaseInsensitiveCompare(channelName2) == .orderedAscending)
}
With Swift 3, you can choose one of the following ways to solve your problem.
1. Using sorted(by:) with a Movie class that does not conform to Comparable protocol
If your Movie class does not conform to Comparable protocol, you must specify in your closure the property on which you wish to use Array's sorted(by:) method.
Movie class declaration:
import Foundation
class Movie: CustomStringConvertible {
let name: String
var date: Date
var description: String { return name }
init(name: String, date: Date = Date()) {
self.name = name
self.date = date
}
}
Usage:
let avatarMovie = Movie(name: "Avatar")
let titanicMovie = Movie(name: "Titanic")
let piranhaMovie = Movie(name: "Piranha II: The Spawning")
let movies = [avatarMovie, titanicMovie, piranhaMovie]
let sortedMovies = movies.sorted(by: { $0.name < $1.name })
// let sortedMovies = movies.sorted { $0.name < $1.name } // also works
print(sortedMovies)
/*
prints: [Avatar, Piranha II: The Spawning, Titanic]
*/
2. Using sorted(by:) with a Movie class that conforms to Comparable protocol
However, by making your Movie class conform to Comparable protocol, you can have a much concise code when you want to use Array's sorted(by:) method.
Movie class declaration:
import Foundation
class Movie: CustomStringConvertible, Comparable {
let name: String
var date: Date
var description: String { return name }
init(name: String, date: Date = Date()) {
self.name = name
self.date = date
}
static func ==(lhs: Movie, rhs: Movie) -> Bool {
return lhs.name == rhs.name
}
static func <(lhs: Movie, rhs: Movie) -> Bool {
return lhs.name < rhs.name
}
}
Usage:
let avatarMovie = Movie(name: "Avatar")
let titanicMovie = Movie(name: "Titanic")
let piranhaMovie = Movie(name: "Piranha II: The Spawning")
let movies = [avatarMovie, titanicMovie, piranhaMovie]
let sortedMovies = movies.sorted(by: { $0 < $1 })
// let sortedMovies = movies.sorted { $0 < $1 } // also works
// let sortedMovies = movies.sorted(by: <) // also works
print(sortedMovies)
/*
prints: [Avatar, Piranha II: The Spawning, Titanic]
*/
3. Using sorted() with a Movie class that conforms to Comparable protocol
By making your Movie class conform to Comparable protocol, you can use Array's sorted() method as an alternative to sorted(by:).
Movie class declaration:
import Foundation
class Movie: CustomStringConvertible, Comparable {
let name: String
var date: Date
var description: String { return name }
init(name: String, date: Date = Date()) {
self.name = name
self.date = date
}
static func ==(lhs: Movie, rhs: Movie) -> Bool {
return lhs.name == rhs.name
}
static func <(lhs: Movie, rhs: Movie) -> Bool {
return lhs.name < rhs.name
}
}
Usage:
let avatarMovie = Movie(name: "Avatar")
let titanicMovie = Movie(name: "Titanic")
let piranhaMovie = Movie(name: "Piranha II: The Spawning")
let movies = [avatarMovie, titanicMovie, piranhaMovie]
let sortedMovies = movies.sorted()
print(sortedMovies)
/*
prints: [Avatar, Piranha II: The Spawning, Titanic]
*/
let sortArray = array.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
For those using Swift 3, the equivalent method for the accepted answer is:
movieArr.sorted { $0.Name < $1.Name }
Most of these answers are wrong due to the failure to use a locale based comparison for sorting. Look at localizedStandardCompare()
Sorted array
Swift 4.2
arrayOfRaces = arrayOfItems.sorted(by: { ($0["raceName"] as! String) < ($1["raceName"] as! String) })
*import Foundation
import CoreData
extension Messages {
#nonobjc public class func fetchRequest() -> NSFetchRequest<Messages> {
return NSFetchRequest<Messages>(entityName: "Messages")
}
#NSManaged public var text: String?
#NSManaged public var date: Date?
#NSManaged public var friends: Friends?
}
//here arrMessage is the array you can sort this array as under bellow
var arrMessages = [Messages]()
arrMessages.sort { (arrMessages1, arrMessages2) -> Bool in
arrMessages1.date! > arrMessages2.date!
}*
Related
Hi I have a task to implement the Fleet protocol which has two functions:
addNewCar - adds a new car object to the Fleet.
- Parameter car: car to add to the Fleet
- Returns: false if the car with same id already exists in the Fleet, true – otherwise.
deleteCar - Deletes the car with the specified id from the Fleet.
- Returns: true if the car with same id existed in the Fleet, false – otherwise.
listCarsByModel - returns 10 car models containing the specified string.
If there are several cars with the same model, brand's name is added to car's model in the format "brand - car",
otherwise returns simply "car".
listCarsByBrand - returns 10 car models whose brand contains the specified string,
result is ordered by brand.
struct Car {
let id: String; // unique identifier
let model: String;
let brand: String;
}
protocol Fleet {
func addNewCar(car: Car) -> Bool
func deleteCar(id: String) -> Bool
func listCarsByModel(searchString: String) -> Set<String>
func listCarsByBrand(searchString: String) -> [String]
}
class FleetImpl: Fleet {
var cars: [Car] = []
func addNewCar(car: Car) -> Bool {
if let i = cars.firstIndex(where: { $0.id == car.id }) {
print(i)
return false
} else {
cars.append(car)
print(car)
return true
}
}
func deleteCar(id: String) -> Bool {
return true
}
func listCarsByModel(searchString: String) -> Set<String> {
}
func listCarsByBrand(searchString: String) -> [String] {
}
}
I've used method firstIndex(where:). But the function adds new car to the array with existing id. (i.e. two or more cars with the same id)
How can I access that 'id' property of Car struct in order to manipulate that data?
Please help me out
This is the test code:
func test(fleet: Fleet) {
assert(!fleet.deleteCar(id: "1"))
assert(fleet.addNewCar(car: Car(id: "1", model: "1", brand: "Lex")))
assert(!fleet.addNewCar(car: Car(id: "1", model: "any name because we check id only", brand: "any brand")))
assert(fleet.deleteCar(id: "1"))
assert(fleet.addNewCar(car: Car(id: "3", model: "Some Car3", brand: "Some Brand2")))
assert(fleet.addNewCar(car: Car(id: "4", model: "Some Car1", brand: "Some Brand3")))
var byModels: Set<String> = fleet.listCarsByModels(searchString: "Car")
assert(byModels.count == 10)
byModels = fleet.listCarsByModels(searchString: "Some Car")
assert(byModels.count == 4)
assert(byModels.contains("Some Brand3 - Some Car1"))
assert(byModels.contains("Some Car2"))
assert(byModels.contains("Some Car3"))
assert(!byModels.contains("Some Car1"))
assert(byModels.contains("Some Brand1 - Some Car1"))
var byBrand: [String] = fleet.listCarsByBrand(searchString: "Brand")
assert(byBrand.count == 10)
byBrand = fleet.listCarsByBrand(searchString: "Some Brand")
assert(byBrand.count == 4)
assert(byBrand[0] == "Some Car1")
assert(byBrand[1] == "Some Car2" || byBrand[1] == "Some Car3")
assert(byBrand[2] == "Some Car2" || byBrand[2] == "Some Car3")
assert(byBrand[3] == "Some Car1")
}
test(fleet: FleetImpl())
Maybe a set instead of an array would work better in this case as we're dealing with unique elements. Also note that rather than using firstIndex(where) I'm using first(where) which will return the car rather than index.
import UIKit
struct Car: Hashable {
let id: String
let model: String
let brand: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
protocol Fleet {
func addNewCar(car: Car) -> Bool
func deleteCar(id: String) -> Bool
func listCarsByModel(searchString: String) -> [String]
func listCarsByBrand(searchString: String) -> [String]
}
class FleetImpl: Fleet {
var cars = Set<Car>()
func addNewCar(car: Car) -> Bool {
guard !cars.contains(where: { $0.id == car.id }) else { return false }
cars.insert(car)
return true
}
func deleteCar(id: String) -> Bool {
guard let car = cars.first(where: { $0.id == id }) else { return false }
cars.remove(car)
return true
}
func listCarsByModel(searchString: String) -> [String] {
let searchedCars = cars.filter { $0.model.lowercased().contains(searchString.lowercased()) }.sorted { $0.brand < $1.brand }
var formattedModels = [String]()
searchedCars.forEach { car in
if let car = searchedCars.first(where: { $0.model == car.model && $0.id != car.id }) {
let carName = [car.model, car.brand].joined(separator: " - ")
formattedModels.append(carName)
} else {
formattedModels.append(car.model)
}
}
return Array(formattedModels.prefix(10))
}
func listCarsByBrand(searchString: String) -> [String] {
let searchedBrands = cars.filter { $0.brand.lowercased().contains(searchString.lowercased()) }.sorted { $0.brand < $1.brand }.map { $0.model }
return Array(searchedBrands.prefix(10))
}
}
I'm making an app for airports and I'm getting an array of data from one api, like so:
"data":[
{"id":"001","code":"ABZ","name":"Aberdeen","country":"United Kingdom"},
{"id":"002","code":"AUH","name":"Abu Dhabi","country":"United Arab Emirates"},
.
.
.
]
AND :
"airports":[
{"from":"001",
"to":["1","3","11","13","12","20","23","27","29","31","33"]
},
.
.
.
]
I have created realm model classes:
class AirportsDataRealm: Object {
#objc dynamic var name: String = ""
#objc dynamic var id: Int = 0
#objc dynamic var code: String = ""
#objc dynamic var country: String = ""
override static func primaryKey() -> String? {
return "id"
}
}
class AirportsFromToRealm: Object {
#objc dynamic var fromID: Int = 0
var toID = List<Int>()
override static func primaryKey() -> String? {
return "fromID"
}
}
now I want to save it into realm, I'm using swiftyJSON and I have used for-loop to do it and it is working fine but I think it's taking long time since the array is very long, here is what I've done:
// Airports Data
let countData = json["data"].count
for i in 0...countData - 1{
let airportsDataModel = AirportsDataRealm()
airportsDataModel.code = json["data"][i]["code"].stringValue
airportsDataModel.name = json["data"][i]["name"].stringValue
airportsDataModel.country = json["data"][i]["country"].stringValue
airportsDataModel.id = Int(json["data"][i]["id"].stringValue)!
try! realm.write {
realm.add(airportsDataModel, update: true)
}
}
//Airports FROM-TO
let countFromTo = json["airports"].count
for i in 0...countFromTo - 1{
let fromToDataModel = AirportsFromToRealm()
fromToDataModel.fromID = Int(json["airports"][i]["from"].stringValue)!
let arrayTo = json["airports"][i]["to"].arrayValue.map{ $0.intValue }
fromToDataModel.toID.append(objectsIn: arrayTo)
try! realm.write {
realm.add(fromToDataModel, update: true)
}
}
is there any way to save the whole array in realm in one shot without for-loop?
P.S
"there should be a relation between the two tables because each from 'id' has a list of 'to' id's and the id's are from the data table, for now I managed to create this relations when fetching the data using filters ,, so just ignore this"
Thank you
Simply use map method,
First I needed to add initializers to my object classes and pass json array as a parameter, like so:
class AirportsDataRealm: Object {
#objc dynamic var name: String = ""
#objc dynamic var id: Int = 0
#objc dynamic var code: String = ""
#objc dynamic var country: String = ""
convenience required init(withJSON json : JSON) {
self.init()
self.name = json["name"].stringValue
self.id = json["id"].intValue
self.code = json["code"].stringValue
self.country = json["country"].stringValue
}
override static func primaryKey() -> String? {
return "id"
}
}
class AirportsFromToRealm: Object {
#objc dynamic var fromID: Int = 0
var toID = List<Int>()
convenience required init(withJSON json : JSON) {
self.init()
self.fromID = json["from"].intValue
let toArray = json["to"].arrayValue.map{ $0.intValue }
self.toID.append(objectsIn: toArray)
}
override static func primaryKey() -> String? {
return "fromID"
}
}
Then by using map method the code will look like this:
func updateAirport(json: JSON) {
// Airports Data
let airportsData : [AirportsDataRealm]
let airportsDataJsonArray = json["data"].array
airportsData = airportsDataJsonArray!.map{AirportsDataRealm(withJSON: $0)}
//Airports FROM-TO
let airportsFromTo : [AirportsFromToRealm]
let airportsFromToJsonArray = json["airports"].array
airportsFromTo = airportsFromToJsonArray!.map{AirportsFromToRealm(withJSON: $0)}
//Write To Realm
try! realm.write {
realm.add(airportsData, update: true)
realm.add(airportsFromTo, update: true)
}
}
No for loops anymore ^_^
I have a class called Person:
class Person{
let name: String?
let areas: [Area]
}
And Area consisting of the following:
class Area{
let id: String
let name: String
}
I've created a "people" array and what I want to do is check the "people" array if area.name = "Brooklyn". If it doesn't remove that person from the "people" array.
Here is what I have tried but it doesn't work:
var people = [Person]()
for s in people{
for a in s.preferredArea{
if a.areaName != "Brooklyn"{
let index = people.indexOf(s)
people.removeAtIndex(index!)
}
}
}
Can anyone point me into the right direction please?
To use indexOf, you'll have to make Person conforming the protocol Equatable:
class Person: Equatable {
let name: String
var area: [Area]
init(name: String) {
self.name = name
self.area = []
}
}
func ==(lhs: Person, rhs: Person) -> Bool {
return lhs.name == rhs.name &&
lhs.area == rhs.area // You'll have to make Area equatable too
}
Having this done, your code just works fine. If you don't want to use this method, you can use:
people.enumerate().forEach {
if !$0.1.area.contains({ $0.name == "Brooklyn" }) {
people.removeAtIndex($0.0)
}
}
Or if you can use filter to create a new array:
var newPeople = people.filter { $0.area.contains { $0.name == "Brooklyn" } }
I want to do something like this:
enum WeekDay {
case Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
class Person: Object {
dynamic var birthday: WeekDay? = .Monday
dynamic var id: String? = nil
dynamic var birthdayRaw: String? = nil
override static func primaryKey() -> String? {
return "id"
}
}
But, I'm getting an error:
Property cannot be marked dynamic because its type cannot be
represented in Objective-C
How can I solve this ? Thanks for any help.
Realm doesn't have a direct way do it. Github issue.
But you can consider this trick
enum WeekDay: String {
case Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
class Person: Object {
private var _day: WeekDay?
var birthday: WeekDay? {
get {
if let resolTypeRaw = birthdayRaw {
_day = WeekDay(rawValue: resolTypeRaw)
return _day
}
return .Sunday
}
set {
birthdayRaw = newValue?.rawValue
_day = newValue
}
}
dynamic var id: String? = nil
dynamic var birthdayRaw: String? = nil
override static func primaryKey() -> String? {
return "id"
}
}
As of Realm 3.x you can use Int-based enums (apparently, by side-effect).
As of Realm 4.1 you can use any RawRepresentable enum (Int, Float, String) by complying with the "RealmEnum" protocol. Details in the pull request
i've create an extension, i hope it will help you
import RealmSwift
protocol RealmPersistableEnum: RawRepresentable, _OptionalPersistable { }
extension RealmPersistableEnum where RawValue: _OptionalPersistable {
static func _rlmGetProperty(_ obj: ObjectBase, _ key: PropertyKey) -> Self {
Self(rawValue: RawValue._rlmGetProperty(obj, key)) ?? Self()
}
static func _rlmGetPropertyOptional(_ obj: ObjectBase, _ key: PropertyKey) -> Self? {
guard let value = RawValue._rlmGetPropertyOptional(obj, key) else { return nil }
return Self(rawValue: value)
}
static func _rlmSetProperty(_ obj: ObjectBase, _ key: PropertyKey, _ value: Self) {
RawValue._rlmSetProperty(obj, key, value.rawValue)
}
}
Use example
enum SomeEnumInt: Int, RealmPersistableEnum {
case none = 0
case test = 1
case debug = 2
init() {
self = .none
}
}
enum SomeEnumString: String, RealmPersistableEnum {
case none
case test
case debug
init() {
self = .none
}
}
class Foo: Object {
#Persisted var v1: String
#Persisted var v2: SomeEnumInt
#Persisted var v3: SomeEnumString
}
I have an array, with custom objects.
I Would like to pop the repeated objects, with the repeated properties:
let product = Product()
product.subCategory = "one"
let product2 = Product()
product2.subCategory = "two"
let product3 = Product()
product3.subCategory = "two"
let array = [product,product2,product3]
in this case, pop the product2 or product3
Here is an Array extension to return the unique list of objects based on a given key:
extension Array {
func unique<T:Hashable>(map: ((Element) -> (T))) -> [Element] {
var set = Set<T>() //the unique list kept in a Set for fast retrieval
var arrayOrdered = [Element]() //keeping the unique list of elements but ordered
for value in self {
if !set.contains(map(value)) {
set.insert(map(value))
arrayOrdered.append(value)
}
}
return arrayOrdered
}
}
using this you can so this
let unique = [product,product2,product3].unique{$0.subCategory}
this has the advantage of not requiring the Hashable and being able to return an unique list based on any field or combination
You can use Swift Set:
let array = [product,product2,product3]
let set = Set(array)
You have to make Product conform to Hashable (and thus, Equatable) though:
class Product : Hashable {
var subCategory = ""
var hashValue: Int { return subCategory.hashValue }
}
func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.subCategory == rhs.subCategory
}
And, if Product was a NSObject subclass, you have to override isEqual:
override func isEqual(object: AnyObject?) -> Bool {
if let product = object as? Product {
return product == self
} else {
return false
}
}
Clearly, modify those to reflect other properties you might have in your class. For example:
class Product : Hashable {
var category = ""
var subCategory = ""
var hashValue: Int { return [category, subCategory].hashValue }
}
func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.category == rhs.category && lhs.subCategory == rhs.subCategory
}
If Product conforms to Equatable, where a product is equal based on it's subcategory (and you don't care about order), you can add the objects to a set, and take an array from that set:
let array = [product,product2,product3]
let set = NSSet(array: array)
let uniqueArray = set.allObjects
or
let array = [product,product2,product3]
let set = Set(array)
let uniqueArray = Array(set)
If your class conforms to protocol Hashable and you would like to keep the original array order you can create an extension as follow:
extension Array where Element: Hashable {
var uniqueElements: [Element] {
var elements: [Element] = []
for element in self {
if let _ = elements.indexOf(element) {
print("item found")
} else {
print("item not found, add it")
elements.append(element)
}
}
return elements
}
}
class Product {
var subCategory: String = ""
}
let product = Product()
product.subCategory = "one"
let product2 = Product()
product2.subCategory = "two"
let product3 = Product()
product3.subCategory = "two"
let array = [product,product2,product3]
extension Product : Hashable {
var hashValue: Int {
return subCategory.hashValue
}
}
func ==(lhs: Product, rhs: Product)->Bool {
return lhs.subCategory == rhs.subCategory
}
let set = Set(array)
set.forEach { (p) -> () in
print(p, p.subCategory)
}
/*
Product one
Product two
*/
if an item is part of set or not doesn't depends on hashValue, it depends on comparation. if your product conform to Hashable, it should conform to Equatable. if you need that the creation of the set depends solely on subCategory, the comparation should depends solely on subCategory. this can be a big trouble, if you need to compare your products some other way
Here is a KeyPath based version of the Ciprian Rarau' solution
extension Array {
func unique<T: Hashable>(by keyPath: KeyPath<Element, T>) -> [Element] {
var set = Set<T>()
return self.reduce(into: [Element]()) { result, value in
guard !set.contains(value[keyPath: keyPath]) else {
return
}
set.insert(value[keyPath: keyPath])
result.append(value)
}
}
}
example usage:
let unique = [product, product2, product3].unique(by: \.subCategory)