How to edit an Swift array in a compact form - ios

Given the following example code:
class Loader {
var posts = [Post]()
init() {
for i in 131...141 {
posts.append(PostImpl(id: i))
}
}
}
protocol Post {
var id:Int {get}
var readByUser: Bool {get set}
}
class PostImpl: Post {
var id: Int
var readByUser: Bool = false;
init(id:Int) {
self.id = id
}
}
I want to mark the item with id == 135 as readByUser=true. Is there some way of doing this in a more compact/readable/easy way than:
let loader = Loader()
for (index,post) in loader.posts.enumerated() {
if post.id == 135 {
loader.posts[index].readByUser = true
}
}

It can be this if you want short:
loader.posts[loader.posts.index(where: { $0.id == 135 })!].readByUser = true
Or check optional if you want:
if let index = loader.posts.index(where: { $0.id == 135 }) {
loader.posts[index].readByUser = true
}

var loader = Loader()
loader.posts.filter { $0.id == 135 }.forEach { $0.readByUser = true }
This also looks good but because you have an array of items that implement a protocol. If you don't tell Swift that this is a class protocol, it will assume that it can be implemented by struct.
So one more change if you're going to use it.
protocol Post : class {
var id:Int { get }
var readByUser: Bool { get set }
}

Related

How to check if every two flipped cards' current title of their buttons in an array are matching in a card matching game

I am adding in some functionalities for this iOS swift matching card game and I need to check if the first 2 buttons flipped over match. They have four types of emojis for eight cards, which means there are 4 pairs of matches. I am having trouble finding out how to check if the cards match and when they match, I need the background color of the buttons to opaque (invisible). Everything else works except the empty if statement in the concentration class within the chooseCard function. That is where I need help.
Here's all the code so you can see whats related to what:
class ViewController: UIViewController {
lazy var game = Concentration(numberOfPairsOfCards: (cardButtons.count + 1) / 2)
var flipCount = 0 {
// Property Observer
didSet { flipLabel.text = "Flips: \(flipCount)" }
}
#IBOutlet weak var flipLabel: UILabel!
#IBOutlet var cardButtons: [UIButton]!
#IBAction func touchCard(_ sender: UIButton) {
flipCount+=1
if let cardNumber = cardButtons.firstIndex(of: sender) {
game.chooseCard(at: cardNumber)
updateViewFromModel()
} else {
print ("chosen card was not in cardButtons")
}
}
var emojiChoices = ["👻", "🎃", "🙏🏾", "🦆"]
var emoji = [Int:String]()
func emoji(for card: Card) -> String {
if emoji[card.identifier] == nil, emojiChoices.count > 0 {
let randomIndex = Int(arc4random_uniform(UInt32(emojiChoices.count)))
emoji[card.identifier] = emojiChoices.remove(at: randomIndex)
}
return emoji[card.identifier] ?? "?"
}
func updateViewFromModel() {
for index in cardButtons.indices {
let button = cardButtons[index]
let card = game.cards[index]
if card.isFaceUp {
button.setTitle(emoji(for: card), for: UIControl.State.normal)
button.backgroundColor = UIColor.white
}
else {
button.setTitle("", for: UIControl.State.normal)
button.backgroundColor = UIColor.orange
}
}
}
}
import Foundation
class Concentration {
var cards = [Card]()
func chooseCard(at index: Int) {
if cards[index].isFaceUp {
cards[index].isFaceUp = false
}
else {
cards[index].isFaceUp = true
}
if cards[index].isFaceUp && cards[index].isMatched {
}
}
init(numberOfPairsOfCards: Int) {
for _ in 0..<numberOfPairsOfCards {
let card = Card()
cards += [card,card]
}
//TODO: Shuffle cards
cards.shuffle()
}
}
import Foundation
struct Card {
var isMatched = false
var isFaceUp = false
var identifier: Int
static var identifierFactory = 0
static func getUniqueIdentifier() -> Int {
Card.identifierFactory += 1
return Card.identifierFactory
}
init() {
self.identifier = Card.getUniqueIdentifier()
}
}
In your concentration class you will check for faceUp card indexes
Change your Concentration from class to Struct
struct Concentration { // instead of class Concentration
private var indexOfFaceUpCard: Int? {
get {
let faceUpCardIndices = cards.indices.filter { cards[$0].isFaceUp }
return faceUpCardIndices.count == 1 ? faceUpCardIndices.first : nil
} set {
for index in cards.indices {
cards[index].isFaceUp = (index == newValue)
}
}
}
Then you have mutating chooseCard method
mutating func chooseCard(at index: Int)
In your chooseCard method you check for matching
if !cards[index].isMatched {
if let matchIndex = indexOfFaceUpCard, matchIndex != index {
if cards[matchIndex] == cards[index] {
cards[matchIndex].isMatched = true
cards[index].isMatched = true
}
cards[index].isFaceUp = true
} else {
indexOfFaceUpCard = index
}
}
So your method look like this
mutating func chooseCard(at index: Int) {
if !cards[index].isMatched {
if let matchIndex = indexOfFaceUpCard, matchIndex != index {
if cards[matchIndex] == cards[index] {
cards[matchIndex].isMatched = true
cards[index].isMatched = true
}
cards[index].isFaceUp = true
} else {
indexOfFaceUpCard = index
}
}
}
Update your card struct
struct Card: Hashable {
var isMatched = false
var isFaceUp = false
var identifier: Int
static var identifierFactory = 0
static func getUniqueIdentifier() -> Int {
Card.identifierFactory += 1
return Card.identifierFactory
}
static func ==(lhs: Card, rhs: Card) -> Bool {
return lhs.identifier == rhs.identifier
}
init() {
self.identifier = Card.getUniqueIdentifier()
}
}

Passing data in PageViewControllers swift

I have a page controller where I added UIViewControllers and display a bunch of form in each viewcontroller. The issue I am facing now is that I need to get the data supplied in each of the forms and save it which is done in the last view controller. I have tried using delegates but the moment the next button is clicked, the previous value stored becomes nil and only the value of the latest VC is displayed. How can I pass data in this textfields. Any help is appritated.
My delegate
protocol NextDelegate: AnyObject {
func next(pageIndex: Int, model: CreatePropertyModel)
func previous(pageIndex: Int, model: CreatePropertyModel)
}
how I created array of VC
lazy var controllers: [UIViewController] = {
let descVC = DescVC()
descVC.delegate = self
let priceVC = PriceVC()
priceVC.delegate = self
let featuresVC = FeaturesVC()
featuresVC.delegate = self
let picturesVC = PicturesVC()
picturesVC.delegate = self
return [descVC, priceVC, featuresVC, picturesVC]
}()
Model Example
class CreatePropertyModel: DictionaryEncodable {
var title: String?
var desc: String?
var property_type_id: Int?
var property_sub_type_id: Int?
var location_id: Int?
var currency: String?
var price: Int?
}
For all your steps, store it in a singleton.
protocol Answer {
var isDone: Bool { get }
}
class Answer1: Answer {
static public let updatedNotification = Notification.Name("Answer1Updated")
var name: String? {
didSet {
NotificationCenter.default.post(name: Answer1.updatedNotification, object: nil)
}
}
var isDone: Bool {
return name != nil
}
}
class Answer2: Answer {
var age: Int?
var isDone: Bool {
return age != nil
}
}
class Form {
static let shared = Form()
var answers: [Answer] = [Answer1(), Answer2()]
var isDone: Bool {
return answers.allSatisfy { $0.isDone == true }
}
private init() {}
func reset() {
answers = [Answer1(), Answer2()]
}
var answer1: Answer1? {
return Form.shared.answers.filter { $0 is Answer1 }.first as? Answer1
}
var answer2: Answer2? {
return Form.shared.answers.filter { $0 is Answer2 }.first as? Answer2
}
}
Then, in your view controller, read / write values like this.
class MyViewControllerForAnswer1: UIViewController {
var answer: Answer1? {
return Form.shared.answer1
}
var name: String? {
get {
return answer?.name
}
set {
answer?.name = newValue
}
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(answerUpdated(notification:)), name: Answer1.updatedNotification, object: nil)
}
#objc func answerUpdated(notification: Notification) {
// Update your content
}
}

