Swift: For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence' - swift

I'm running into a weird error with a for in loop and an array. it says
For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence'
Which doesn't make any sense... it knows it's an Array...
The for loop in question:
var transcriptCandidate = decoded.transcripts[0].tokens
var words = [String]()
var timestamps = [Int]()
var workingString = ""
var lastTimestamp = -1
for (x, token) in transcriptCandidate {
let text = token.text
let timestamp = token.startTime
if(lastTimestamp == -1){
lastTimestamp = timestamp.toInt()
}
Here's the definition of the class that contains the array I'm trying to iterate through:
public struct DeepSpeechCandidateTranscript {
/// Array of DeepSpeechTokenMetadata objects
public private(set) var tokens: [DeepSpeechTokenMetadata] = []
/** Approximated confidence value for this transcript. This corresponds to
both acoustic model and language model scores that contributed to the
creation of this transcript.
*/
let confidence: Double
internal init(fromInternal: CandidateTranscript) {
let tokensBuffer = UnsafeBufferPointer<TokenMetadata>(start: fromInternal.tokens, count: Int(fromInternal.num_tokens))
for tok in tokensBuffer {
tokens.append(DeepSpeechTokenMetadata(fromInternal: tok))
}
confidence = fromInternal.confidence
}
}
Thanks!

You can either do this, where x is the index and token is the element:
for (x, token) in transcriptCandidate.enumerated() {
}
Or this if you don't need the index:
for token in transcriptCandidate {
}

Related

Swift - How to Change Multi-Level JSON Struct Single Element Value on Condition

I have multi-level Struct that converts complex JSON data to a Struct. What I am struggling is to change the dataStruct.group.point.presentValue element value on condition.
var dataStruct : DataStruct = load("jsonfile.json")
struct DataStruct : Codable {
let name: String
var group: [groupData]
}
struct groupData: Codable, Hashable {
let id, name : String
var point : [pointInfo]
}
struct pointInfo : Codable, Hashable {
let id : String
let address : address
let name : String
var presentValue : String
}
struct address: Codable, Hashable {
let index: String
let type: String
}
I have tried the following map function, but the compiler complains that the Group in ForEach is 'let' constant.
Basically the function is supposed to compare address.index field in the Struct to the passed pointNo variable, and once it has been found (unique), point.presentValue is changed to the new value.
What is the correct way to achieve this?
func updatePresentValue(pointNo : String) {
dataStruct.group.forEach { Group in
Group.point = Group.point.map { point -> pointInfo in
var p = point
if point.address.index == pointNo {
p.presentValue = "New value"
return p
}
else { return p }
}
}
}
Basically there are two ways.
Extract the objects by assigning them to variables, modify them and reassign them to their position in dataStruct.
Enumerate the arrays and modify the objects in place.
This is an example of the second way
func updatePresentValue(pointNo : String) {
for (groupIndex, group) in dataStruct.group.enumerated() {
for (pointIndex, point) in group.point.enumerated() {
if point.address.index == pointNo {
dataStruct.group[groupIndex].point[pointIndex].presentValue = "New value"
}
}
}
}
It gets more complicated when dealing with multilevel structures but here is one way to do it where we first enumerate over group so we get both the object and the index of the object for each iteration so we can use this index when updating the group array. The inner struct is updated using a mutable copy of point
for (index, group) in dataStruct.group.enumerated() {
if group.point.contains(where: { $0.address.index == pointNo }) {
var copy = group
copy.point = group.point.reduce(into: []) {
if $1.address.index == pointNo {
var pointCopy = $1
pointCopy.presentValue = "new value"
$0.append(pointCopy)
} else {
$0.append($1)
}
}
dataStruct.group[index] = copy
}
}

Filter Realm Results array returns elements that should be filtered

Any ideas why this filter is not working correctly ?
for item in activeItems {
print("item.product: \(item.product), \(item.spaceRequired)")
}
returns
item.product: nil, 40.0
Filtering where product is nil
let f1 = activeItems.filter{$0.product != nil}
print("f1: \(f1)")
print("f1.count: \(f1.count)")
returns a count of ZERO but the array still appears to contain an item
f1: LazyFilterSequence<Results<AssortmentItem>>(_base: Results<AssortmentItem> <0x109ce2c90> (
[0] AssortmentItem {
...
f1.count: 0
And then filtering and mapping just spaceRequired
let f11 = f1.filter{$0.product!.isProduct == true}.map({$0.spaceRequired})
print("f11: \(f11)")
returns the same array with a single item
f11: LazyMapSequence<LazyFilterSequence<Results<AssortmentItem>>, Double>(_base: Swift.LazyFilterSequence<RealmSwift.Results<Merchandise_Manager.AssortmentItem>>(_base: Results<AssortmentItem> <0x109ce2c90> (
[0] AssortmentItem {
And then trying to reduce crashes
let w = f11.reduce(0,+)
This seems to fix the problem
let width = Array(activeItems.filter{$0.product != nil}).filter{$0.product!.isProduct == true}.map({$0.spaceRequired}).reduce(0,+)
Is this a bug in Swift 5 or in Realm ?
EDIT: It looks like this is a bug in Realm's handling of things.
To be a bit cleared below is a more complete set of the Realm objects.
import Foundation
import RealmSwift
let activeDate: NSDate = Date() as NSDate
let defaultWidth: Double = 40.0
class MyObject: Object {
#objc dynamic var number: Int = 0
#objc dynamic var name: String?
let items = List<ChildObject>()
}
extension MyObject {
var activeItems: Results<ChildObject> {
let activeDate = activeDate // Some globally defined value
let active = items.filter("startDate <= %# && (endDate >= %# || endDate == nil)", activeDate, activeDate).sorted(byKeyPath: "number")
return active
}
/// Works Correctly
var totalWidth: Double {
let width = Array(activeItems.filter{$0.product != nil}).filter{$0.product!.isProduct == true}.map({$0.spaceRequired}).reduce(0,+)
let width2 = Array(activeItems.filter{$0.product == nil}.map({$0.spaceRequired})).reduce(0,+)
return width+width2
}
/// Crashes
var totalWidth: Double {
let width = activeItems.filter{$0.product != nil}.filter{$0.product!.isProduct == true}.map({$0.spaceRequired}).reduce(0,+)
let width2 = activeItems.filter{$0.product == nil}.map({$0.spaceRequired}).reduce(0,+)
return width+width2
}
}
class ChildObject: Object {
#objc dynamic var parent: MyObject?
#objc dynamic var number: Int = 0
#objc dynamic var product: Product?
#objc dynamic var name: String?
#objc dynamic var spaceRequired: Double = 40.0
#objc dynamic var startDate: NSDate?
#objc dynamic var endDate: NSDate?
}
extension ChildObject {
var spaceRequired: Double {
if let p = product {
return p.width
} else {
return defaultWidth
}
}
}
class Product: Object {
#objc dynamic var isProduct: Bool = false
#objc dynamic var width: Double = 30.0
}
There's a couple of issues at work here but the main problem is that Realm Results are live updating; while you can filter data using the Swifty
let f1 = activeItems.filter{$0.product != nil}
it's going to give intermittent results since Realm doesn't know which items are filtered or not as .filter { is not a Realm function and Realm won't know what to update within the results.
You should generally use the built in Realm filtering mechanism
let results = realm.objects(ItemClass.self).filter("product != nil")
Those results will be live updating - if an object leaves the filter parameter, the results follow that. If an object matches the filter the results are updated as well.
I believe this Github issue #2138 provides some more light on the issue.
If you absolutely need static data, then I would suggest extending the Results class to return an array; like this
extension Results {
func toArray() -> [Element] {
return compactMap { $0 }
}
}
Keeping in mind this will use more memory as Realm objects are lazy loaded and and array isn't.
EDIT:
There's some additonal information in the question so I crafted up a simple example trying to replicate the issue. There's a HouseClass object which contains a List of RoomClass objects and then the HouseClass is extended to return the total width of all of the rooms in its list.
class RoomClass: Object {
#objc dynamic var room_name = ""
#objc dynamic var width = 0
#objc dynamic var length = 0
#objc dynamic var belongs_to_house: HouseClass!
}
class HouseClass: Object {
#objc dynamic var house_id = NSUUID().uuidString
#objc dynamic var house_name = ""
let rooms = List<RoomClass>()
override static func primaryKey() -> String? {
return "house_id"
}
}
extension HouseClass {
var totalWidth: Int {
let width = Array(rooms).map {$0.width}.reduce(0,+)
return width
}
var anotherTotalWidth: Int {
let width = rooms.map {$0.width}.reduce(0,+)
return width
}
}
and then the code to get all of the houses and output their room widths based on two different functions (see the HouseClass extension)
let houseResults = realm.objects(HouseClass.self)
for house in houseResults {
let w0 = house.totalWidth
print(w0)
let w1 = house.anotherTotalWidth
print(w1)
}
I added 100 houses each with three rooms and ran the above code several times without crash.
Count of f1 is 0 so map is not worked.
You can optimize your width calculation as following
let width = activeItems
.filter { $0.product?.isProduct ?? false }
.map { $0.spaceRequired }
.reduce(0,+)

Remove duplicated in a Struct Array

I am filtering an array that can have a value where there are multiple Models of the same name, only they have different model numbers.
Variables
var modelArray = [model]()
Struct
struct model {
var modelName = String();
var modelNumber = String();
var manufacturer = String();
var phiTypeCode = String();
var phiTypeDesc = String();
}
Filter
var filteredArray = self.modelArray.filter { $0.manufacturer.range(of: manufacturerVar, options: .caseInsensitive) != nil }
This produces the correct filtered Array, only due to the possibility of similar models with different model numbers, I am trying to remove duplicates from filteredArray.
Fairly new to swift I don't have a great deal of experience making the struct hashable to be able to use the suggested solutions.
Hopefully this is more clear
First off, I tried making a sample in my PlayGround.
Conform your model model to the protocal Equatable, like so:
struct Car: Equatable {
var modelName = String()
var manufacturer = String()
init(modelName: String, manufacturer: String) {
self.modelName = modelName
self.manufacturer = manufacturer
}
static func == (lhs: Car, rhs: Car) -> Bool {
return lhs.modelName == rhs.modelName
}
}
In the code above, we're assuming that the modelName is the primary key of your model.
Now make a function that enumerates your data source and returns a new data source after checking the element one by one, like so:
// returns unique array
func unique(cars: [Car]) -> [Car] {
var uniqueCars = [Car]()
for car in cars {
if !uniqueCars.contains(car) {
uniqueCars.append(car)
}
}
return uniqueCars
}
Finally, you now have the function to generate a new unique data source.
Example:
// Variable
var cars = [Car]()
// Adding data models to data source
let car1 = Car(modelName: "Kia Picanto", manufacturer: "Kia")
let car2 = Car(modelName: "Honda XX", manufacturer: "Honda")
let car3 = Car(modelName: "Honda XX", manufacturer: "Honda")
cars.append(car1)
cars.append(car2)
cars.append(car3)
// Now contains only two elements.
let uniqueCars = unique(cars: cars)

Listing all class attributes swift 3

I'm trying to print all the values from an object that inherits from a class, here is my example:
I create the class:
class Pokemon {
var name: String?
var type: String?
var level: Int?
var exp = 0.0
}
Create the object and assign some values:
var pikachu = Pokemon()
pikachu.name = "Pika Pika"
pikachu.level = 1
pikachu.type = "electricity"
pikachu.exp = 0
Now I would like to loop through all the pikachu object attributes and print the values. I'm thinking in a for each loop but I'm not sure how to implement it.
I know I can do something like this:
func printStats(pokemon: Pokemon) {
if pokemon.name != nil {
print(" name: \(pokemon.name!)\n level:\(pokemon.level!)\n type:\(pokemon.type!)\n exp: \(pokemon.exp!)")
}
}
printStats(pokemon: pikachu)
output:
name: Pika Pika
level:1
type:electricity
exp: 0.0
But I just want to loop through all values, instead of explicit writing every attribute in the function.
I found it the way of doing it:
let pokeMirror = Mirror(reflecting: pikachu)
let properties = pokeMirror.children
for property in properties {
print("\(property.label!) = \(property.value)")
}
output:
name = Optional("Pika Pika")
type = Optional("electricity")
level = Optional(1)
exp = Optional(0.0)
and if you want to remove the "Optional" just initialize the attributes.
Looks like a duplicate of Does Swift support reflection?
Alternatively, you can use a dictionary to store the attributes of Any? type.
e.g.
class Pokemon {
var attributes = [String:Any?]()
}
var pikachu = Pokemon()
pikachu.attributes["name"] = "Pika Pika"
pikachu.attributes["level"] = 1
pikachu.attributes["type"] = "electricity"
pikachu.attributes["exp"] = 0
func printStats(pokemon: Pokemon) {
pokemon.attributes.forEach { key, value in
if let value = value {
print("\(key): \(value)")
}
}
}
In Swift 5 you can create a new func in your class:
func debugLog() {
print(Mirror(reflecting: self).children.compactMap { "\($0.label ?? "Unknown Label"): \($0.value)" }.joined(separator: "\n"))
}
And then call it with MyObject().debugLog()
use Mirror API to get instance's properties
if you are developing iOS app, using NSObject, you may want to override description. Then can use print to print the instance.
A mirror describes the parts that make up a particular instance, such as the instance’s stored properties, collection or tuple elements, or its active enumeration case.
class YourClass: NSObject {
public override var description: String {
var des: String = "\(type(of: self)) :"
for child in Mirror(reflecting: self).children {
if let propName = child.label {
des += "\(propName): \(child.value) \n"
}
}
return des
}
}
let instance = YourClass()
print(instance)
see more in Reflection in Swift

Random values of Int in dictionary instead of zeros

I have an Avatar struct. This struct has properties called elements that hold several parts of the face like Eyes, Mouth and so on. In the init(withGender: AvatarGender) method. I want all these elements to have their default to zero.
But instead of having zeros I get huge random Ints. I can't figure out why!
struct Avatar {
var gender: AvatarGender
var skin: AvatarSkinColor
var elements: [AvatarElement: Int]
var rawString: String {
// a computed property
}
init(withGender: AvatarGender) {
gender = withGender
skin = .White
elements = [AvatarElement: Int]()
var allElementsType = [AvatarElement]()
if gender == .Man {
allElementsType = AvatarElement.allMaleValues
} else if gender == .Woman {
allElementsType = AvatarElement.allFemaleValues
}
for element in allElementsType {
elements[element] = 0 // <= This doesn't work !!!
}
}
init(fromRawString: String) {
// Another init method that works properly
}
}
Above AvatarGender, AvatarSkinColor and AvatarElement are enums.