Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Swift's firstIndex method #4

Open
wants to merge 1 commit into
base: App
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions Battle Cards/Battle Cards/Array+Identifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,7 @@

import Foundation

// Extends the capabilities of Array if the element inside is identifiable
extension Array where Element: Identifiable {

// Find the index of the first element using the id
func firstIndexOf(element: Element) -> Int? {

// Basic loop through the array
for index in 0..<self.count {

// If current pointed item has the same index as the one which is asked, return it
if self[index].id == element.id {
return index
}
}

// find none return nil
return nil
}

}

// Extends the capabilities of Array
extension Array {
var firstHalf: Array<Element> {
self.indices.filter{ $0 < self.count / 2 }.map{ self[$0] }
Expand Down
2 changes: 1 addition & 1 deletion Battle Cards/Battle Cards/BattleSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct BattleSystem<Color, Element> where Color: Equatable, Element: Equatable {
}

mutating func choose(card: Card, makeColor: (Int) -> Color, makeElement: (Int) -> Element) {
if let cardIndex: Int = playerHand.firstIndexOf(element: card) {
if let cardIndex: Int = playerHand.firstIndex(of: card) {
// Retrieve card from hand
let chosen = playerHand.remove(at: cardIndex)

Expand Down
2 changes: 1 addition & 1 deletion Battle Cards/Battle Cards/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ struct ContentView: View {
private func chooseCard(card: BattleSystem<Color, String>.Card) {

// Check if selected card is real
guard emojiCardGame.playerHand.firstIndexOf(element: card) != nil else {
guard emojiCardGame.playerHand.firstIndex(of: card) != nil else {
return
}
audio.playSound(.flip)
Expand Down