Swift I can't append two different strings on the same array - arrays

I have an API call that brings an array of CustomDescription. Each element of this array has a name, so, I created the following:
A string array named namArray
A for loop that runs my CustomDescription array, and append it's elements to namArray.
My problem comes when I print my namArray after my for loop, and it just shows one element inside the array, it doesn't append the elements corresponding to the calls.
Here is my code:
func getTypes(typesUrl: String, handler: #escaping([CustomDescription]) -> Void) {
AF.request(typesUrl).validate().responseTypes { (types) in
guard let typeData = types.value,
let typeNames = typeData.names else { return }
var namArray = [String]()
for name in typeNames {
guard let types = name.name else { return }
if name.language?.name == "es" {
namArray.append(types)
}
}
print(namArray)
handler(typeNames)
}
}
And my console logs the following:
You've got 94 species successfully
["Planta"]
["Veneno"]
I want to get ["Planta", "Veneno"] instead, but can't figure out how.

Related

SwiftUI - Return values from array of structs are all in one row

I am calling a function in order to do a select statement in a bundled SQLite database. The function returns an array of structs. The database is being read correctly as I have put some print commands in the code. However the final array only has 1 row in it, which contains all the data, instead of 16 rows of structs.
The struct code, which is in databaseHelper.swift, is...
struct ButtonData: Hashable {
let english: String
let categoryID: Int
let indonesian: String
}
The database code, in databaseHelper, is
class DatabaseHelper {
var buttonVars = [ButtonData]()
var database: Connection!
let buttonsTable = Table("Button")
let english = Expression<String>("english")
let category = Expression<String>("category")
let categoryID = Expression<Int>("ID")
let filename = Expression<String>("filename")
let indonesian = Expression<String>("indonesian")
init() {
do {
let path = Bundle.main.path(forResource: "sga", ofType: "db")!
let database = try Connection(path, readonly: true)
self.database = database
print("Database initialized at path \(path)")
} catch {
print("error")
}
}
func queryDatabase(passedCategory: String) -> [ButtonData] {
do {
let buttons = try self.database.prepare(self.buttonsTable.filter(self.category==passedCategory))
for row in buttons {
print("English: \(row[self.english]), ID: \(row[self.categoryID]), Indonesian: \(row[self.indonesian])")
// buttonVars.append(ButtonData(english: row[english], categoryID: row[categoryID], indonesian: row[indonesian]))
buttonVars.append(ButtonData(english: row[english], categoryID: row[categoryID], indonesian: row[indonesian]))
}
} catch {
print(error)
}
print(buttonVars[0])
print(buttonVars[1])
print(buttonVars[2])
print(buttonVars[3])
print(buttonVars[4])
print(buttonVars[5])
print(buttonVars[6])
print(buttonVars[7])
print(buttonVars[8])
print(buttonVars[9])
print(buttonVars[10])
print(buttonVars[11])
print(buttonVars[12])
print(buttonVars[13])
print(buttonVars[14])
print(buttonVars[15])
return buttonVars
}
}
The function code, which is in SoundPageView.swift (this page calls the database function), is...
func getArrayValues() {
let buttonRows = [DatabaseHelper().queryDatabase(passedCategory: category)]
let btnCount: Int = buttonRows.count
print(btnCount)
print(buttonRows[0])
}
The print values in the console show me that btnCount = 1 but before the array is returned, it is made of 16 rows. It is only after it is returned that it is reduced to 1 row.
Can anyone tell me what I'm doing wrong? I don't know how to access the data. Thanks.
I'm not sure why you are putting the brackets in this call:
let buttonRows = [DatabaseHelper().queryDatabase(passedCategory: category)]
The func queryDatabase returns an array on its own. I think it should be this:
let buttonRows = DatabaseHelper().queryDatabase(passedCategory: category)
Otherwise your result will be an array with one entry, which is the result of the call to queryDatabase.

Loop or map Core Data array to get every occurence of an object