Replacing Element in a Set if a condition is met

I'm working with a Set of CarCagetory:
public struct Images {
static let categoryTeaser = UIImage()
}
public enum PremiumModel {
case model1, model2, model3, unknown
}
public struct CarCategory {
public let teaserImage: UIImage
public let make: String
public let model: PremiumModel
public let startPrice: Double
}
// MARK: - Equatable
extension CarCategory: Equatable {
public static func == (lhs: CarCategory, rhs: CarCategory) -> Bool {
return lhs.model == rhs.model
}
}
// MARK: - Hashable
extension CarCategory: Hashable {
public var hashValue: Int {
return model.hashValue
}
}
I'm iterating over an array of Cars and extracting a Set of categories according to the model:
var categories = Set<CarCategory>()
carSpecifications.forEach {
if PremiumModel(rawValue: $0.car.model) != .unknown {
categories.insert(CarCategory(teaserImage: Images.categoryTeaser, make: $0.car.make, model: PremiumModel(rawValue: $0.car.model), startPrice: $0.pricing.price))
}
}
This works just fine.
Now I want to keep my Set updated with the lowest price for a certain model. I'm thinking on a dictionary of [PremiumModel: Double] where I keep the lowest price for a model and at the end I update my Set accordingly, but I wonder if there is a better way.
Edit:
That's my current implementation using a dictionary. It feels rudimentary...
carSpecifications.forEach {
let model = PremiumModel(rawValue: $0.car.model)
if model != .unknown {
if let value = minPriceForModel[model] {
minPriceForModel[model] = min(value, $0.pricing.price)
} else {
minPriceForModel[model] = $0.pricing.price
}
categories.insert(CarCategory(teaserImage: Images.categoryTeaser, make: $0.car.make, model: model, startPrice: $0.pricing.price))
}
}
let sortedCategories = Array(categories.sorted(by: <))
.compactMap { (category: CarCategory) -> CarCategory in
var newCategory = category
newCategory.startPrice = minPriceForModel[category.model] ?? 0
return newCategory
}
return sortedCategories

