Swift Issue with adding value to array for use within UITableView Controller - swift

Confused as to why getting error "Cannot subscript a value of type 'inout [[String]]' (aka 'inout Array>'). Within a working table view class
(Originally followed Jared Davidson tutorial https://www.youtube.com/watch?v=pR6dR-vVZeY)
var secondArray = [SecondTable]()
let latest = ViewController().getArrayListLast()
var latestClass = latest.getFullClasses()
print(latestClass[0])
for i in 0...(latest.getAssetClasses().count)
{
if SecondTable(secondTitle: latestClass[i]) != nil
{
secondArray = secondArray.append(SecondTable(secondTitle: latestClass[i]))
}
}

append(_:) is mutating function and it doesn't return anything.
Now your code making 3 more mistakes.
As #vadian mentioned in comment ViewController() will never work when using storyboard.
It will give you run time crash index out of bounds because you have provide for loop till the array count but it should be till the less one of the array count, so it should like for i in 0..<(latest.getAssetClasses().count)
You are creating two times object with init of SecondTable to just check for the nil instead of that you can use if let.
if let secondTable = SecondTable(secondTitle: latestClass[i]) {
secondArray.append(secondTable)
}

Related

No exact matches in call to initializer when initializing Data in AppStorage

I'm learning how to store custom types in AppStorage, and came across an issue. In this simplified example, I'm trying to save an empty Int array to AppStorage once the view is created.
The following code gives me the error, No exact matches in call to initializer . I know that this error usually means there are mismatching types somewhere, but I'm not sure what the types should be, or how to fix it.
struct test: View {
init() {
let emptyList = [Int]()
guard let encodedList = try? JSONEncoder().encode(emptyList) else { return }
self.storedList = encodedList
}
#AppStorage("stored_list") var storedList: Data //NO EXACT MATCHES TO CALL IN INITIALIZER
//"body" implementation not shown
}
Why is this error occurring, and how can I fix it?
It should be either with default value or optional, so correct variants are
#AppStorage("stored_list") var storedList: Data = Data()
or
#AppStorage("stored_list") var storedList: Data?

How to get value of a NSSingleObjectArrayI

func responseDataHandler(data: NSDictionary) {
let temperature_c = data.value(forKeyPath: "data.current_condition.temp_C")
DispatchQueue.main.async{
self.Temperature.text = temperature_c as? String
}
}
I have the above code where I am accessing a weather API which returns data in the form of an NSDictionary to this function. I need to access the value in temperature_c which when I try to print it, it says that it is: Optional(<__NSSingleObjectArrayI 0x600002147fd0>(
25
)
). Temperature is the outlet for label on my storyboard which I want to take on the value of 25 however as written now, it doesn't work and I have tried everything to try and access the value in the Single Object Array but nothing is working. I found this stack overflow question that was similar but it doesn't work for my situation because I keep getting the error that temperature_c is of type any and doesn't have subscripts.
The issue is that you can't cast to String an array, you should try to convert it to [String]. So could change your code to:
self.Temperature.text = (temperature_c as? [String])?.first ?? "Not available"
Let's go step by step:
temperature_c as? [String] tries to convert the NSDictionary to a String array which is the expectable type.
Since the previous step may return nil we have to use optional chaining ?. If we got a valid array using first return the the arrays first element.
Since both previous steps can return nil we can use nil coalescing operator to return a default value. In this case I use "Not available" but you can set any value.
You could write it in a more verbose way like this:
var text2Display2 = "Not available"
if let theArray = temperature_c as? [String] {
if let element = theArray.first {
text2Display2 = element
}
}
self.Temperature.text = text2Display2

Value of type _ cannot convert to specified type Set<> Swift

I'm trying to create a set of random exercises. I have made my struct Hashable and Equatable following the tutorial here https://medium.com/#JoyceMatos/hashable-protocols-in-swift-baf0cabeaebd and that is working fine so it's ready to be put in a Set<>.
When I use an Array to collect the workout exercises, as per below, it works fine. But when I switch to a Set<> I get an error "cannot convert value of type [_] to specified type 'Set'. What is it about 'Sets' that mean you can't map in the same way as an Array?
func generateWorkout() {
let allPossibleExercises = masterExerciseArray
let numberOfExercisesKey = Int(arc4random_uniform(4)+3)
//error triggers on the line below if I switch [WorkoutExercise]
//for Set<WorkoutExercise> (which conforms to Hashable/Equatable
let workoutSet : [WorkoutExercise] = (1...numberOfExercisesKey).map { _ in
let randomKey = Int(arc4random_uniform(UInt32(allPossibleExercises.count)))
return WorkoutExerciseGenerator( name: allPossibleExercises[randomKey].name,
maxReps: allPossibleExercises[randomKey].maxReps).generate()
}
print (workoutSet)
}
There is an answer here with a similar error message Cannot convert value of type '[_]' to specified type 'Array' but my array wouldn't be empty as in this example so I don't think this is the same root cause?
UPDATE : for anyone having the same problem, you can use Array but then simply convert the Array to a Set afterwards if the correct elements are Hashable/Equatable
If creating the array works create the array and then make the Set from the array. If all involved objects conform to Hashable this is supposed to work.
func generateWorkout() {
let allPossibleExercises = masterExerciseArray
let numberOfExercisesKey = Int(arc4random_uniform(4)+3)
let workoutArray : [WorkoutExercise] = (1...numberOfExercisesKey).map { _ in
let randomKey = Int(arc4random_uniform(UInt32(allPossibleExercises.count)))
return WorkoutExerciseGenerator( name: allPossibleExercises[randomKey].name,
maxReps: allPossibleExercises[randomKey].maxReps).generate()
}
let workoutSet = Set(workoutArray)
print (workoutSet)
}

.prepare vs. .select

I have a working connection to a database in an iOS10 app, using SQLite.swift.
I want to select details for a specific university where I have an IHE_ID passed in from another view controller.
I would like to just select the row for that specific university, but I can't get a query to work. I can however loop through all the data with a prepare and then choose the one I need from that, which of course is more resource intensive than I need since I already have the specific IHE_ID passed in as anIHE Int from the sending view controller.
connection is working so omitting that code...
do {
let db = try Connection(destinationDBPath, readonly: true)
let IHEs = Table("IHE")
let IHE_ID = Expression<Int>("IHE_ID")
let IHE_Name = Expression<String>("IHE_Name")
let IHE_COE_Url = Expression<String>("IHE_COE_URL")
let IHE_Sector = Expression<Int>("IHE_Sector")
let IHE_COE_Name = Expression<String>("IHE_COE_Name")
for ihe in try db.prepare(IHEs){
if (ihe[IHE_ID] == anIHE){
// build this string, otherwise ignore rest of dataset (doing this because query isn't working)
buildIHE = "Name: \(ihe[IHE_Name])\n"
buildIHE.append("URL: \(ihe[IHE_COE_Url])\n")
// buildIHE.append("Sector: \(ihe[IHE_Sector])\n")
if (ihe[IHE_Sector] == 0) {
buildIHE.append("Sector: Public\n")
} else {
buildIHE.append("Sector: Private\n")
}
buildIHE.append("College of Education Name: \(ihe[IHE_COE_Name])\n")
}
}
print ("Got through building IHE portion of view")
What I'd like to do is use this instead of the for loop.
if let query = IHEs.select(IHE_ID,IHE_Name,IHE_COE_Url,IHE_Sector,IHE_COE_Name).filter(IHE_ID == anIHE){
print("Query successful for \(anIHE) with name \(query[IHE_Name])")
// more actions to build the string I need would then occur
} else {
print("Query has failed or returned nil")
}
Finally, I'll use the selected elements if I can get the query to work.
I think I probably just have something wrong with my syntax on the query, but any help is appreciated.
The line with the "if let query" has this error in Xcode:
Initializer for conditional binding must have Optional type, not 'Table'.
This leads me to think it's something with my use of the .select statement and just new to using SQLite.swift and swift in general.
Last thing is that anIHE comes into this function as an Int, and IHE_ID is Expression as shown in this code. I'm thinking this may be part of the problem.
The Initializer for conditional binding must have Optional type error means that the expression on the right of the if let v = expr statement is not an Optional: there is no point using if let, and the Swift compiler says that you should just write let v = expr.
And indeed, IHEs.select(...).filter(...) returns a non-optional value of type Table.
It is not the database row you expect, because the query has been defined, but not executed yet. After all, you weren't using db: where would the rows be loaded from?
The solution is to bring back the database connection, and load a single row. This is done with the pluck method.
if let ihe = try db.pluck(IHEs.select(...).filter(...)) {
print("Name \(ihe[IHE_Name])")
}

Why the fatal error: Array index out of range show when print array in Swift?

I am new in Swift. I create a node swift file to store the node information. And the other group swift file is a group which store the all node.
The code of Node.swift is like the following:
class Node {
var id:UInt8=0
var type:Int=0
var name:String=""
var bdAddr:NSUUID!
//The node private packet counter
var nodePktNum:Int=0
}
The code of Group.swift is like the following:
class Group {
var mLedDevice:[LedDevice]? = [LedDevice]()
class LedDevice {
var node :Node?
var rssi :Int?
}
func allocateNode()
{
print("mLedDevice![0].node = \(mLedDevice![0].node))")
}
}
When I try call function (allocateNode) and try to print mLedDevice![0].node) via print("mLedDevice![0].node = \(mLedDevice![0].node))")
It show the error fatal error: Array index out of range.
Did I missing something for initialize of var mLedDevice:[LedDevice]? = [LedDevice]() ?
Thanks in advance.
===================================EDIT=================================
I want to add the item into array , so I create a parameter like let let leddevice : LedDevice , and try to give it some value. And add the leddevice into array mLedDevice. But it show constant 'leddevice' used before being initialized.
How to give the init value for let leddevice : LedDevice ?
func allocateNode()
{
let leddevice : LedDevice
leddevice.node?.id = UInt8(0)
leddevice.node!.bdAddr = NodeUUID
mLedDevice?.append(leddevice)
}
The only thing I can think about that can cause this is that the array is empty i.e. you are attempting to access index 0 of that array but that doesn't exist.
Try the following and it may give you an insight on how to solve it after seeing the content of the array:
print("mLedDevice = \(mLedDevice))")
In other words you are instantiating an array with no elements in it.
In your line of code
var mLedDevice:[LedDevice]? = [LedDevice]()
You are only initializing an empty array. What you are trying afterwards is to access the first element, of an empty array, which is out of bounds.
Before your print statement, you will need to add an item to your array
var ledDevice = LedDevice()
mLedDevice.append(ledDevice)
And then your print statement would not give you any errors.
UPDATED: Answer for the added question
let leddevice : LedDevice is defining a constant of type LedDevice but is not yet initialized, and then it is being used in the next lines of code. You should replace it with
let leddevice = LedDevice()
Which will also initialize the variable.
Note: If you have any further questions, you should write a new question for that.
Note2: Have you read any guides about initialization?