I would like to iterate through a CoreData Managed Object array to get every code occurence and call a dataUpdate("\(code)") function with each code string as a param.
func updateWalletValue() {
// CryptosMO is the Managed Object containing `code` values among others (`amountValue` for example).
var items : [CryptosMO] = []
if CoreDataHandler.fetchObject() != nil {
items = CDHandler.fetchObject()!
}
total = items.reduce(0.0, { $0 + Double($1.amountValue)! } )
WalletTableViewController.staticTotal = total
// What I am trying to do:
for code in items.code {
CryptoDataCall.init().dataUpdate("\(code)")
}
}
How can I access code occurences and use them in a loop or map them to call the function?
Okay found the solution myself :)
let codes = items.map { $0.code }
for code in codes {
CryptoDataCall.init().dataUpdate("\(code)")
}
Iterate over the items array, and access the code member in
the closure:
for items in items { item in
// do something with `item.code` ...
}
Or with shorthand parameter names:
for items in items {
// do something with `$0.code` ...
}
Or with forEach:
items.forEach {
// do something with `$0.code` ...
}

Swift - Get first item in array, and return element and indices

I want to return the first item in a given array of custom objects and return an array of indices and the first custom object element so I can wrap a guard statement around it.
ie:
let firstOrder = existingOrders.enumerated().flatMap{$0,$1}.first
or attempt #1
let array = existingOrders.enumerated().map { (a, b) in return [$0.index : $1.element] }.first
or attempt #2
let array = existingOrders.enumerated().map { ($0.offset, $0.element) }.first
print (array)
This isn't returning the actual object; and it seems to return a tuple.
where
existingOrders = [ExistingOrder, EngineYard.ExistingOrder, EngineYard.ExistingOrder]
it returns the following;
[(0, EngineYard.ExistingOrder), (1, EngineYard.ExistingOrder), (2, EngineYard.ExistingOrder)]
attempt #3;
let array = existingOrders.enumerated().map { ($0.offset, $0.element) }
print (array)
guard let firstOrder = array.first else {
break
}
print (firstOrder) // should be a tuple of index and custom object
How do I grab the optional first item in an array and return index and element?
Many thanks
Edit. the reason I'm doing this is so that I can transfer the correct object to another class.
// transfer all
while (factory.existingOrders.count > 0) {
let array = myOrderBook.existingOrders.enumerated().map { ($0.offset, $0.element) }
guard let firstOrder = array.first else {
break
}
let index = (firstOrder.0)
factory.orderBook.transfer(index: index, destination: .completedOrder)
}
Where the Orderbook is a class;
Factory {
var orderBook:OrderBook = OrderBook()
}
OrderBook {
var existingOrders: [ExistingOrder] = [ExistingOrder]()
var completedOrders: [CompletedOrder] = [CompletedOrder]()
}
And the idea is that I want to transfer an object from existing orders to completed orders and vice versa
The function requires an index, but I guess I could refactor it so I can transfer an object instead.
The answer I was looking for was;
let array = myOrderBook.existingOrders.enumerated().map { ($0.offset, $0.element) }
However, I found that my code needed to be refactored.
Thanks.
Issue closed.

Swift - Shuffling a filtered array of structs doesn't change originating array

