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

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?

Related

Getting nil values when `map`-ing over array of entities

I'm using Core Data in an iOS app and executing the following Swift code which results in an error at the second line:
let movies = (try? container.newBackgroundContext().fetch(request)) ?? []
return movies.map { $0.name! } // error: unexpectedly found nil while unwrapping...
Note that in the example above it's 100% certain that there is no entity in movies with nil in name. The corresponding attribute in the Core Data model is set to not optional.
When I change the code as shown below (i.e. not inlining newBackgroundContext()) there is no error:
let context = container.newBackgroundContext()
let movies = (try? context.fetch(request)) ?? []
return movies.map { $0.name! } // no error this time
I'm quite new to Swift and assume it has something to do with memory management (e.g. context is deinited prematurely) but I would appreciate an actual explanation of why the error occurs in the first code listing.
Whenever you run map function on your array it returns an optional value. seems like there is no value in name and you are doing force unwrapping. try to use compactMap.
movies.compactMap { $0.name }

How can convert Mirror To Orginal class or struct using swift language

i have an question about the Mirror reflection .
i convert my struct to mirror to iterate through all the properties to get values and after i iterate through it and change the values in properties i need to convert mirror again to the original struct with values which i edited but i can't , is swift language have way to do this conversion ?
the code below
//MARK:- loop get tags
func getTags(filter: Any){
let getTags = Mirror(reflecting: filter)
for (tag) in getTags.children {
if let getTag = tag.value as? String {
if let _ = Int(getTag) {
}else {
if getTag != "" && getTag != "All" {
arrayOfTags.append(getTag)
}
}
}// if let
}// end for loop
}
thanks
You can't construct a struct without hardcore memory manipulation. You could create objets with functions that are still available from Objective C. You could set the property with the setValue forKey function. Your objects needs to be derived from NSObject.
Doing this and taking into account all scenario's is quite a challenge. There is a CocoaPod library that could help with this. Have a look at EVReflection You could create a dictionary from your object and an object from your dictionary.
it's been asked a while a go.. but just in case I think you may be looking for https://cocoapods.org/pods/EVReflection

Swift Dictionaries with enum as Identifier not auto completing in completion block

I have a function that returns a completion. The completion has as in parameter a custom class with a dictionary that uses an enum as an identifier.
The problem I am facing is that Xcode does not recognises the type of the variable and does not auto complete when I use it inside of completion block.
My code looks like that
Function
func testFunc (completion:(PrxServiceResponseCallback) ->()){
let responseCallback = PrxServiceResponseCallback()
completion(responseCallback)
}
Class
class PrxServiceResponseCallback:NSObject{
var success = false
var resultCode:Int32 = 0
var response:[PrxResponseAttributes:Any] = [PrxResponseAttributes:Any]()
}
enum PrxResponseAttributes{
case sourceProtocolInfoArray
case sinkProtocolInfoArray
case connectionIDsArray
case connectionInfo
}
Calling the function
testFunc { (testResonse) in
testResonse.response[.]//Not Auto completing
}
The whole idea of making a dictionary with an enum as identifier was to make easier which attributes the dictionary returns but, If I can't auto complete, the idea is more pointless.
Any ideas?
Xcode doesn't give you autocomplete doesn't mean that your code doesn't compile. It just means that Xcode is too stupid to figure things out. I have encountered such situations many times before. It seems to always happen inside closures.
You can just ignore the fact that no autocomplete shows up and type the case name yourself:
testResonse.response[.sourceProtocolInfoArray]
It will compile.
You can also consider creating a struct instead of storing the values in a dictionary. Here's a sample struct (I guessed the types):
struct Response {
let sourceProtocolInfo: [String]
let sinkProtocolInfo: [String]
let connectionIDs: [Int]
let connectionInfo: String
}
If you use var response:[UIColor:Any] = [UIColor:Any]()
it is still not giving auto complete so it is not your issue and you can not do much on it .
If you need auto complete then use PrxResponseAttributes with . (dot)
you can do it like
testResonse.response[PrxResponseAttributes.sourceProtocolInfoArray]
EDIT
Note: It is only happening with implemented closure if you add one property in your PrxServiceResponseCallback class like
var anyValue:Any? {
return response[.connectionInfo] // it is showing completion
}

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)
}

How to fetch core data objects into Dictionary in Swift?

I've saved objects in core data, and I am looking how to fetch those objects as a Dictionary
Here is an example of my code where sections is keys for the dictionary and Company as an array of core data objects.
private var companies = Dictionary<String, Array<Company>>()
private var sections: [String] = ["Pending", "Active", "Pending"]
override func viewWillAppear(_ animated: Bool) {
let fetchRequest : NSFetchRequest<Company> = Company.fetchRequest()
let moc = DatabaseController.getContext()
do {
let request = try moc.fetch(fetchRequest)
for case let (index, object) in request.enumerated() {
companies[sections[index]]!.append(object)
}
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}}
When I am trying to execute my code, I have an error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Could anyone help me with that issue?
That error message means that you're force unwrapping an optional that doesn't have a value. In other words you're using ! where you shouldn't. (You should basically never use the force unwrap operator (!).)
Let's look at the line where you do that:
companies[sections[index]]!.append(object)
If we break this down and add the inferred types we have:
let section: String? = sections[index]
let companyArray: Array<Company> = companies[section]!
You're crashing because companies starts off empty, so asking for any of the arrays will return nil. (Actually, I'm not sure how your code is compiling, since you can't subscript into the dictionary with an optional.)
However, if you fix that, we still have a problem because you're using the index of the fetched array to look up the section. If you have more than three companies, that will start to fail.
I suspect you want something like:
for let company in result {
if var companyArray = companies[company.status] {
companyArray.append(company)
} else {
companies[company.status] = [company]
}
}
Where status is an invented property on Company that returns a String like "Pending" or "Active".
I've found the solution, just need to use NSFetchResultController in order to display data in TableView by different sections.