Cannot assign to property: 'self' is immutable swift error

Inside my app I created a Protocol called "Positions" and extended the UIView class to conform to this protocol in order to add some properties to the UIView class. I used the code below.
import Foundation
import UIKit
protocol Positions {
var initialPositions: [CGRect] {get set}
var finalPositions: [CGRect] {get set}
var positionsAreDefined: Bool {get}
}
public enum PositionsType {
case initial
}
private var initialPositionKey: UInt = 0
private var finalPositionKey: UInt = 0
extension Positions {
var initialPositions: [CGRect] {
get {
if objc_getAssociatedObject(self, &initialPositionKey) != nil {
return objc_getAssociatedObject(self, &initialPositionKey) as! [CGRect]
} else {
return [] as! [CGRect]
}
}
set {
objc_setAssociatedObject(self, &initialPositionKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
var finalPositions: [CGRect] {
get {
if objc_getAssociatedObject(self, &finalPositionKey) != nil {
return objc_getAssociatedObject(self, &finalPositionKey) as! [CGRect]
} else {
return [] as! [CGRect]
}
}
set {
objc_setAssociatedObject(self, &finalPositionKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
var positionsAreDefined: Bool {
get {return initialPositions.count != 0 && finalPositions.count != 0 && initialPositions.count == finalPositions.count}
}
var positionsCount: Int {
get {return initialPositions.count}
}
}
extension UIView: Positions {}
Well, today I tried to extend the UIView class with a new method that changes the added-by-me properties but the compiler gave me this error on the line where I tried to modify the properties value:
Cannot assign to property: 'self' is immutable
Here's the method:
public func horizontallyInContainer(withShift shift: CGFloat, forExpansionNumber index: Int, forPositionType type: PositionsType) {
if self.positionsAreDefined && self.superview != nil {
if type == .initial {
var newPositions = self.initialPositions
newPositions[index].origin.x = self.superview!.bounds.width * 0.5 - self.bounds.width + shift
self.initialPositions = newPositions //Here happens the error
} else {
var newPositions = self.finalPositions
newPositions[index].origin.x = self.superview!.bounds.width * 0.5 - self.bounds.width + shift
self.finalPositions = newPositions //Also here happens
}
}
}
Can anyone explain me the nature of that error?
Make the protocol become reference type
protocol Positions: AnyObject {

How to use generics as params?(Swift 2.0)

Code in playground is here
class ProductModel {
var productID : Int = 0
init(id:Int) {
productID = id
}
}
protocol GenericListProtocol {
typealias T = ProductModel
var list : [T] { get set }
var filteredlist : [T] { get set }
func setData(list : [T])
}
extension GenericListProtocol {
func setData(list: [T]) {
list.forEach { item in
guard let productItem = item as? ProductModel else {
return
}
print(productItem.productID)
}
}
}
class testProtocol {
class func myfunc<N:GenericListProtocol>(re:N){
var list : [ProductModel] = [ProductModel(id: 1),ProductModel(id: 2),ProductModel(id: 3),ProductModel(id: 4)]
re.setData(list)
}
}
But in the line re.setData(list)
get compile error:
Cannot convert value of type '[ProductModel]' to expected argument
type '[_]'.
My Question is How to use setData method in GenericListProtocol?
Anyone could help will be appreciated.
Moving the ProductModel type into the extension and removing the constraint from the generic protocol seems to work.
class ProductModel {
var productID : Int = 0
init(id:Int) {
productID = id
}
}
protocol GenericListProtocol {
typealias T
var list : [T] { get set }
var filteredlist : [T] { get set }
func setData(list : [T])
}
extension GenericListProtocol {
func setData(list: [ProductModel]) {
list.forEach { item in
print(item.productID)
}
}
}
class testProtocol {
class func myfunc<N:GenericListProtocol>(re:N) {
let list : [ProductModel] = [ProductModel(id: 1),ProductModel(id: 2),ProductModel(id: 3),ProductModel(id: 4)]
re.setData(list)
}
}
I found this question interesting and thought how best we could solve it in generic way.
protocol Product {
var productID : Int {get set}
}
class ProductModel: Product {
var productID : Int = 0
init(id:Int) {
productID = id
}
}
protocol GenericListProtocol {
typealias T : Product
var list : [T] { get set }
var filteredlist : [T] { get set }
}
extension GenericListProtocol {
func setData(list: [T]) {
list.forEach { item in
print(item.productID)
}
}
}
class GenericListProtocolClass : GenericListProtocol
{
typealias T = ProductModel
var intVal = 0
var list = [T]()
var filteredlist = [T]()
}
class testProtocol {
class func myfunc(re: GenericListProtocolClass){
let list : [ProductModel] = [ProductModel(id: 1),ProductModel(id: 2),ProductModel(id: 3),ProductModel(id: 4)]
re.setData(list)
}
}
let temp = GenericListProtocolClass()
testProtocol.myfunc(temp)
Appreciate your thought and suggestion if it can be improved further.

Resources