I'm writing a card game with a number of cards collected together in one array.
The data structure of the cards is the same, but the data is different.
Note: My shuffle does work.
I'm wanting to filter this array, and only shuffle the cards I have filtered.
However, whilst I can shuffle the cards, I've noticed that my originating array of cards does not change at all.
I believe that this issue is caused because my card model is a struct and not a class.
More background info.
Whilst the data for each card is different, the structure is exactly the same; both types have a name, and a numeric value.
This is modelled thusly;
enum FSCardType: Int {
case property
case anotherType
case yetAnotherType
}
// Card Model
struct FSCard {
let type: FSCardType
let name: String
let value: Int
var description: String {
return ("Name: \(self.name) Value: \(self.value), Type: \(self.type)")
}
}
I create my cards in a static function like this:
class FSCardAPI: NSObject {
public static func createCards() -> [FSCard] {
var cards:[FSCard] = [FSCard]()
let properties:[FSCard] = FSCardAPI.createProperties()
cards.append(contentsOf: properties)
return cards
}
public static func shuffleCards(cards:[FSCard]) -> [FSCard] {
let shuffled:[FSCard] = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: cards) as! [FSCard]
return shuffled
}
fileprivate static func createProperties() -> [FSCard] {
var properties:[FSCard] = [FSCard]()
for idx in 1...30 {
let property:FSCard = FSCard(type: .property, name: "Property #\(idx)", value: idx, owner: nil)
properties.append(property)
}
return properties
}
}
Okay, so now I only want to shuffle my property cards within this cards array.
So in my XCTest file first filter all cards that are of type: property
func testShuffleProperties() {
var propertyCards = gameModel.cards.filter { (fs:FSCard) -> Bool in
return (fs.type == .property)
}
propertyCards = FSCardAPI.shuffleCards(cards: propertyCards)
print(propertyCards)
print(" ---- ")
print(gameModel.cards)
}
This calls:
// Inside my FSCardAPI
public static func shuffleCards(cards:[FSCard]) -> [FSCard] {
let shuffled:[FSCard] = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: cards) as! [FSCard]
return shuffled
}
The issue:
When I run my XCTest, I notice that whilst the propertyCards is shuffled, my gameModel.cards are not shuffled;
Test Case 'testShuffleProperties' started.
// contents of `propertyCards`
[FSCard(type: FSCardType.property, name: "Property #4", value: 4, owner: nil),
FSCard(type: FSCardType.property, name: "Property #16", value: 16, owner: nil),
// .. etc
// contents of `gameModel.cards`
[FSCard(type: FSCardType.property, name: "Property #1", value: 1, owner: nil),
FSCard(type: FSCardType.property, name: "Property #2", value: 2, owner: nil),
// .. etc
Summary
I have an array of cards (ie: 30 cards)
Cards are separated by types (ie: property)
I want to filter the property cards and shuffle those cards only
I want the original array to reflect these changes
In my test, the array gets shuffled; but the original array remains the same.
One way I can think of is that I do all the shuffling, and then sort the array by card types and then update the gameModel.cards, but that seems a bit over the top.
Another obvious way I can think about solving this is to change my struct to a class or; perhaps I need another layer in between the struct?
So my query is:
How do I filter an array of structs, only shuffle those results and change the state of the originating array?
Many thanks
Edit:
The properties array is the only item to shuffle.
If I add another array to the list it should not shuffle the entire contents.
IE; if I do this:
public static func createCards() -> [FSCard] {
var cards:[FSCard] = [FSCard]()
let properties:[FSCard] = FSCardAPI.createProperties()
let cheqs:[FSCard] = FSCardAPI.createCheques()
cards.append(contentsOf: properties)
cards.append(contentsOf: cheqs)
return cards
}
I should only shuffle the property cards within themselves without impacting the cheqs.
I guess I could make it easier and just have 2 arrays, but at the same time I think there is no reason to do this because the data structure is the same.
You are not assigning to gameModel.cards and not actually changing the array. so I would not expect gameModel.cards to be shuffled.
You should either just assign the shuffled array back to gameModel.cards like so:
func testShuffleProperties() {
var propertyCards = gameModel.cards.filter { (fs:FSCard) -> Bool in
return (fs.type == .property)
}
propertyCards = FSCardAPI.shuffleCards(cards: propertyCards)
gameModel.cards = propertyCards
print(propertyCards)
print(" ---- ")
print(gameModel.cards)
}
Or if you want to mutate the array directly you should look at passing the cards by reference, or in Swift.. using an inout parameter. An inout parameter passes the value to the function by reference, or passed the memory addres of the value, so that it can be modified directly. Check the shuffle cards function below (how it is defined and used)
(I replaced the shuffle function with a swift extension for ease of use in a playground)
extension MutableCollection where Indices.Iterator.Element == Index {
/// Shuffles the contents of this collection.
mutating func shuffle() {
let c = count
guard c > 1 else { return }
for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
guard d != 0 else { continue }
let i = index(firstUnshuffled, offsetBy: d)
swap(&self[firstUnshuffled], &self[i])
}
}
}
extension Sequence {
/// Returns an array with the contents of this sequence, shuffled.
func shuffled() -> [Iterator.Element] {
var result = Array(self)
result.shuffle()
return result
}
}
enum FSCardType: Int {
case property
case anotherType
case yetAnotherType
}
// Card Model
struct FSCard {
let type: FSCardType
let name: String
let value: Int
var description: String {
return ("Name: \(self.name) Value: \(self.value), Type: \(self.type)")
}
}
class FSCardAPI: NSObject {
public static func createCards() -> [FSCard] {
var cards:[FSCard] = [FSCard]()
let properties:[FSCard] = FSCardAPI.createProperties()
cards.append(contentsOf: properties)
return cards
}
public static func shuffleCards(cards:inout [FSCard]) {
cards = cards.shuffled()
}
fileprivate static func createProperties() -> [FSCard] {
var properties:[FSCard] = [FSCard]()
for idx in 1...30 {
let property:FSCard = FSCard(type: .property, name: "Property #\(idx)", value: idx)
properties.append(property)
}
return properties
}
}
var cards = FSCardAPI.createCards()
FSCardAPI.shuffleCards(cards: &cards)
Output:
Read up on inout parameters here in the In-Out parameters section.
Exert from the documentation:
Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.
You write an in-out parameter by placing the inout keyword right before a parameter’s type. An in-out parameter has a value that is passed in to the function, is modified by the function, and is passed back out of the function to replace the original value. For a detailed discussion of the behavior of in-out parameters and associated compiler optimizations, see In-Out Parameters.
You can only pass a variable as the argument for an in-out parameter. You cannot pass a constant or a literal value as the argument, because constants and literals cannot be modified. You place an ampersand (&) directly before a variable’s name when you pass it as an argument to an in-out parameter, to indicate that it can be modified by the function.
EDIT: Try this updated shuffle function, pass in the whole array and see if does what you need.
public static func shuffleCards(cards:inout [FSCard]) {
let propertyCards = cards.filter({ $0.type == .property })
let indexes = propertyCards.map { Int(cards.index(of: $0)!) }
let shuffledCards = propertyCards.shuffled()
for (idx, val) in indexes.enumerated() {
cards[val] = shuffledCards[idx]
}
}
The problem is that after you shuffle your property cards, you are not doing anything with them. You should replace property cards in your original card list with the ones in your shuffled property card list.
var propertyCardsShuffledOriginalArray = originalArray.map {
var card = $0
if $0.type == .property {
card = shuffledPropertyCards.first as! FSCard
shuffledPropertyCards.removeFirst()
}
return card
}
propertyCardsShuffledOriginalArray is what you need

Swift: Search string in sentence in Array

I am developing search textfield. I have an array of Strings that looks like this....
var keywords:[String] = ["dance, music and hippo", "apple juice, tomato"]
I tried to use function contains but its not doing well.
for i in 0..<keywordsArr.count {
if(!keywordsArr[i].contains(searchTextField.text!)){
print("removed \(keywordsArr[i])")
keywordsArr.removeAtIndex(i)
}
}
this is extension that i am using.
extension String {
func contains(find: String) -> Bool{
return self.rangeOfString(find) != nil
}
}
the result should be i am calling function with text: "music" and it should return index of that string
You can achieve this very succinctly, using filter, which returns an array with only elements for which the closure holds. You should also use optional binding to avoid using a force unwrap of searchTextField.text
if let text = searchTextField.text {
keywordsArr = keywordsArr.filter { $0.contains(text) }
}
First you shouldn't edit the array size inside the loop, because each time you iterate you recalculate its size with "keywordsArr.count", and that will cause an exception You can use those few lines to return the index of the string if it's contained in your array, or -1 if not. good luck
func doesContain(array:[String], searchString:String) -> Int {
let size = array.count
for i in 0..<size {
if array[i].rangeOfString(searchString) != nil {
print("exists")
print("removed \(array[i])")
return i
}
}
return -1
}
var keywords:[String] = ["dance, music and hippo", "apple juice, tomato"]
doesContain(keywords, searchString: "music")

Resources